generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
124 lines (106 loc) · 3.65 KB
/
application.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package boot
import (
"context"
"database/sql"
"github.com/broadinstitute/sherlock/internal/auth"
"github.com/broadinstitute/sherlock/internal/auth/gha_oidc_auth"
"github.com/broadinstitute/sherlock/internal/boot/liveness"
"github.com/broadinstitute/sherlock/internal/config"
"github.com/broadinstitute/sherlock/internal/db"
"github.com/broadinstitute/sherlock/internal/metrics/v2metrics"
"github.com/broadinstitute/sherlock/internal/models/v2models"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"net/http"
"time"
)
type Application struct {
sqlDB *sql.DB
gormDB *gorm.DB
cancelCtx context.CancelFunc
server *http.Server
}
func (a *Application) Start() {
log.Info().Msgf("BOOT | connecting to database...")
if sqlDB, err := db.Connect(); err != nil {
log.Fatal().Msgf("db.Connect() err: %v", err)
} else {
a.sqlDB = sqlDB
}
log.Info().Msgf("BOOT | starting liveness endpoint...")
go liveness.Start()
log.Info().Msgf("BOOT | migrating database and configuring ORM...")
if gormDB, err := db.Configure(a.sqlDB); err != nil {
log.Fatal().Msgf("db.Configure() err: %v", err)
} else {
a.gormDB = gormDB
}
log.Info().Msgf("BOOT | creating global context...")
ctx, cancelCtx := context.WithCancel(context.Background())
a.cancelCtx = cancelCtx
if config.Config.MustString("mode") != "debug" {
log.Info().Msgf("BOOT | caching Firecloud accounts...")
if err := auth.CacheFirecloudAccounts(ctx); err != nil {
log.Fatal().Msgf("auth.CacheFirecloudAccounts() err: %v", err)
}
go auth.KeepFirecloudCacheUpdated(ctx)
}
log.Info().Msgf("BOOT | initializing GitHub Actions OIDC token verification...")
if err := gha_oidc_auth.InitVerifier(ctx); err != nil {
log.Fatal().Msgf("gha_oidc_auth.InitVerifier() err: %v", err)
}
log.Info().Msgf("BOOT | reading extra permissions defined in configuration...")
auth.CacheExtraPermissions()
if config.Config.Bool("metrics.v2.enable") {
log.Info().Msgf("BOOT | registering metric views...")
if err := v2metrics.RegisterViews(); err != nil {
log.Fatal().Msgf("v2metrics.RegisterViews() err: %v", err)
}
log.Info().Msgf("BOOT | calculating metric values...")
if err := v2models.UpdateMetrics(ctx, a.gormDB); err != nil {
log.Fatal().Msgf("v2models.UpdateMetrics() err: %v", err)
}
go v2models.KeepMetricsUpdated(ctx, a.gormDB)
}
log.Info().Msgf("BOOT | building Gin router...")
if router, err := buildRouter(a.gormDB); err != nil {
log.Fatal().Msgf("buildRouter() err: %v", err)
} else {
a.server = &http.Server{
Addr: ":8080",
Handler: router,
}
}
log.Info().Msgf("BOOT | starting server...")
if err := a.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Msgf("server.ListenAndServe() err: %v", err)
}
}
func (a *Application) Stop() {
if a.server != nil {
log.Info().Msgf("BOOT | shutting down server...")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := a.server.Shutdown(ctx); err != nil {
log.Warn().Msgf("server.Shutdown() err: %v", err)
}
} else {
log.Info().Msgf("BOOT | no server; skipping shutting down server")
}
if a.cancelCtx != nil {
log.Info().Msgf("BOOT | canceling global context...")
a.cancelCtx()
} else {
log.Info().Msgf("BOOT | no context cancellation function, skipping canceling global context")
}
if a.sqlDB != nil {
log.Info().Msgf("BOOT | closing database connections...")
if err := a.sqlDB.Close(); err != nil {
log.Warn().Msgf("sqlDB.Close() err: %v", err)
}
} else {
log.Info().Msgf("BOOT | no SQL database reference, skipping closing database connections")
}
log.Info().Msgf("BOOT | stopping liveness endpoint...")
liveness.Stop()
}