This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add modeling service for abuse protection
- Loading branch information
Showing
14 changed files
with
881 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// 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. | ||
|
||
// This server builds or re-builds the statistical models for predicting the | ||
// future number of codes a realm with generate for abuse prevention. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/google/exposure-notifications-verification-server/pkg/buildinfo" | ||
"github.com/google/exposure-notifications-verification-server/pkg/config" | ||
"github.com/google/exposure-notifications-verification-server/pkg/controller/modeler" | ||
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit" | ||
"github.com/google/exposure-notifications-verification-server/pkg/render" | ||
"github.com/sethvargo/go-signalcontext" | ||
|
||
"github.com/google/exposure-notifications-server/pkg/logging" | ||
"github.com/google/exposure-notifications-server/pkg/observability" | ||
"github.com/google/exposure-notifications-server/pkg/server" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func main() { | ||
ctx, done := signalcontext.OnInterrupt() | ||
|
||
debug, _ := strconv.ParseBool(os.Getenv("LOG_DEBUG")) | ||
logger := logging.NewLogger(debug) | ||
logger = logger.With("build_id", buildinfo.BuildID) | ||
logger = logger.With("build_tag", buildinfo.BuildTag) | ||
|
||
ctx = logging.WithLogger(ctx, logger) | ||
|
||
err := realMain(ctx) | ||
done() | ||
|
||
if err != nil { | ||
logger.Fatal(err) | ||
} | ||
logger.Info("successful shutdown") | ||
} | ||
|
||
func realMain(ctx context.Context) error { | ||
logger := logging.FromContext(ctx) | ||
|
||
config, err := config.NewModeler(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to process config: %w", err) | ||
} | ||
|
||
// Setup monitoring | ||
logger.Info("configuring observability exporter") | ||
oeConfig := config.ObservabilityExporterConfig() | ||
oe, err := observability.NewFromEnv(ctx, oeConfig) | ||
if err != nil { | ||
return fmt.Errorf("unable to create ObservabilityExporter provider: %w", err) | ||
} | ||
if err := oe.StartExporter(); err != nil { | ||
return fmt.Errorf("error initializing observability exporter: %w", err) | ||
} | ||
defer oe.Close() | ||
logger.Infow("observability exporter", "config", oeConfig) | ||
|
||
// Setup database | ||
db, err := config.Database.Load(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to load database config: %w", err) | ||
} | ||
if err := db.Open(ctx); err != nil { | ||
return fmt.Errorf("failed to connect to database: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
// Create the renderer | ||
h, err := render.New(ctx, "", config.DevMode) | ||
if err != nil { | ||
return fmt.Errorf("failed to create renderer: %w", err) | ||
} | ||
|
||
// Create the router | ||
r := mux.NewRouter() | ||
|
||
// Rate limiting | ||
limiterStore, err := ratelimit.RateLimiterFor(ctx, &config.RateLimit) | ||
if err != nil { | ||
return fmt.Errorf("failed to create limiter: %w", err) | ||
} | ||
defer limiterStore.Close(ctx) | ||
|
||
modelerController := modeler.New(ctx, config, db, limiterStore, h) | ||
r.Handle("/", modelerController.HandleModel()).Methods("POST") | ||
|
||
srv, err := server.New(config.Port) | ||
if err != nil { | ||
return fmt.Errorf("failed to create server: %w", err) | ||
} | ||
logger.Infow("server listening", "port", config.Port) | ||
return srv.ServeHTTPHandler(ctx, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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 config | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/exposure-notifications-verification-server/pkg/database" | ||
"github.com/google/exposure-notifications-verification-server/pkg/ratelimit" | ||
|
||
"github.com/google/exposure-notifications-server/pkg/observability" | ||
|
||
"github.com/sethvargo/go-envconfig" | ||
) | ||
|
||
// Modeler is the configuration for the modeler service. | ||
type Modeler struct { | ||
Database database.Config | ||
Observability observability.Config | ||
RateLimit ratelimit.Config | ||
|
||
// DevMode produces additional debugging information. Do not enable in | ||
// production environments. | ||
DevMode bool `env:"DEV_MODE"` | ||
|
||
Port string `env:"PORT, default=8080"` | ||
} | ||
|
||
// NewModeler returns the config for the modeler server. | ||
func NewModeler(ctx context.Context) (*Modeler, error) { | ||
var config Modeler | ||
if err := ProcessWith(ctx, &config, envconfig.OsLookuper()); err != nil { | ||
return nil, err | ||
} | ||
return &config, nil | ||
} | ||
|
||
func (c *Modeler) Validate() error { | ||
return nil | ||
} | ||
|
||
func (c *Modeler) ObservabilityExporterConfig() *observability.Config { | ||
return &c.Observability | ||
} |
Oops, something went wrong.