-
Notifications
You must be signed in to change notification settings - Fork 0
/
postapi_middleware.go
89 lines (68 loc) · 2.1 KB
/
postapi_middleware.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 postapi
import (
"runtime"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/gogap/config"
"github.com/sirupsen/logrus"
)
func (p *PostAPI) loadCORS(router *gin.Engine, conf config.Configuration) {
var corsConf cors.Config
if conf.IsEmpty() {
corsConf = cors.DefaultConfig()
corsConf.AllowMethods = []string{"POST"}
corsConf.AllowOrigins = []string{"*"}
corsConf.AllowOriginFunc = func(origin string) bool {
return true
}
logrus.WithField("component", "postapi").WithField("alias", p.alias).Infoln("using default cors config")
} else {
corsConf = cors.Config{
AllowOrigins: conf.GetStringList("allow-origins"),
AllowMethods: conf.GetStringList("allow-methods"),
AllowHeaders: conf.GetStringList("allow-headers"),
ExposeHeaders: conf.GetStringList("expose-headers"),
AllowCredentials: conf.GetBoolean("allow-credentials", false),
MaxAge: conf.GetTimeDuration("max-age", time.Hour*12),
}
corsConf.AllowOriginFunc = wildcardMatchFunc(corsConf.AllowOrigins)
}
corsConf.AllowHeaders = append(corsConf.AllowHeaders, "X-Api", "X-Api-Batch", "X-Api-Timeout")
router.Use(cors.New(corsConf))
}
func (p *PostAPI) loadPprof(router *gin.Engine, conf config.Configuration) {
if conf == nil {
return
}
if !conf.GetBoolean("enabled", false) {
return
}
logrus.WithField("component", "postapi").WithField("alias", p.alias).Infoln("http.pprof enabled")
pprof.Register(router)
runtime.SetBlockProfileRate(int(conf.GetInt32("block-profile-rate", 0)))
}
func (p *PostAPI) loadGZip(router *gin.Engine, conf config.Configuration) {
if conf == nil {
return
}
if !conf.GetBoolean("enabled", true) {
return
}
logrus.WithField("component", "postapi").WithField("alias", p.alias).Infoln("gzip enabled")
compressLevel := conf.GetString("level", "default")
level := gzip.DefaultCompression
switch compressLevel {
case "best-compression":
{
level = gzip.BestCompression
}
case "best-speed":
{
level = gzip.BestSpeed
}
}
router.Use(gzip.Gzip(level))
}