-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathrouters.go
112 lines (91 loc) · 3.26 KB
/
routers.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
// Package routers is a package dedicated to registering routes, and supports both
// manual route registration and automatic route registration.
package routers
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/go-dev-frame/sponge/pkg/errcode"
"github.com/go-dev-frame/sponge/pkg/gin/handlerfunc"
"github.com/go-dev-frame/sponge/pkg/gin/middleware"
"github.com/go-dev-frame/sponge/pkg/gin/middleware/metrics"
"github.com/go-dev-frame/sponge/pkg/gin/prof"
"github.com/go-dev-frame/sponge/pkg/gin/validator"
"github.com/go-dev-frame/sponge/pkg/logger"
"github.com/go-dev-frame/sponge/docs"
"github.com/go-dev-frame/sponge/internal/config"
)
var (
apiV1RouterFns []func(r *gin.RouterGroup) // group router functions
// if you have other group routes you can define them here
// example:
// apiV2RouterFns []func(r *gin.RouterGroup)
)
// NewRouter create a new router
func NewRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Recovery())
r.Use(middleware.Cors())
if config.Get().HTTP.Timeout > 0 {
// if you need more fine-grained control over your routes, set the timeout in your routes, unsetting the timeout globally here.
r.Use(middleware.Timeout(time.Second * time.Duration(config.Get().HTTP.Timeout)))
}
// request id middleware
r.Use(middleware.RequestID())
// logger middleware, to print simple messages, replace middleware.Logging with middleware.SimpleLog
r.Use(middleware.Logging(
middleware.WithLog(logger.Get()),
middleware.WithRequestIDFromContext(),
middleware.WithIgnoreRoutes("/metrics"), // ignore path
))
// metrics middleware
if config.Get().App.EnableMetrics {
r.Use(metrics.Metrics(r,
//metrics.WithMetricsPath("/metrics"), // default is /metrics
metrics.WithIgnoreStatusCodes(http.StatusNotFound), // ignore 404 status codes
))
}
// limit middleware
if config.Get().App.EnableLimit {
r.Use(middleware.RateLimit())
}
// circuit breaker middleware
if config.Get().App.EnableCircuitBreaker {
r.Use(middleware.CircuitBreaker())
}
// trace middleware
if config.Get().App.EnableTrace {
r.Use(middleware.Tracing(config.Get().App.Name))
}
// profile performance analysis
if config.Get().App.EnableHTTPProfile {
prof.Register(r, prof.WithIOWaitTime())
}
// validator
binding.Validator = validator.Init()
r.GET("/health", handlerfunc.CheckHealth)
r.GET("/ping", handlerfunc.Ping)
r.GET("/codes", handlerfunc.ListCodes)
if config.Get().App.Env != "prod" {
r.GET("/config", gin.WrapF(errcode.ShowConfig([]byte(config.Show()))))
// register swagger routes, generate code via swag init
docs.SwaggerInfo.BasePath = ""
// access path /swagger/index.html
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
// register routers, middleware support
registerRouters(r, "/api/v1", apiV1RouterFns)
// if you have other group routes you can add them here
// example:
// registerRouters(r, "/api/v2", apiV2RouteFns, middleware.Auth())
return r
}
func registerRouters(r *gin.Engine, groupPath string, routerFns []func(*gin.RouterGroup), handlers ...gin.HandlerFunc) {
rg := r.Group(groupPath, handlers...)
for _, fn := range routerFns {
fn(rg)
}
}