/
main.go
123 lines (107 loc) · 4.16 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
package main
import (
"context"
"fmt"
"net"
"github.com/byuoitav/auth/wso2"
"github.com/byuoitav/smee/internal/app/alertmanager/handlers"
"github.com/byuoitav/smee/internal/app/commandcli"
"github.com/byuoitav/smee/internal/pkg/couch"
"github.com/byuoitav/smee/internal/pkg/postgres"
"github.com/byuoitav/smee/internal/smee"
"github.com/byuoitav/smee/opa"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
type Deps struct {
// set by command line flags
Port int
HubURL string
LogLevel string
ClientID string
ClientSecret string
GatewayURL string
RedirectURL string
OPAURL string
OPAToken string
RedisURL string
PostgresURL string
DisableAlertManager bool
CommandServerAddress string
CommandToken string
CouchURL string
CouchUsername string
CouchPassword string
WebRoot string
// created by functions
log *zap.Logger
wso2 *wso2.Client
opa *opa.Client
disableAuth bool
postgres *postgres.Client
issueStore smee.IssueStore
incidentStore smee.IncidentStore
maintenanceStore smee.MaintenanceStore
issuetypeStore smee.IssueTypeStore
alertManager smee.AlertManager
eventStreamer smee.EventStreamer
deviceStateStore smee.DeviceStateStore
commandClient *commandcli.Client
couchManager *couch.CouchManager
httpServer *gin.Engine
handlers *handlers.Handlers
httpListener net.Listener
}
func main() {
var deps Deps
pflag.IntVarP(&deps.Port, "port", "P", 8080, "port to run the server on")
pflag.StringVarP(&deps.LogLevel, "log-level", "L", "", "level to log at. refer to https://godoc.org/go.uber.org/zap/zapcore#Level for options")
pflag.StringVar(&deps.HubURL, "hub-url", "", "url of the event hub")
pflag.StringVar(&deps.ClientID, "client-id", "", "wso2 key")
pflag.StringVar(&deps.ClientSecret, "client-secret", "", "wso2 secret")
pflag.StringVar(&deps.GatewayURL, "gateway-url", "https://api.byu.edu", "wso2 gateway address")
pflag.StringVar(&deps.RedirectURL, "redirect-url", "https://localhost:8080", "wso2 redirect address")
pflag.StringVar(&deps.OPAURL, "opa-url", "", "the URL for the OPA server to be used for authz")
pflag.StringVar(&deps.OPAToken, "opa-token", "", "the token to use for calls to OPA")
pflag.BoolVar(&deps.disableAuth, "disable-auth", false, "disables authz/n checks")
pflag.StringVar(&deps.RedisURL, "redis-url", "", "redis url")
pflag.StringVar(&deps.PostgresURL, "postgres-url", "", "postgres url")
pflag.BoolVar(&deps.DisableAlertManager, "disable-alert-manager", false, "Disables the Alert Management portion of smee")
pflag.StringVar(&deps.CommandServerAddress, "command-server", "", "url for the av-cli command server")
pflag.StringVar(&deps.CommandToken, "command-token", "", "the token to use for calls to the av-cli command server")
pflag.StringVar(&deps.CouchURL, "couch-address", "", "")
pflag.StringVar(&deps.CouchUsername, "couch-username", "", "")
pflag.StringVar(&deps.CouchPassword, "couch-password", "", "")
pflag.StringVar(&deps.WebRoot, "web-root", "/website", "The location on the filesystem of the root of the website files")
pflag.Parse()
deps.build()
defer deps.cleanup()
g, ctx := errgroup.WithContext(context.Background())
// Skip turning on the alert manager if we have disabled it
if !deps.DisableAlertManager {
g.Go(func() error {
if err := deps.alertManager.Run(ctx); err != nil {
return fmt.Errorf("unable to run alert manager: %w", err)
}
return fmt.Errorf("alert manager stopped running")
})
}
g.Go(func() error {
if err := deps.httpServer.RunListener(deps.httpListener); err != nil {
return fmt.Errorf("unable to run http server: %w", err)
}
return fmt.Errorf("http server stopped running")
})
g.Go(func() error {
<-ctx.Done()
if err := deps.httpListener.Close(); err != nil {
return fmt.Errorf("unable to close http listener: %w", err)
}
return fmt.Errorf("closed http listener: %w", ctx.Err())
})
if err := g.Wait(); err != nil {
deps.log.Fatal(err.Error())
}
}