-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrouter.go
More file actions
93 lines (77 loc) · 2.89 KB
/
router.go
File metadata and controls
93 lines (77 loc) · 2.89 KB
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
// Package httpapi implements routing paths. Each services in own file.
package httpapi
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/prometheus/client_golang/prometheus/promhttp"
ginprometheus "github.com/zsais/go-gin-prometheus"
"github.com/device-management-toolkit/console/config"
v1 "github.com/device-management-toolkit/console/internal/controller/httpapi/v1"
v2 "github.com/device-management-toolkit/console/internal/controller/httpapi/v2"
openapi "github.com/device-management-toolkit/console/internal/controller/openapi"
dto "github.com/device-management-toolkit/console/internal/entity/dto/v1"
"github.com/device-management-toolkit/console/internal/usecase"
"github.com/device-management-toolkit/console/pkg/logger"
)
// NewRouter -.
func NewRouter(handler *gin.Engine, l logger.Interface, t usecase.Usecases, cfg *config.Config) {
// Options
handler.Use(gin.Logger())
handler.Use(gin.Recovery())
// Add Prometheus middleware for automatic HTTP metrics
// Don't automatically register /metrics endpoint - we have our own
p := ginprometheus.NewPrometheus("gin")
p.MetricsPath = ""
// Use middleware function directly without calling Use() which would register conflicting routes
handler.Use(p.HandlerFunc())
// Initialize Fuego adapter
fuegoAdapter := openapi.NewFuegoAdapter(t, l)
fuegoAdapter.RegisterRoutes()
fuegoAdapter.AddToGinRouter(handler)
// Public routes
login := v1.NewLoginRoute(cfg)
handler.POST("/api/v1/authorize", login.Login)
// Setup UI routes (no-op in noui builds)
setupUIRoutes(handler, l, cfg)
// K8s probe
handler.GET("/healthz", func(c *gin.Context) { c.Status(http.StatusOK) })
// Prometheus metrics
handler.GET("/metrics", gin.WrapH(promhttp.Handler()))
// version info
vr := v1.NewVersionRoute(cfg)
handler.GET("/version", vr.LatestReleaseHandler)
// Protected routes using JWT middleware
var protected *gin.RouterGroup
if cfg.Disabled {
protected = handler.Group("/api")
} else {
protected = handler.Group("/api", login.JWTAuthMiddleware())
}
// Register custom validators once
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
if err := v.RegisterValidation("alphanumhyphenunderscore", dto.ValidateAlphaNumHyphenUnderscore); err != nil {
l.Error("failed to register custom validation: " + err.Error())
}
}
// Routers
h2 := protected.Group("/v1")
{
v1.NewDeviceRoutes(h2, t.Devices, l)
v1.NewAmtRoutes(h2, t.Devices, t.AMTExplorer, t.Exporter, l)
v1.NewCIRACertRoutes(h2, l)
}
h := protected.Group("/v1/admin")
{
v1.NewDomainRoutes(h, t.Domains, l)
v1.NewCIRAConfigRoutes(h, t.CIRAConfigs, l)
v1.NewProfileRoutes(h, t.Profiles, l)
v1.NewWirelessConfigRoutes(h, t.WirelessProfiles, l)
v1.NewIEEE8021xConfigRoutes(h, t.IEEE8021xProfiles, l)
}
h3 := protected.Group("/v2")
{
v2.NewAmtRoutes(h3, t.Devices, l)
}
}