-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin.go
89 lines (73 loc) · 2.37 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package sfutils
import (
"fmt"
"net/http"
"os"
"runtime/debug"
"strconv"
"strings"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
var (
systemAccounts = gin.Accounts{
"sflabs_system_account": "7Dnq9cgStvj7M1v8SvOaZZ0O",
}
)
func ginPanicHandler(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case error:
// ugly as fuck
xMsg := x.Error()
if strings.Count(xMsg, "127.0.0.1") == 2 {
if strings.Contains(xMsg, "write: broken pipe") ||
strings.Contains(xMsg, "write: connection reset by peer") {
break
}
}
logrus.Errorln("[PANIC]", fmt.Sprintf("%s: %v\n%s", c.FullPath(), x.Error(), string(debug.Stack())))
default:
logrus.Errorln("[PANIC]", fmt.Sprintf("%s: %v\n%s", c.FullPath(), x, string(debug.Stack())))
}
c.String(http.StatusInternalServerError, "Internal Server Error")
}
}()
c.Next()
}
// NewGin returns new gin.Engine object, ready to use.
// Configuration can be done via next environment variables:
// GIN_MODE - debug/release/test
// GIN_TEMPLATES_MODE - debug/release (if the variable is not set, templates are not loaded)
// GIN_TEMPLATES_PATH - absolute/relative path to html templates
// ENABLE_PPROF - enable/disable golang profiler on path /system/pprof
// ENABLE_PROMETHEUS - enable/disable gin metrics for prometheus on path /system/prometheus
// ENABLE_OPENAPI - enable/disable openapi schema on path /system/openapi
func NewGin() *gin.Engine {
engine := gin.Default()
engine.RedirectTrailingSlash = true
engine.RedirectFixedPath = true
ginTemplatesMode := os.Getenv("GIN_TEMPLATES_MODE")
if len(ginTemplatesMode) > 0 {
var debugTemplates = true
if ginTemplatesMode == "release" {
debugTemplates = false
}
engine.HTMLRender = NewTemplateRender(os.Getenv("GIN_TEMPLATES_PATH"), ".tmpl", debugTemplates)
}
engine.Use(ginPanicHandler)
systemGroup := engine.Group("/system", gin.BasicAuth(systemAccounts))
if enable, _ := strconv.ParseBool(os.Getenv("ENABLE_PPROF")); enable {
pprof.RouteRegister(systemGroup, "pprof")
}
if enable, _ := strconv.ParseBool(os.Getenv("ENABLE_PROMETHEUS")); enable {
p := NewPrometheus("gin")
p.UseRouterGroup(engine, systemGroup)
}
if enable, _ := strconv.ParseBool(os.Getenv("ENABLE_OPENAPI")); enable {
systemGroup.GET("openapi", newOpenAPIHandler())
}
return engine
}