-
Notifications
You must be signed in to change notification settings - Fork 129
/
main.go
97 lines (79 loc) · 2.01 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
97
package main
import (
"context"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/armadaproject/armada/internal/common"
"github.com/armadaproject/armada/internal/common/database"
"github.com/armadaproject/armada/internal/lookoutv2"
"github.com/armadaproject/armada/internal/lookoutv2/configuration"
"github.com/armadaproject/armada/internal/lookoutv2/schema"
)
const (
CustomConfigLocation string = "config"
MigrateDatabase = "migrateDatabase"
)
func init() {
pflag.StringSlice(
CustomConfigLocation,
[]string{},
"Fully qualified path to application configuration file (for multiple config files repeat this arg or separate paths with commas)",
)
pflag.Bool(MigrateDatabase, false, "Migrate database instead of running server")
pflag.Parse()
}
func makeContext() (context.Context, func()) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-c:
cancel()
case <-ctx.Done():
}
}()
return ctx, func() {
signal.Stop(c)
cancel()
}
}
func migrate(ctx context.Context, config configuration.LookoutV2Configuration) {
db, err := database.OpenPgxPool(config.Postgres)
if err != nil {
panic(err)
}
migrations, err := schema.LookoutMigrations()
if err != nil {
panic(err)
}
err = database.UpdateDatabase(ctx, db, migrations)
if err != nil {
panic(err)
}
}
func main() {
common.ConfigureLogging()
common.BindCommandlineArguments()
var config configuration.LookoutV2Configuration
userSpecifiedConfigs := viper.GetStringSlice(CustomConfigLocation)
common.LoadConfig(&config, "./config/lookoutv2", userSpecifiedConfigs)
log.SetLevel(log.DebugLevel)
ctx, cleanup := makeContext()
defer cleanup()
if viper.GetBool(MigrateDatabase) {
log.Info("Migrating database")
migrate(ctx, config)
return
}
err := lookoutv2.Serve(config)
if err != nil {
log.Error(err)
os.Exit(1)
}
}