From 8cdd7ac8ac3e14809bec19ccd100646d96339910 Mon Sep 17 00:00:00 2001 From: robert-cronin Date: Mon, 11 Nov 2024 04:39:07 +0000 Subject: [PATCH] Export GetGraphqlServer and accept initialized backend Signed-off-by: robert-cronin --- cmd/guacgql/cmd/server.go | 29 +++++++---------------------- pkg/assembler/server/server.go | 18 ++++++++++++++++++ pkg/assembler/server/server_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 pkg/assembler/server/server.go create mode 100644 pkg/assembler/server/server_test.go diff --git a/cmd/guacgql/cmd/server.go b/cmd/guacgql/cmd/server.go index f7902088e6..845d5aea76 100644 --- a/cmd/guacgql/cmd/server.go +++ b/cmd/guacgql/cmd/server.go @@ -19,7 +19,6 @@ import ( "context" "errors" "fmt" - "github.com/guacsec/guac/pkg/version" "net/http" "os" "os/signal" @@ -27,7 +26,8 @@ import ( "syscall" "time" - "github.com/99designs/gqlgen/graphql/handler" + "github.com/guacsec/guac/pkg/version" + "github.com/99designs/gqlgen/graphql/handler/debug" "github.com/99designs/gqlgen/graphql/playground" "github.com/guacsec/guac/pkg/assembler/backends" @@ -35,10 +35,9 @@ import ( _ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue" "github.com/guacsec/guac/pkg/assembler/backends/neo4j" "github.com/guacsec/guac/pkg/assembler/backends/neptune" - "github.com/guacsec/guac/pkg/assembler/graphql/generated" - "github.com/guacsec/guac/pkg/assembler/graphql/resolvers" "github.com/guacsec/guac/pkg/assembler/kv" "github.com/guacsec/guac/pkg/assembler/kv/redis" + "github.com/guacsec/guac/pkg/assembler/server" "github.com/guacsec/guac/pkg/logging" "github.com/guacsec/guac/pkg/metrics" "github.com/spf13/cobra" @@ -79,12 +78,14 @@ func startServer(cmd *cobra.Command) { os.Exit(1) } - srv, err := getGraphqlServer(ctx) + backend, err := backends.Get(flags.backend, ctx, getOpts[flags.backend](ctx)) if err != nil { - logger.Errorf("unable to initialize graphql server: %v", err) + logger.Errorf("error creating %v backend: %w", flags.backend, err) os.Exit(1) } + srv := server.GetGraphqlServer(ctx, backend) + metric, err := setupPrometheus(ctx, "guacgql") if err != nil { logger.Fatalf("Error setting up Prometheus: %v", err) @@ -172,22 +173,6 @@ func validateFlags() error { return nil } -func getGraphqlServer(ctx context.Context) (*handler.Server, error) { - var topResolver resolvers.Resolver - - backend, err := backends.Get(flags.backend, ctx, getOpts[flags.backend](ctx)) - if err != nil { - return nil, fmt.Errorf("Error creating %v backend: %w", flags.backend, err) - } - topResolver = resolvers.Resolver{Backend: backend} - - config := generated.Config{Resolvers: &topResolver} - config.Directives.Filter = resolvers.Filter - srv := handler.NewDefaultServer(generated.NewExecutableSchema(config)) - - return srv, nil -} - func healthHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) _, _ = fmt.Fprint(w, "Server is healthy") diff --git a/pkg/assembler/server/server.go b/pkg/assembler/server/server.go new file mode 100644 index 0000000000..e532162408 --- /dev/null +++ b/pkg/assembler/server/server.go @@ -0,0 +1,18 @@ +package server + +import ( + "context" + + "github.com/99designs/gqlgen/graphql/handler" + "github.com/guacsec/guac/pkg/assembler/backends" + "github.com/guacsec/guac/pkg/assembler/graphql/generated" + "github.com/guacsec/guac/pkg/assembler/graphql/resolvers" +) + +func GetGraphqlServer(ctx context.Context, backend backends.Backend) *handler.Server { + topResolver := resolvers.Resolver{Backend: backend} + config := generated.Config{Resolvers: &topResolver} + config.Directives.Filter = resolvers.Filter + srv := handler.NewDefaultServer(generated.NewExecutableSchema(config)) + return srv +} diff --git a/pkg/assembler/server/server_test.go b/pkg/assembler/server/server_test.go new file mode 100644 index 0000000000..df4c2c15e1 --- /dev/null +++ b/pkg/assembler/server/server_test.go @@ -0,0 +1,26 @@ +package server + +import ( + "context" + "testing" + + _ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue" + + "github.com/guacsec/guac/internal/testing/stablememmap" + "github.com/guacsec/guac/pkg/assembler/backends" +) + +func TestGetGraphqlServer(t *testing.T) { + ctx := context.Background() + + store := stablememmap.GetStore() + backend, err := backends.Get("keyvalue", ctx, store) + if err != nil { + t.Errorf("Error getting backend: %v", err) + } + + srv := GetGraphqlServer(ctx, backend) + if srv == nil { + t.Errorf("Expected GetGraphqlServer to return a non-nil server") + } +}