Skip to content

Commit

Permalink
Move http server init to portal_service.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenmj committed Oct 14, 2024
1 parent 9c4ef54 commit ef82a19
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
8 changes: 4 additions & 4 deletions cmd/bb_portal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
Expand All @@ -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",
],
)

Expand Down
40 changes: 5 additions & 35 deletions cmd/bb_portal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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")
Expand Down Expand Up @@ -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")
}

Expand All @@ -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"),
Expand All @@ -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 {
Expand Down Expand Up @@ -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...)
Expand Down
36 changes: 36 additions & 0 deletions cmd/bb_portal/portal_service.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 0 additions & 2 deletions pkg/proto/configuration/bb_portal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
61 changes: 29 additions & 32 deletions pkg/proto/configuration/bb_portal/bb_portal.pb.go
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/proto/configuration/bb_portal/bb_portal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit ef82a19

Please sign in to comment.