This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
220 lines (192 loc) · 7.04 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"context"
"flag"
"net/http"
"os"
"os/user"
"runtime"
"time"
"github.com/fabric8-services/admin-console/app"
"github.com/fabric8-services/admin-console/application"
"github.com/fabric8-services/admin-console/configuration"
"github.com/fabric8-services/admin-console/controller"
"github.com/fabric8-services/admin-console/migration"
authsupport "github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/closeable"
"github.com/fabric8-services/fabric8-common/goamiddleware"
"github.com/fabric8-services/fabric8-common/log"
"github.com/fabric8-services/fabric8-common/metric"
"github.com/goadesign/goa"
goalogrus "github.com/goadesign/goa/logging/logrus"
"github.com/goadesign/goa/middleware"
"github.com/goadesign/goa/middleware/gzip"
"github.com/goadesign/goa/middleware/security/jwt"
"github.com/google/gops/agent"
"github.com/jinzhu/gorm"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/lib/pq"
)
func main() {
// --------------------------------------------------------------------
// Parse flags
// --------------------------------------------------------------------
var configFilePath string
var printConfig bool
var migrateDB bool
flag.StringVar(&configFilePath, "config", "", "Path to the config file to read")
flag.BoolVar(&printConfig, "printConfig", false, "Prints the config (including merged environment variables) and exits")
flag.BoolVar(&migrateDB, "migrateDatabase", false, "Migrates the database to the newest version and exits.")
flag.Parse()
config := configuration.New()
// Initialized developer mode flag and log level for the logger
log.InitializeLogger(config.IsLogJSON(), config.GetLogLevel())
var db *gorm.DB
var err error
for {
db, err = gorm.Open("postgres", config.GetPostgresConfigString())
if err != nil {
log.Logger().Errorf("ERROR: Unable to open connection to database %v", err)
closeable.Close(context.TODO(), db)
log.Logger().Infof("Retrying to connect in %v...", config.GetPostgresConnectionRetrySleep())
time.Sleep(config.GetPostgresConnectionRetrySleep())
} else {
defer closeable.Close(context.TODO(), db)
break
}
}
// Migrate the schema
err = migration.Migrate(db.DB(), config.GetPostgresDatabase())
if err != nil {
log.Panic(nil, map[string]interface{}{
"err": err,
}, "failed migration")
}
// Nothing to here except exit, since the migration is already performed.
if migrateDB {
os.Exit(0)
}
// Initialize sentry client
// haltSentry, err := sentry.InitializeSentryClient(
// nil, // will use the `os.Getenv("Sentry_DSN")` instead
// sentry.WithRelease(app.Commit),
// sentry.WithEnvironment(config.GetEnvironment()),
// )
// if err != nil {
// log.Panic(context.TODO(), map[string]interface{}{
// "err": err,
// }, "failed to setup the sentry client")
// }
// defer haltSentry()
// Create service
service := goa.New("admin-console")
// Mount middleware
service.Use(middleware.RequestID())
// Use our own log request to inject identity id and modify other properties
service.Use(gzip.Middleware(9))
service.Use(app.ErrorHandler(service, true))
service.Use(middleware.Recover())
// record HTTP request metrics in prometh
service.Use(
metric.Recorder(
"admin_console",
metric.WithRequestDurationBucket(prometheus.ExponentialBuckets(0.05, 2, 8))))
service.WithLogger(goalogrus.New(log.Logger()))
appDB := application.NewGormApplication(db)
tokenManager, err := authsupport.DefaultManager(config)
if err != nil {
log.Panic(nil, map[string]interface{}{
"err": err,
}, "failed to create token manager")
}
// Middleware that extracts and stores the token in the context
jwtMiddlewareTokenContext := goamiddleware.TokenContext(tokenManager, app.NewJWTSecurity())
service.Use(jwtMiddlewareTokenContext)
service.Use(authsupport.InjectTokenManager(tokenManager))
service.Use(log.LogRequest(config.IsDeveloperModeEnabled()))
app.UseJWTMiddleware(service, jwt.New(tokenManager.PublicKeys(), nil, app.NewJWTSecurity()))
// Mount the '/status' controller
dbChecker := controller.NewGormDBChecker(db)
statusCtrl := controller.NewStatusController(service, dbChecker, config)
app.MountStatusController(service, statusCtrl)
// Mount the '/search' controller
searchCtrl := controller.NewSearchController(service, config, appDB)
app.MountSearchController(service, searchCtrl)
// Mount the '/tenants/update' controller
tenantUpdateCtrl := controller.NewTenantUpdateController(service, config, appDB)
app.MountTenantUpdateController(service, tenantUpdateCtrl)
// Mount the '/auditlogs/users' controller
auditLogsCtrl := controller.NewAuditLogsController(service, config, appDB)
app.MountAuditLogController(service, auditLogsCtrl)
log.Logger().Infoln("Git Commit SHA: ", app.Commit)
log.Logger().Infoln("UTC Build Time: ", app.BuildTime)
log.Logger().Infoln("UTC Start Time: ", app.StartTime)
log.Logger().Infoln("GOMAXPROCS: ", runtime.GOMAXPROCS(-1))
log.Logger().Infoln("NumCPU: ", runtime.NumCPU())
printUserInfo()
// Nothing to here except exit.
if printConfig {
os.Exit(0)
}
http.Handle("/api/", service.Mux)
http.Handle("/favicon.ico", http.NotFoundHandler())
if config.GetDiagnoseHTTPAddress() != "" {
log.Logger().Infoln("Diagnose: ", config.GetDiagnoseHTTPAddress())
// Start diagnostic http
if err := agent.Listen(agent.Options{Addr: config.GetDiagnoseHTTPAddress(), ConfigDir: "/tmp/gops/"}); err != nil {
log.Error(context.TODO(), map[string]interface{}{
"addr": config.GetDiagnoseHTTPAddress(),
"err": err,
}, "unable to connect to diagnose server")
}
}
// // Start/mount metrics http
if config.GetHTTPAddress() == config.GetMetricsHTTPAddress() {
http.Handle("/metrics", promhttp.Handler())
} else {
go func(metricAddress string) {
mx := http.NewServeMux()
mx.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(metricAddress, mx); err != nil {
log.Error(nil, map[string]interface{}{
"addr": metricAddress,
"err": err,
}, "unable to connect to metrics server")
service.LogError("startup", "err", err)
}
}(config.GetMetricsHTTPAddress())
}
// Start http
if err := http.ListenAndServe(config.GetHTTPAddress(), nil); err != nil {
log.Error(nil, map[string]interface{}{
"addr": config.GetHTTPAddress(),
"err": err,
}, "unable to connect to server")
service.LogError("startup", "err", err)
}
}
func printUserInfo() {
u, err := user.Current()
if err != nil {
log.Warn(nil, map[string]interface{}{
"err": err,
}, "failed to get current user")
} else {
log.Info(nil, map[string]interface{}{
"username": u.Username,
"uuid": u.Uid,
}, "Running as user name '%s' with UID %s.", u.Username, u.Uid)
g, err := user.LookupGroupId(u.Gid)
if err != nil {
log.Warn(nil, map[string]interface{}{
"err": err,
}, "failed to lookup group")
} else {
log.Info(nil, map[string]interface{}{
"groupname": g.Name,
"gid": g.Gid,
}, "Running as as group '%s' with GID %s.", g.Name, g.Gid)
}
}
}