From ef82a1975dbd393b4a2696b7410cbc963073a36e Mon Sep 17 00:00:00 2001 From: Morten Mjelva Date: Mon, 14 Oct 2024 13:46:20 +0200 Subject: [PATCH] Move http server init to portal_service.go --- cmd/bb_portal/BUILD.bazel | 8 +-- cmd/bb_portal/main.go | 40 ++---------- cmd/bb_portal/portal_service.go | 36 +++++++++++ pkg/proto/configuration/bb_portal/BUILD.bazel | 2 - .../configuration/bb_portal/bb_portal.pb.go | 61 +++++++++---------- .../configuration/bb_portal/bb_portal.proto | 1 - 6 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 cmd/bb_portal/portal_service.go mode change 100755 => 100644 pkg/proto/configuration/bb_portal/bb_portal.pb.go diff --git a/cmd/bb_portal/BUILD.bazel b/cmd/bb_portal/BUILD.bazel index 4cb0b145..05211b08 100644 --- a/cmd/bb_portal/BUILD.bazel +++ b/cmd/bb_portal/BUILD.bazel @@ -4,7 +4,10 @@ load("@rules_oci//oci:defs.bzl", "oci_image", "oci_image_index") go_library( name = "bb_portal_lib", - srcs = ["main.go"], + srcs = [ + "main.go", + "portal_service.go", + ], importpath = "github.com/buildbarn/bb-portal/cmd/bb_portal", visibility = ["//visibility:private"], deps = [ @@ -17,7 +20,6 @@ go_library( "//pkg/processing", "//pkg/proto/configuration/bb_portal", "@com_github_99designs_gqlgen//graphql/handler", - "@com_github_99designs_gqlgen//graphql/handler/debug", "@com_github_99designs_gqlgen//graphql/playground", "@com_github_buildbarn_bb_storage//pkg/global", "@com_github_buildbarn_bb_storage//pkg/grpc", @@ -30,8 +32,6 @@ go_library( "@io_entgo_contrib//entgql", "@org_golang_google_genproto//googleapis/devtools/build/v1:build", "@org_golang_google_grpc//:grpc", - "@org_golang_google_grpc//codes", - "@org_golang_google_grpc//status", ], ) diff --git a/cmd/bb_portal/main.go b/cmd/bb_portal/main.go index db014b92..66b2876c 100644 --- a/cmd/bb_portal/main.go +++ b/cmd/bb_portal/main.go @@ -4,16 +4,9 @@ import ( "context" "flag" "log/slog" - "net/http" - "net/http/httputil" - "net/url" "os" "time" - "entgo.io/contrib/entgql" - "github.com/99designs/gqlgen/graphql/handler" - "github.com/99designs/gqlgen/graphql/handler/debug" - "github.com/99designs/gqlgen/graphql/playground" "github.com/fsnotify/fsnotify" "github.com/gorilla/mux" _ "github.com/mattn/go-sqlite3" @@ -24,9 +17,7 @@ import ( "github.com/buildbarn/bb-portal/ent/gen/ent" "github.com/buildbarn/bb-portal/ent/gen/ent/migrate" - "github.com/buildbarn/bb-portal/internal/api" "github.com/buildbarn/bb-portal/internal/api/grpc/bes" - "github.com/buildbarn/bb-portal/internal/graphql" "github.com/buildbarn/bb-portal/pkg/cas" "github.com/buildbarn/bb-portal/pkg/processing" "github.com/buildbarn/bb-portal/pkg/proto/configuration/bb_portal" @@ -43,7 +34,6 @@ const ( ) var ( - enableDebug = flag.Bool("debug", false, "Enable debugging mode.") dsDriver = flag.String("datasource-driver", "sqlite3", "Data source driver to use") dsURL = flag.String("datasource-url", "file:buildportal.db?_journal=WAL&_fk=1", "Data source URL for the DB") bepFolder = flag.String("bep-folder", "./bep-files/", "Folder to watch for new BEP files") @@ -71,14 +61,14 @@ func main() { return util.StatusWrap(err, "Failed to apply global configuration options") } - client, err := ent.Open( + dbClient, err := ent.Open( *dsDriver, *dsURL, ) if err != nil { return util.StatusWrapf(err, "Failed to open ent client") } - if err = client.Schema.Create(context.Background(), migrate.WithGlobalUniqueID(true)); err != nil { + if err = dbClient.Schema.Create(context.Background(), migrate.WithGlobalUniqueID(true)); err != nil { return util.StatusWrapf(err, "Failed to run schema migration") } @@ -91,26 +81,14 @@ func main() { return util.StatusWrapf(err, "Failed to create fsnotify.Watcher") } defer watcher.Close() - runWatcher(watcher, client, *bepFolder, blobArchiver) - - srv := handler.NewDefaultServer(graphql.NewSchema(client)) - srv.Use(entgql.Transactioner{TxOpener: client}) - if *enableDebug { - srv.Use(&debug.Tracer{}) - } + runWatcher(watcher, dbClient, *bepFolder, blobArchiver) router := mux.NewRouter() - router.PathPrefix("/graphql").Handler(srv) - router.Handle("/graphiql", playground.Handler("GraphQL Playground", "/graphql")) casManager := cas.NewConnectionManager(cas.ManagerParams{ TLSCACertFile: *caFile, CredentialsHelperCommand: *credentialsHelperCommand, }) - - router.Handle("/api/v1/blobs/{blobID}/{name}", api.NewBlobHandler(client, casManager)) - router.Handle("/api/v1/bep/upload", api.NewBEPUploadHandler(client, blobArchiver)).Methods("POST") - router.PathPrefix("/").Handler(frontendServer()) - + newPortalService(blobArchiver, casManager, dbClient, router) bb_http.NewServersFromConfigurationAndServe( configuration.HttpServers, bb_http.NewMetricsHandler(router, "Diagnostics"), @@ -120,7 +98,7 @@ func main() { if err := bb_grpc.NewServersFromConfigurationAndServe( configuration.GrpcServers, func(s go_grpc.ServiceRegistrar) { - build.RegisterPublishBuildEventServer(s.(*go_grpc.Server), bes.New(client, blobArchiver)) + build.RegisterPublishBuildEventServer(s.(*go_grpc.Server), bes.New(dbClient, blobArchiver)) }, siblingsGroup, ); err != nil { @@ -179,14 +157,6 @@ func runWatcher(watcher *fsnotify.Watcher, client *ent.Client, bepFolder string, } } -func frontendServer() http.Handler { - targetURL := &url.URL{ - Scheme: "http", - Host: "localhost:3000", - } - return httputil.NewSingleHostReverseProxy(targetURL) -} - func fatal(msg string, args ...any) { // Workaround: No slog.Fatal. slog.Error(msg, args...) diff --git a/cmd/bb_portal/portal_service.go b/cmd/bb_portal/portal_service.go new file mode 100644 index 00000000..c273cde9 --- /dev/null +++ b/cmd/bb_portal/portal_service.go @@ -0,0 +1,36 @@ +package main + +import ( + "net/http" + "net/http/httputil" + "net/url" + + "entgo.io/contrib/entgql" + "github.com/99designs/gqlgen/graphql/handler" + "github.com/99designs/gqlgen/graphql/playground" + "github.com/buildbarn/bb-portal/ent/gen/ent" + "github.com/buildbarn/bb-portal/internal/api" + "github.com/buildbarn/bb-portal/internal/graphql" + "github.com/buildbarn/bb-portal/pkg/cas" + "github.com/buildbarn/bb-portal/pkg/processing" + "github.com/gorilla/mux" +) + +func newPortalService(archiver processing.BlobMultiArchiver, casManager *cas.ConnectionManager, dbClient *ent.Client, router *mux.Router) { + srv := handler.NewDefaultServer(graphql.NewSchema(dbClient)) + srv.Use(entgql.Transactioner{TxOpener: dbClient}) + + router.PathPrefix("/graphql").Handler(srv) + router.Handle("/graphiql", playground.Handler("GraphQL Playground", "/graphql")) + router.Handle("/api/v1/blobs/{blobID}/{name}", api.NewBlobHandler(dbClient, casManager)) + router.Handle("/api/v1/bep/upload", api.NewBEPUploadHandler(dbClient, archiver)).Methods("POST") + router.PathPrefix("/").Handler(frontendServer()) +} + +func frontendServer() http.Handler { + targetURL := &url.URL{ + Scheme: "http", + Host: "localhost:3000", + } + return httputil.NewSingleHostReverseProxy(targetURL) +} diff --git a/pkg/proto/configuration/bb_portal/BUILD.bazel b/pkg/proto/configuration/bb_portal/BUILD.bazel index 0a5eb8a4..25ee3845 100644 --- a/pkg/proto/configuration/bb_portal/BUILD.bazel +++ b/pkg/proto/configuration/bb_portal/BUILD.bazel @@ -7,7 +7,6 @@ proto_library( srcs = ["bb_portal.proto"], visibility = ["//visibility:public"], deps = [ - "@com_github_buildbarn_bb_storage//pkg/proto/configuration/auth:auth_proto", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/global:global_proto", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/grpc:grpc_proto", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/http:http_proto", @@ -20,7 +19,6 @@ go_proto_library( proto = ":bb_portal_proto", visibility = ["//visibility:public"], deps = [ - "@com_github_buildbarn_bb_storage//pkg/proto/configuration/auth", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/global", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/grpc", "@com_github_buildbarn_bb_storage//pkg/proto/configuration/http", diff --git a/pkg/proto/configuration/bb_portal/bb_portal.pb.go b/pkg/proto/configuration/bb_portal/bb_portal.pb.go old mode 100755 new mode 100644 index 6183a832..ce3ed6da --- a/pkg/proto/configuration/bb_portal/bb_portal.pb.go +++ b/pkg/proto/configuration/bb_portal/bb_portal.pb.go @@ -7,7 +7,6 @@ package bb_portal import ( - _ "github.com/buildbarn/bb-storage/pkg/proto/configuration/auth" global "github.com/buildbarn/bb-storage/pkg/proto/configuration/global" grpc "github.com/buildbarn/bb-storage/pkg/proto/configuration/grpc" http "github.com/buildbarn/bb-storage/pkg/proto/configuration/http" @@ -95,39 +94,37 @@ var file_pkg_proto_configuration_bb_portal_bb_portal_proto_rawDesc = []byte{ 0x74, 0x61, 0x6c, 0x2f, 0x62, 0x62, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x21, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x62, 0x62, 0x5f, - 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, 0x1a, 0x27, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, 0x1a, 0x2b, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, - 0x61, 0x75, 0x74, 0x68, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, - 0x2b, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, - 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2f, - 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x27, 0x70, 0x6b, + 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2f, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x1a, 0x27, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, + 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x67, 0x72, 0x70, + 0x63, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x27, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, - 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2e, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x27, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x68, - 0x74, 0x74, 0x70, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x8d, - 0x02, 0x0a, 0x18, 0x41, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, - 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x54, 0x0a, 0x0c, 0x68, - 0x74, 0x74, 0x70, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x31, 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, - 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x68, 0x74, 0x74, 0x70, - 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x68, 0x74, 0x74, 0x70, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, - 0x73, 0x12, 0x54, 0x0a, 0x0c, 0x67, 0x72, 0x70, 0x63, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, - 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x31, 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, - 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, - 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x67, 0x72, 0x70, 0x63, - 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x12, 0x45, 0x0a, 0x06, 0x67, 0x6c, 0x6f, 0x62, 0x61, - 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2d, 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, - 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x2e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, - 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x06, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x42, 0x42, - 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x75, 0x69, - 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2f, 0x62, 0x62, 0x2d, 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, - 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, - 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x62, 0x62, 0x5f, 0x70, 0x6f, 0x72, 0x74, - 0x61, 0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x68, 0x74, 0x74, 0x70, 0x2e, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x8d, 0x02, 0x0a, 0x18, 0x41, 0x70, 0x70, 0x6c, 0x69, 0x63, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x12, 0x54, 0x0a, 0x0c, 0x68, 0x74, 0x74, 0x70, 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, + 0x72, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x31, 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, + 0x62, 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x2e, 0x68, 0x74, 0x74, 0x70, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, + 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0b, 0x68, 0x74, 0x74, + 0x70, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x12, 0x54, 0x0a, 0x0c, 0x67, 0x72, 0x70, 0x63, + 0x5f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x31, + 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x67, 0x72, 0x70, 0x63, 0x2e, 0x53, 0x65, + 0x72, 0x76, 0x65, 0x72, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x52, 0x0b, 0x67, 0x72, 0x70, 0x63, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x73, 0x12, 0x45, + 0x0a, 0x06, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x2d, + 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x69, + 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x67, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x2e, + 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x06, 0x67, + 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x42, 0x42, 0x5a, 0x40, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x62, 0x61, 0x72, 0x6e, 0x2f, 0x62, 0x62, + 0x2d, 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, 0x2f, 0x70, 0x6b, 0x67, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, + 0x62, 0x62, 0x5f, 0x70, 0x6f, 0x72, 0x74, 0x61, 0x6c, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( diff --git a/pkg/proto/configuration/bb_portal/bb_portal.proto b/pkg/proto/configuration/bb_portal/bb_portal.proto index 3604c535..3872a7a6 100644 --- a/pkg/proto/configuration/bb_portal/bb_portal.proto +++ b/pkg/proto/configuration/bb_portal/bb_portal.proto @@ -2,7 +2,6 @@ syntax = "proto3"; package buildbarn.configuration.bb_portal; -import "pkg/proto/configuration/auth/auth.proto"; import "pkg/proto/configuration/global/global.proto"; import "pkg/proto/configuration/grpc/grpc.proto"; import "pkg/proto/configuration/http/http.proto";