-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
74 lines (68 loc) · 1.64 KB
/
gin.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
package restful
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/justdomepaul/toolbox/config"
"github.com/justdomepaul/toolbox/errorhandler"
"html/template"
"net/http"
)
func NewGin(
option config.Set,
render *Render,
guarder *JWTGuarder,
) (*gin.Engine, error) {
if option.Server.ReleaseMode {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
}
srv := gin.New()
if option.Server.CustomizedRender {
tmpl, err := template.New("tmpl").Parse(ErrorPageTmpl)
if err != nil {
return nil, err
}
if err := render.Add(ErrPageKey, tmpl); err != nil {
return nil, err
}
srv.HTMLRender = render
}
srv.MaxMultipartMemory = option.Server.MaxMultipartMemoryMB << 20
cf := cors.DefaultConfig()
cf.AllowMethods = []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions}
cf.AllowHeaders = []string{
"Origin",
"Upgrade",
"Content-Length",
"Content-Type",
"Authorization",
"Connection",
"Accept-Encoding",
"Accept-Language",
"Host",
"X-Google-*",
"X-AppEngine-*",
"X-CloudScheduler",
"X-CloudScheduler-JobName",
"X-CloudScheduler-ScheduleTime",
"Sec-WebSocket-Key",
"Sec-WebSocket-Version",
"Sec-WebSocket-Protocol",
}
if option.Server.AllowAllOrigins {
cf.AllowAllOrigins = true
} else {
cf.AllowOrigins = option.Server.AllowOrigins
}
fns := []gin.HandlerFunc{
cors.New(cf),
gin.Logger(),
errorhandler.GinPanicErrorHandler(option.Core.SystemName, option.Server.PrefixMessage),
}
if option.Server.JWTGuard {
fns = append(fns, guarder.JWTGuarder(option.Server.AllowedPaths...))
}
srv.Use(fns...)
return srv, nil
}