-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
43 lines (34 loc) · 850 Bytes
/
app.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
package gowebtemplate
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/lazyfury/go-web-template/response"
)
// New 初始化
func New() *gin.Engine {
g := gin.New()
// 自定义recover
g.Use(response.Recover)
// HandleMethodNotAllowed
g.HandleMethodNotAllowed = true
g.NoMethod(func(c *gin.Context) {
if c.Request.Method == http.MethodOptions {
c.AbortWithStatus(http.StatusNoContent)
return
}
response.Error(response.NoMethod)
})
// handle 路由不存在
g.NoRoute(func(c *gin.Context) {
if c.Request.URL.Path != "/favicon.ico" {
response.Error(response.NoRoute)
}
})
// 移除多余斜杠 /api//v1/doSomething/ => /api/v1/doSomething
g.RemoveExtraSlash = true
// 重定向请求移除斜杠请求
g.RedirectTrailingSlash = true
// 错误码配置
response.RecoverErrHtml = true
return g
}