Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Break Monolith's realMain into an importable utility method for testing. #441

Merged
merged 1 commit into from
May 26, 2020
Merged
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
95 changes: 2 additions & 93 deletions cmd/monolith/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,110 +17,19 @@ package main

import (
"context"
"fmt"
"net/http"

"go.opencensus.io/plugin/ochttp"

"github.com/google/exposure-notifications-server/internal/authorizedapp"
"github.com/google/exposure-notifications-server/internal/cleanup"
"github.com/google/exposure-notifications-server/internal/database"
"github.com/google/exposure-notifications-server/internal/export"
"github.com/google/exposure-notifications-server/internal/federationin"
"github.com/google/exposure-notifications-server/internal/handlers"
"github.com/google/exposure-notifications-server/internal/logging"
"github.com/google/exposure-notifications-server/internal/publish"
"github.com/google/exposure-notifications-server/internal/setup"
"github.com/google/exposure-notifications-server/internal/storage"
"github.com/google/exposure-notifications-server/internal/monolith"

// Enable observability with distributed tracing and metrics.
_ "github.com/google/exposure-notifications-server/internal/observability"
)

var _ setup.DBConfigProvider = (*MonoConfig)(nil)
var _ setup.AuthorizedAppConfigProvider = (*MonoConfig)(nil)
var _ setup.BlobStorageConfigProvider = (*MonoConfig)(nil)
var _ setup.KeyManagerProvider = (*MonoConfig)(nil)

type MonoConfig struct {
Port string `envconfig:"PORT" default:"8080"`

AuthorizedApp *authorizedapp.Config
Cleanup *cleanup.Config
Export *export.Config
Publish *publish.Config
Database *database.Config
FederationIn *federationin.Config
BlobstoreType string `envconfig:"BLOBSTORE_TYPE" default:"CLOUD_STORAGE"`
}

func (c *MonoConfig) DB() *database.Config { return c.Database }
func (c *MonoConfig) KeyManager() bool { return true }
func (c *MonoConfig) BlobStorage() storage.BlobstoreConfig {
return storage.BlobstoreConfig{
BlobstoreType: storage.BlobstoreType(c.BlobstoreType),
}
}
func (c *MonoConfig) AuthorizedAppConfig() *authorizedapp.Config { return c.AuthorizedApp }

