Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/pkgsite: Adding function to setup server #3

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions internal/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions internal/frontend/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions internal/testing/integration/frontend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down