diff --git a/cmd/frontend/main.go b/cmd/frontend/main.go index 7c4f0fb7d..f7a82ee34 100644 --- a/cmd/frontend/main.go +++ b/cmd/frontend/main.go @@ -92,7 +92,7 @@ func main() { Addr: cfg.RedisHAHost + ":" + cfg.RedisHAPort, }) } - server, err := frontend.NewServer(frontend.ServerConfig{ + config := frontend.ServerConfig{ DataSource: ds, Queue: fetchQueue, CompletionClient: haClient, @@ -101,9 +101,6 @@ func main() { ThirdPartyPath: *thirdPartyPath, DevMode: *devMode, AppVersionLabel: cfg.AppVersionLabel(), - }) - if err != nil { - log.Fatalf(ctx, "frontend.NewServer: %v", err) } router := dcensus.NewRouter(frontend.TagRoute) var cacheClient *redis.Client @@ -112,7 +109,10 @@ func main() { Addr: cfg.RedisCacheHost + ":" + cfg.RedisCachePort, }) } - server.Install(router.Handle, cacheClient) + server, err := frontend.CreateAndInstallServer(config, router.Handle, cacheClient) + if err != nil { + log.Fatalf(ctx, "frontend.NewServer: %v", err) + } views := append(dcensus.ServerViews, postgres.SearchLatencyDistribution, postgres.SearchResponseCount, diff --git a/internal/frontend/server.go b/internal/frontend/server.go index cc54d5765..bd699f69e 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go @@ -449,3 +449,18 @@ func parsePageTemplates(base template.TrustedSource) (map[string]*template.Templ } return templates, nil } + +// CreateAndInstallServer creates a new server object, and installs and registers the routes given to it. +func CreateAndInstallServer(config ServerConfig, handle func(string, http.Handler), redisClient *redis.Client)(*Server, error){ + server, err := NewServer(config) + if err != nil{ + return nil, err + } + if redisClient != nil{ + server.Install(handle, redisClient) + }else{ + server.Install(handle, nil) + } + + return server, nil +} diff --git a/internal/frontend/server_test.go b/internal/frontend/server_test.go index fb67bb12f..45147b42e 100644 --- a/internal/frontend/server_test.go +++ b/internal/frontend/server_test.go @@ -997,19 +997,19 @@ func newTestServer(t *testing.T, proxyModules []*proxy.TestModule, experimentNam return FetchAndUpdateState(ctx, mpath, version, proxyClient, sourceClient, testDB) }) - s, err := NewServer(ServerConfig{ + config := ServerConfig{ DataSource: testDB, Queue: q, TaskIDChangeInterval: 10 * time.Minute, StaticPath: template.TrustedSourceFromConstant("../../content/static"), ThirdPartyPath: "../../third_party", AppVersionLabel: "", - }) + } + mux := http.NewServeMux() + s, err := CreateAndInstallServer(config, mux.Handle, nil) if err != nil { t.Fatal(err) } - mux := http.NewServeMux() - s.Install(mux.Handle, nil) var exps []*internal.Experiment for _, n := range experimentNames { diff --git a/internal/testing/integration/frontend_test.go b/internal/testing/integration/frontend_test.go index f8c974e9f..889c94c2a 100644 --- a/internal/testing/integration/frontend_test.go +++ b/internal/testing/integration/frontend_test.go @@ -166,23 +166,22 @@ func TestModulePackageDirectoryResolution(t *testing.T) { } } -// TODO(https://github.com/golang/go/issues/40096): factor out this code reduce -// duplication func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.Server { t.Helper() - s, err := frontend.NewServer(frontend.ServerConfig{ + config := frontend.ServerConfig{ DataSource: testDB, TaskIDChangeInterval: 10 * time.Minute, StaticPath: template.TrustedSourceFromConstant("../../../content/static"), ThirdPartyPath: "../../../third_party", AppVersionLabel: "", Queue: q, - }) + } + + mux := http.NewServeMux() + s, err := frontend.CreateAndInstallServer(config, mux.Handle, nil) if err != nil { t.Fatal(err) } - mux := http.NewServeMux() - s.Install(mux.Handle, nil) experimenter, err := middleware.NewExperimenter(ctx, 1*time.Minute, testDB) if err != nil {