func main() {
ctx := context.Background()
logger := logging.FromContext(ctx)

if err := realMain(ctx); err != nil {
if err := monolith.RunServer(ctx); err != nil {
logger.Fatal(err)
}
}

func realMain(ctx context.Context) error {
logger := logging.FromContext(ctx)

var config MonoConfig
env, closer, err := setup.Setup(ctx, &config)
if err != nil {
return fmt.Errorf("setup.Setup: %w", err)
}
defer closer()

mux := http.NewServeMux()

// Cleanup export
cleanupExport, err := cleanup.NewExportHandler(config.Cleanup, env)
if err != nil {
return fmt.Errorf("cleanup.NewExportHandler: %w", err)
}
mux.Handle("/cleanup-export", cleanupExport)

// Cleanup exposure
cleanupExposure, err := cleanup.NewExposureHandler(config.Cleanup, env)
if err != nil {
return fmt.Errorf("cleanup.NewExposureHandler: %w", err)
}
mux.Handle("/cleanup-exposure", cleanupExposure)

// Export
exportServer, err := export.NewServer(config.Export, env)
if err != nil {
return fmt.Errorf("export.NewServer: %w", err)
}
mux.HandleFunc("/export/create-batches", exportServer.CreateBatchesHandler)
mux.HandleFunc("/export/do-work", exportServer.WorkerHandler)

// Federation in
mux.Handle("/federation-in", federationin.NewHandler(env, config.FederationIn))

// Federation out
// TODO: this is a grpc listener and requires a lot of setup.

// Publish
publishServer, err := publish.NewHandler(ctx, config.Publish, env)
if err != nil {
return fmt.Errorf("publish.NewHandler: %w", err)
}
mux.HandleFunc("/publish", handlers.WithMinimumLatency(config.Publish.MinRequestDuration, publishServer))

instrumentedHandler := &ochttp.Handler{Handler: mux}

logger.Infof("monolith running at :%s", config.Port)
return http.ListenAndServe(":"+config.Port, instrumentedHandler)
}
118 changes: 118 additions & 0 deletions internal/monolith/monolith_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main runs all the server components at different URL paths.
package monolith

import (
"context"
"fmt"
"net/http"

"go.opencensus.io/plugin/ochttp"

"github.com/google/exposure-notifications-server/internal/authorizedapp"
"github.com/google/exposure-notifications-server/internal/cleanup"
"github.com/google/exposure-notifications-server/internal/database"
"github.com/google/exposure-notifications-server/internal/export"
"github.com/google/exposure-notifications-server/internal/federationin"
"github.com/google/exposure-notifications-server/internal/handlers"
"github.com/google/exposure-notifications-server/internal/logging"
"github.com/google/exposure-notifications-server/internal/publish"
"github.com/google/exposure-notifications-server/internal/setup"
"github.com/google/exposure-notifications-server/internal/storage"

// Enable observability with distributed tracing and metrics.
_ "github.com/google/exposure-notifications-server/internal/observability"
)

var _ setup.DBConfigProvider = (*MonoConfig)(nil)
var _ setup.AuthorizedAppConfigProvider = (*MonoConfig)(nil)
var _ setup.BlobStorageConfigProvider = (*MonoConfig)(nil)
var _ setup.KeyManagerProvider = (*MonoConfig)(nil)

type MonoConfig struct {
Port string `envconfig:"PORT" default:"8080"`

AuthorizedApp *authorizedapp.Config
Cleanup *cleanup.Config
Export *export.Config
Publish *publish.Config
Database *database.Config
FederationIn *federationin.Config
BlobstoreType string `envconfig:"BLOBSTORE_TYPE" default:"CLOUD_STORAGE"`
}

func (c *MonoConfig) DB() *database.Config { return c.Database }
func (c *MonoConfig) KeyManager() bool { return true }
func (c *MonoConfig) BlobStorage() storage.BlobstoreConfig {
return storage.BlobstoreConfig{
BlobstoreType: storage.BlobstoreType(c.BlobstoreType),
}
}
func (c *MonoConfig) AuthorizedAppConfig() *authorizedapp.Config { return c.AuthorizedApp }

func RunServer(ctx context.Context) error {

logger := logging.FromContext(ctx)

var config MonoConfig
env, closer, err := setup.Setup(ctx, &config)
if err != nil {
return fmt.Errorf("setup.Setup: %w", err)
}
defer closer()

mux := http.NewServeMux()

// Cleanup export
cleanupExport, err := cleanup.NewExportHandler(config.Cleanup, env)
if err != nil {
return fmt.Errorf("cleanup.NewExportHandler: %w", err)
}
mux.Handle("/cleanup-export", cleanupExport)

// Cleanup exposure
cleanupExposure, err := cleanup.NewExposureHandler(config.Cleanup, env)
if err != nil {
return fmt.Errorf("cleanup.NewExposureHandler: %w", err)
}
mux.Handle("/cleanup-exposure", cleanupExposure)

// Export
exportServer, err := export.NewServer(config.Export, env)
if err != nil {
return fmt.Errorf("export.NewServer: %w", err)
}
mux.HandleFunc("/export/create-batches", exportServer.CreateBatchesHandler)
mux.HandleFunc("/export/do-work", exportServer.WorkerHandler)

// Federation in
mux.Handle("/federation-in", federationin.NewHandler(env, config.FederationIn))

// Federation out
// TODO: this is a grpc listener and requires a lot of setup.

// Publish
publishServer, err := publish.NewHandler(ctx, config.Publish, env)
if err != nil {
return fmt.Errorf("publish.NewHandler: %w", err)
}
mux.HandleFunc("/publish", handlers.WithMinimumLatency(config.Publish.MinRequestDuration, publishServer))

instrumentedHandler := &ochttp.Handler{Handler: mux}

logger.Infof("monolith running at :%s", config.Port)
return http.ListenAndServe(":"+config.Port, instrumentedHandler)
}