This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
174 lines (151 loc) · 6.68 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
package main
import (
"context"
"net/http"
authsupport "github.com/fabric8-services/fabric8-common/auth"
"github.com/fabric8-services/fabric8-common/goamiddleware"
"github.com/fabric8-services/fabric8-common/log"
"github.com/fabric8-services/fabric8-notification/app"
"github.com/fabric8-services/fabric8-notification/auth"
"github.com/fabric8-services/fabric8-notification/collector"
"github.com/fabric8-services/fabric8-notification/configuration"
"github.com/fabric8-services/fabric8-notification/controller"
"github.com/fabric8-services/fabric8-notification/email"
"github.com/fabric8-services/fabric8-notification/jsonapi"
"github.com/fabric8-services/fabric8-notification/template"
"github.com/fabric8-services/fabric8-notification/token"
"github.com/fabric8-services/fabric8-notification/types"
"github.com/fabric8-services/fabric8-notification/validator"
"github.com/fabric8-services/fabric8-notification/wit"
"github.com/goadesign/goa"
goaclient "github.com/goadesign/goa/client"
"github.com/goadesign/goa/middleware"
"github.com/goadesign/goa/middleware/gzip"
goajwt "github.com/goadesign/goa/middleware/security/jwt"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
goalogrus "github.com/goadesign/goa/logging/logrus"
)
func main() {
// Initialized configuration
config, err := configuration.GetData()
if err != nil {
logrus.Panic(nil, map[string]interface{}{
"err": err,
}, "failed to setup the configuration")
}
// Initialized developer mode flag for the logger
log.InitializeLogger(config.IsLogJSON(), config.GetLogLevel())
err = config.Validate()
if err != nil {
log.Panic(nil, map[string]interface{}{
"err": err,
}, "Missing required configuration")
}
witClient, err := wit.NewCachedClient(config.GetWITURL())
if err != nil {
log.Panic(nil, map[string]interface{}{
"url": config.GetWITURL(),
"err": err,
}, "Could not create WIT client")
}
authClient, err := auth.NewCachedClient(config.GetAuthServiceURL())
if err != nil {
log.Panic(nil, map[string]interface{}{
"url": config.GetAuthServiceURL(),
"err": err,
}, "could not create Auth client")
}
// Calling the Auth service to generate a fabric8 service account token.
// This is needed to call /api/users/ID and 'see' the
// email address even if the user has made it 'private'
saService := token.NewFabric8ServiceAccountTokenClient(authClient, config.GetServiceAccountID(), config.GetServiceAccountSecret())
saToken, err := saService.Get(context.Background())
if err != nil {
log.Panic(nil, map[string]interface{}{
"err": err,
}, "could not generate service account token")
}
// update the client with the signer.
authClient.SetJWTSigner(&goaclient.JWTSigner{
TokenSource: &goaclient.StaticTokenSource{
StaticToken: &goaclient.StaticToken{
Value: saToken,
},
},
})
sender, err := email.NewMandrillSender(config.GetMadrillAPIKey())
if err != nil {
log.Panic(nil, map[string]interface{}{
"err": err,
}, "Could not create Madrill Sender")
}
notifier := email.NewAsyncWorkerNotifier(sender, 10)
registry := collector.NewRegistry()
registry.Register(types.WorkitemCreate, collector.ConfiguredVars(config, collector.NewWorkItemResolver(authClient, witClient)), nil)
registry.Register(types.WorkitemUpdate, collector.ConfiguredVars(config, collector.NewWorkItemResolver(authClient, witClient)), nil)
registry.Register(types.CommentCreate, collector.ConfiguredVars(config, collector.NewCommentResolver(authClient, witClient)), nil)
registry.Register(types.CommentUpdate, collector.ConfiguredVars(config, collector.NewCommentResolver(authClient, witClient)), nil)
registry.Register(types.UserEmailUpdate, collector.ConfiguredVars(config, collector.NewUserResolver(authClient)), validator.ValidateUser)
registry.Register(types.UserDeactivation, collector.ConfiguredVars(config, collector.NewUserResolver(authClient)), validator.ValidateUserDeactivation)
registry.Register(types.InvitationTeamNoorg, collector.ConfiguredVars(config, collector.NewUserResolver(authClient)), nil)
registry.Register(types.InvitationSpaceNoorg, collector.ConfiguredVars(config, collector.NewUserResolver(authClient)), nil)
registry.Register(types.AnalyticsNotifyCVE, collector.ConfiguredVars(config, collector.NewCVEResolver(authClient, witClient)), nil)
registry.Register(types.AnalyticsNotifyVersion, collector.ConfiguredVars(config, collector.NewCVEResolver(authClient, witClient)), nil)
templateRegistry := &template.AssetRegistry{}
service := goa.New("notification")
// Mount middleware
service.WithLogger(goalogrus.New(log.Logger()))
service.Use(middleware.RequestID())
service.Use(gzip.Middleware(9))
service.Use(jsonapi.ErrorHandler(service, true))
service.Use(middleware.Recover())
// Setup Security
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(log.LogRequest(config.IsDeveloperModeEnabled()))
app.UseJWTMiddleware(service, goajwt.New(tokenManager.PublicKeys(), nil, app.NewJWTSecurity()))
// Mount "status" controller
statusCtrl := controller.NewStatusController(service)
app.MountStatusController(service, statusCtrl)
// Mount "notification" controller
notifyCtrl := controller.NewNotifyController(service, registry, templateRegistry, notifier)
app.MountNotifyController(service, notifyCtrl)
log.Logger().Infoln("Git Commit SHA: ", controller.Commit)
log.Logger().Infoln("UTC Build Time: ", controller.BuildTime)
log.Logger().Infoln("UTC Start Time: ", controller.StartTime)
log.Logger().Infoln("Dev mode: ", config.IsDeveloperModeEnabled())
http.Handle("/api/", service.Mux)
http.Handle("/favicon.ico", http.NotFoundHandler())
// Start/mount metrics http
if config.GetHTTPAddress() == config.GetMetricsHTTPAddress() {
http.Handle("/metrics", prometheus.Handler())
} else {
go func(metricAddress string) {
mx := http.NewServeMux()
mx.Handle("/metrics", prometheus.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)
}
}