-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
36 lines (34 loc) · 793 Bytes
/
router.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
package ginutils
import (
"github.com/gin-gonic/gin"
"net/http"
)
func SetupRouter(opts ...Option) (*gin.Engine, error) {
//gin.SetMode()
config := &Config{
OpenHealth: true,
}
for _, opt := range opts {
opt(config)
}
router := gin.New()
if config.SkipPaths == nil {
config.SkipPaths = []string{"/health"}
} else {
config.SkipPaths = append(config.SkipPaths, "/health")
}
router.Use(gin.LoggerWithConfig(gin.LoggerConfig{
SkipPaths: config.SkipPaths,
}))
router.Use(gin.Recovery())
if config.OpenHealth {
relativePath := "/health"
if config.ContextPath != "" {
relativePath = config.ContextPath + relativePath
}
router.GET(relativePath, func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "UP", "message": "OK"})
})
}
return router, nil
}