-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
88 lines (72 loc) · 2.88 KB
/
server.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
package router
import (
"fmt"
"net/http"
"os"
"time"
commongin "github.com/equinor/radix-common/pkg/gin"
"github.com/equinor/radix-vulnerability-scanner-api/api"
"github.com/equinor/radix-vulnerability-scanner-api/swaggerui"
_ "github.com/equinor/radix-vulnerability-scanner-api/swaggerui" // statik files
"github.com/equinor/radix-vulnerability-scanner-api/utils/auth"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
const (
radixDNSZoneEnvironmentVariable = "RADIX_DNS_ZONE"
swaggerUIPath = "/swaggerui"
)
// NewServer Constructor function
func NewServer(clusterName, apiRootPath string, authProvider auth.AuthProvider, controllers ...api.Controller) http.Handler {
rootRouter := gin.New()
rootRouter.RemoveExtraSlash = true
rootRouter.Use(gin.Recovery())
rootRouter.Use(commongin.SetZerologLogger(commongin.ZerologLoggerWithRequestId))
rootRouter.Use(commongin.ZerologRequestLogger())
rootRouter.Use(corsMiddleware(clusterName))
initializeSwaggerUI(rootRouter)
v1Router := rootRouter.Group(apiRootPath)
{
v1Router.Use(NewAuthenticationMiddleware(authProvider))
initializeAPIServer(v1Router, controllers)
}
rootRouter.GET("/health", func(ctx *gin.Context) { ctx.Status(http.StatusOK) })
return rootRouter
}
func corsMiddleware(clusterName string) gin.HandlerFunc {
radixDNSZone := os.Getenv(radixDNSZoneEnvironmentVariable)
return cors.New(cors.Config{
AllowOrigins: []string{
fmt.Sprintf("https://console.%s", radixDNSZone),
getHostName("web", "radix-web-console-qa", clusterName, radixDNSZone),
getHostName("web", "radix-web-console-prod", clusterName, radixDNSZone),
// Due to active-cluster
getActiveClusterHostName("web", "radix-web-console-qa", radixDNSZone),
getActiveClusterHostName("web", "radix-web-console-prod", radixDNSZone),
},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
AllowHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
AllowMethods: []string{"GET", "PUT", "POST", "OPTIONS", "DELETE", "PATCH", "HEAD"},
})
}
func getActiveClusterHostName(componentName, namespace, radixDNSZone string) string {
return fmt.Sprintf("https://%s-%s.%s", componentName, namespace, radixDNSZone)
}
func getHostName(componentName, namespace, clustername, radixDNSZone string) string {
return fmt.Sprintf("https://%s-%s.%s.%s", componentName, namespace, clustername, radixDNSZone)
}
func initializeSwaggerUI(engine *gin.Engine) {
swaggerFsHandler := http.FS(swaggerui.FS())
engine.StaticFS(swaggerUIPath, swaggerFsHandler)
}
func initializeAPIServer(router gin.IRoutes, controllers []api.Controller) {
for _, controller := range controllers {
for _, route := range controller.GetRoutes() {
addHandlerRoute(router, route)
}
}
}
func addHandlerRoute(router gin.IRoutes, route api.Route) {
router.Handle(route.Method, route.Path, route.Handler)
}