-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.51 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
package main
import (
"flag"
"time"
"github.com/gin-gonic/gin"
"go.b8s.dev/rollingpin/config"
"go.b8s.dev/rollingpin/kube"
"go.b8s.dev/rollingpin/providers/direct"
"go.b8s.dev/rollingpin/providers/harbor"
"go.uber.org/zap"
)
var configPath = flag.String("config", "config.yaml", "Path to the config file.")
func main() {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
conf, err := config.LoadConfig(*configPath)
if err != nil {
panic(err)
}
kube, err := kube.New()
if err != nil {
panic(err)
}
r := buildRouter(conf, logger, kube)
logger.Info("Server started on :8080.")
r.Run(":8080")
}
func requestLogger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
requestStart := time.Now()
c.Next()
logger.Info("Request Processed",
zap.String("method", c.Request.Method),
zap.Int("status_code", c.Writer.Status()),
zap.String("path", c.Request.URL.Path),
zap.Duration("latency", time.Since(requestStart)),
)
}
}
func buildRouter(conf *config.Config, logger *zap.Logger, client kube.IClient) *gin.Engine {
r := gin.New()
r.SetTrustedProxies(nil)
r.Use(gin.Recovery(), requestLogger(logger))
if config.ProviderEnabled(conf, "harbor") {
harborRouter := &harbor.Router{Config: conf, Logger: logger, Client: client}
harborRouter.Mount(r.Group("/webhooks/harbor"))
}
if config.ProviderEnabled(conf, "direct") {
directRouter := &direct.Router{Config: conf, Logger: logger, Client: client}
directRouter.Mount(r.Group("/webhooks/direct"))
}
return r
}