-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
bc9ee31
commit 0d83382
Showing
37 changed files
with
1,467 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package playground | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Command(parents ...string) *cobra.Command { | ||
var command options | ||
cmd := &cobra.Command{ | ||
Use: "playground", | ||
Short: "playground", | ||
Long: "Serve playground", | ||
Args: cobra.NoArgs, | ||
SilenceUsage: true, | ||
RunE: command.Run, | ||
} | ||
// server flags | ||
cmd.Flags().StringVar(&command.serverFlags.host, "server-host", "0.0.0.0", "server host") | ||
cmd.Flags().IntVar(&command.serverFlags.port, "server-port", 8080, "server port") | ||
// gin flags | ||
cmd.Flags().StringVar(&command.ginFlags.mode, "gin-mode", gin.ReleaseMode, "gin run mode") | ||
cmd.Flags().BoolVar(&command.ginFlags.log, "gin-log", true, "enable gin logger") | ||
cmd.Flags().BoolVar(&command.ginFlags.cors, "gin-cors", true, "enable gin cors") | ||
cmd.Flags().IntVar(&command.ginFlags.maxBodySize, "gin-max-body-size", 2*1024*1024, "gin max body size") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package playground | ||
|
||
import ( | ||
"context" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/kyverno/kyverno-json/pkg/server" | ||
"github.com/kyverno/kyverno-json/pkg/server/ui" | ||
"github.com/loopfz/gadgeto/tonic" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type options struct { | ||
serverFlags serverFlags | ||
ginFlags ginFlags | ||
} | ||
|
||
type serverFlags struct { | ||
host string | ||
port int | ||
} | ||
|
||
type ginFlags struct { | ||
mode string | ||
log bool | ||
cors bool | ||
maxBodySize int | ||
} | ||
|
||
func (c *options) Run(_ *cobra.Command, _ []string) error { | ||
// initialise gin framework | ||
gin.SetMode(c.ginFlags.mode) | ||
tonic.SetBindHook(tonic.DefaultBindingHookMaxBodyBytes(int64(c.ginFlags.maxBodySize))) | ||
// create router | ||
router, err := server.New(c.ginFlags.log, c.ginFlags.cors) | ||
if err != nil { | ||
return err | ||
} | ||
// register api routes | ||
if err := ui.AddRoutes(router); err != nil { | ||
return err | ||
} | ||
// run server | ||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer stop() | ||
shutdown := server.Run(ctx, router, c.serverFlags.host, c.serverFlags.port) | ||
<-ctx.Done() | ||
stop() | ||
if shutdown != nil { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
if err := shutdown(ctx); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build !js && !wasm | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func Run(_ context.Context, s Server, host string, port int) Shutdown { | ||
address := fmt.Sprintf("%v:%v", host, port) | ||
srv := &http.Server{ | ||
Addr: address, | ||
Handler: s.Handler(), | ||
ReadHeaderTimeout: 3 * time.Second, | ||
} | ||
go func() { | ||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
panic(err) | ||
} | ||
}() | ||
return srv.Shutdown | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/server/playground/scan/handler.go → pkg/server/playground/handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package scan | ||
package playground | ||
|
||
import ( | ||
"context" | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/server/playground/scan/request.go → pkg/server/playground/request.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package scan | ||
package playground | ||
|
||
type Request struct { | ||
Payload string `json:"payload"` | ||
|
2 changes: 1 addition & 1 deletion
2
pkg/server/playground/scan/response.go → pkg/server/playground/response.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package scan | ||
package playground | ||
|
||
import ( | ||
"github.com/kyverno/kyverno-json/pkg/apis/v1alpha1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.