-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
107 lines (91 loc) · 3.44 KB
/
Copy pathmain.go
File metadata and controls
107 lines (91 loc) · 3.44 KB
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
// Copyright (c) 2004-present Facebook All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/alecthomas/kong"
"github.com/facebookincubator/symphony/pkg/server/metrics"
"github.com/net-auto/resourceManager/ent/schema"
logger "github.com/net-auto/resourceManager/logging"
stdlog "log"
"net/url"
"os"
"syscall"
"github.com/facebookincubator/symphony/pkg/ctxgroup"
"github.com/facebookincubator/symphony/pkg/ctxutil"
"github.com/facebookincubator/symphony/pkg/server"
"github.com/facebookincubator/symphony/pkg/telemetry"
"github.com/facebookincubator/symphony/pkg/viewer"
_ "github.com/net-auto/resourceManager/ent/runtime"
"go.uber.org/zap"
)
type cliFlags struct {
ConfigFile kong.ConfigFlag `type:"existingfile" placeholder:"PATH" help:"Configuration file path."`
ListenAddress string `name:"web.listen-address" default:":http" help:"Web address to listen on."`
MetricsAddress metrics.Addr `name:"metrics.listen-address" default:":9464" help:"Metrics address to listen on."`
DatabaseURL *url.URL `name:"db.url" env:"DB_URL" required:"" placeholder:"URL" help:"Database URL."`
TelemetryConfig telemetry.Config `embed:""`
TenancyConfig viewer.Config `embed:""`
RbacConfig schema.RbacConfig `embed:""`
LogPath string `name:"logPath" env:"RM_LOG_PATH" default:"./rm.log" help:"Path to logfile." type:"path"`
LogLevel string `name:"loglevel" env:"RM_LOG_LEVEL" default:"info" help:"Logging level - fatal, error, warning, info, debug or trace." type:"string"`
LogWithColors bool `name:"logWithColors" default:"false" help:"Force colors in log." type:"bool"`
}
func main() {
var cf cliFlags
kong.Parse(&cf, cf.TelemetryConfig)
ctx := ctxutil.WithSignal(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
app, cleanup, err := newApplication(ctx, &cf)
if err != nil {
stdlog.Fatal(err)
}
defer cleanup()
defer logger.Close()
logger.Info(ctx, "initializing RBAC with %+v", zap.Reflect("rbacConfig", cf.RbacConfig))
initializeRbacSettings(cf)
logger.Info(ctx, "starting application %+v", zap.String("httpEndpoint", cf.ListenAddress))
err = app.run(ctx)
logger.Info(ctx, "terminating application %+v", zap.Error(err))
}
// initializeRbacSettings configures which roles and groups grant users admin access ... globally
func initializeRbacSettings(cf cliFlags) {
schema.InitializeAdminRolesFromSlice(cf.RbacConfig.AdminRoles)
schema.InitializeAdminGroupsFromSlice(cf.RbacConfig.AdminGroups)
}
type application struct {
*zap.Logger
server *server.Server
addr string
metrics *metrics.Metrics
metricsAddr metrics.Addr
}
func (app *application) run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
g := ctxgroup.WithContext(ctx)
g.Go(func(context.Context) error {
err := app.server.ListenAndServe(app.addr)
app.Debug("http server terminated", zap.Error(err))
return err
})
g.Go(func(ctx context.Context) error {
return app.metrics.Serve(ctx, app.metricsAddr)
})
g.Go(func(ctx context.Context) error {
defer cancel()
<-ctx.Done()
return nil
})
<-ctx.Done()
g.Go(func(context.Context) error {
app.Debug("start http server termination")
err := app.server.Shutdown(context.Background())
app.Debug("end http server termination", zap.Error(err))
return err
})
return g.Wait()
}