This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
96 lines (83 loc) · 2.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"time"
"github.com/dstotijn/ct-diag-server/api"
"github.com/dstotijn/ct-diag-server/db/postgres"
"github.com/dstotijn/ct-diag-server/diag"
"go.uber.org/zap"
)
func main() {
ctx := context.Background()
var (
addr string
maxUploadBatchSize uint
isDev bool
cacheInterval time.Duration
)
flag.StringVar(&addr, "addr", ":80", "HTTP listen address")
flag.UintVar(&maxUploadBatchSize, "maxUploadBatchSize", 14, "Maximum upload batch size")
flag.BoolVar(&isDev, "dev", false, "Boolean indicating whether the app is running in a dev environment")
flag.DurationVar(&cacheInterval, "cacheInterval", 5*time.Minute, "Interval between cache refresh")
flag.Parse()
logger, err := newLogger(isDev)
if err != nil {
log.Fatal(err)
}
defer logger.Sync()
zap.RedirectStdLog(logger)
db, err := postgres.New(mustGetEnv("POSTGRES_DSN"))
if err != nil {
logger.Fatal("Could not create PostgreSQL client.", zap.Error(err))
}
defer db.Close()
err = db.Ping()
if err != nil {
logger.Fatal("Could not connect to database.", zap.Error(err))
}
exposureCfg := diag.ExposureConfig{
MinimumRiskScore: 0,
AttenuationLevelValues: []int{1, 2, 3, 4, 5, 6, 7, 8},
AttenuationWeight: 50,
DaysSinceLastExposureLevelValues: []int{1, 2, 3, 4, 5, 6, 7, 8},
DaysSinceLastExposureWeight: 50,
DurationLevelValues: []int{1, 2, 3, 4, 5, 6, 7, 8},
DurationWeight: 50,
TransmissionRiskLevelValues: []int{1, 2, 3, 4, 5, 6, 7, 8},
TransmissionRiskWeight: 50,
}
cfg := diag.Config{
Repository: db,
Cache: &diag.MemoryCache{},
CacheInterval: cacheInterval,
MaxUploadBatchSize: maxUploadBatchSize,
ExposureConfig: exposureCfg,
Logger: logger,
}
handler, err := api.NewHandler(ctx, cfg, logger)
if err != nil {
logger.Fatal("Could not create HTTP handler.", zap.Error(err))
}
// Start the HTTP server.
logger.Info("Server started.", zap.String("addr", addr))
if err := http.ListenAndServe(addr, handler); err != nil {
logger.Fatal("Server stopped.", zap.Error(err))
}
}
func mustGetEnv(key string) string {
v := os.Getenv(key)
if v == "" {
log.Fatalf("Environment variable `%s` cannot be empty.", key)
}
return v
}
func newLogger(isDev bool) (*zap.Logger, error) {
if isDev {
return zap.NewDevelopment()
}
return zap.NewProduction()
}