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
78 lines (63 loc) · 2.41 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
package main
import (
"net/http"
"github.com/fabric8-services/build-tool-detector/app"
"github.com/fabric8-services/build-tool-detector/config"
"github.com/fabric8-services/build-tool-detector/controllers"
"github.com/fabric8-services/build-tool-detector/log"
"github.com/fabric8-services/fabric8-common/goamiddleware"
"github.com/fabric8-services/fabric8-common/token"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
"github.com/goadesign/goa/middleware/security/jwt"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
startup = "startup"
errorz = "err"
buildToolDetector = "build-tool-detector"
)
func main() {
// Get a new configuration.
configuration := config.New()
service := goa.New(buildToolDetector)
// Mount middleware.
service.Use(middleware.RequestID())
service.Use(middleware.LogRequest(true))
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover())
tokenManager, err := token.NewManager(configuration)
if err != nil {
log.Logger().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(token.InjectTokenManager(tokenManager))
app.UseJWTMiddleware(service, jwt.New(tokenManager.PublicKeys(), nil, app.NewJWTSecurity()))
// Mount "build-tool-detector" controller.
c := controllers.NewBuildToolDetectorController(service, *configuration)
app.MountBuildToolDetectorController(service, c)
cs := controllers.NewSwaggerController(service)
app.MountSwaggerController(service, cs)
app.MountStatusController(service, controllers.NewStatusController(service))
// Start/mount metrics http.
if configuration.GetMetricsPort() == configuration.GetPort() {
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 {
service.LogError("startup", "err", err)
}
// If no port defined in config file, pick one available.
}(":" + configuration.GetMetricsPort())
}
// Start service.
if err := service.ListenAndServe(":" + configuration.GetPort()); err != nil {
service.LogError(startup, errorz, err)
}
}