-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
139 lines (125 loc) · 3.89 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
package main
import (
"config-service/routes/login"
"config-service/routes/prob"
"config-service/routes/v1/admin"
"config-service/routes/v1/attack_chains"
"config-service/routes/v1/cloud_credentials"
"config-service/routes/v1/cluster"
"config-service/routes/v1/collaboration_config"
"config-service/routes/v1/customer"
"config-service/routes/v1/customer_config"
"config-service/routes/v1/framework"
"config-service/routes/v1/integration_reference"
"config-service/routes/v1/posture_exception"
"config-service/routes/v1/registry"
"config-service/routes/v1/registry_cron_job"
"config-service/routes/v1/repository"
"config-service/routes/v1/runtime_alerts"
"config-service/routes/v1/runtime_incident_policy"
"config-service/routes/v1/runtime_incidents"
"config-service/routes/v1/users_notifications_vulnerabilities"
"config-service/routes/v1/workflows"
"config-service/routes/v1/users_notifications_cache"
"config-service/routes/v1/vulnerability_exception"
"config-service/utils"
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.uber.org/zap"
)
func main() {
//initialize and deffer shutdown
defer initialize()()
//Create routes
router := setupRouter()
//Start server (blocking)
startServer(router)
}
func setupRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.ContextWithFallback = true
//readiness and liveness probes
prob.AddRoutes(router)
//general middlewares
//open telemetry middleware
router.Use(otelgin.Middleware("config-service"))
//response trace headers middleware
router.Use(traceAttributesNHeader)
//set a logger per request context with common fields
router.Use(requestLoggerWithFields)
//log request summary after served
router.Use(requestSummary())
//recover from panics with 500 response
router.Use(ginzap.RecoveryWithZap(zapLogger, true))
//Public routes
//login routes
login.AddRoutes(router)
//public (not authenticate routes
customer.AddPublicRoutes(router)
//auth middleware
router.Use(authenticate)
//add protected routes
admin.AddRoutes(router)
cluster.AddRoutes(router)
posture_exception.AddRoutes(router)
vulnerability_exception.AddRoutes(router)
customer_config.AddRoutes(router)
customer.AddRoutes(router)
framework.AddRoutes(router)
repository.AddRoutes(router)
registry_cron_job.AddRoutes(router)
collaboration_config.AddRoutes(router)
users_notifications_cache.AddRoutes(router)
users_notifications_vulnerabilities.AddRoutes(router)
attack_chains.AddRoutes(router)
runtime_incidents.AddRoutes(router)
runtime_alerts.AddRoutes(router)
runtime_incident_policy.AddRoutes(router)
integration_reference.AddRoutes(router)
cloud_credentials.AddRoutes(router)
workflows.AddRoutes(router)
registry.AddRoutes(router)
return router
}
func startServer(handler http.Handler) {
port := utils.GetConfig().Port
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
srv := &http.Server{
Addr: ":" + port,
Handler: handler,
}
zapLogger.Info("Starting server on port " + port)
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
zapLogger.Info("Shutting down server...")
// let the server have 5 secs to shutdown gracefully
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
zapLogger.Error("Server forced to shutdown:", zap.Error(err))
return
}
zapLogger.Info("Server exiting")
}