-
Notifications
You must be signed in to change notification settings - Fork 41
/
server.go
45 lines (36 loc) · 1.01 KB
/
server.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
package server
import (
"net/http"
"github.com/bynil/sov2ex/pkg/config"
"github.com/bynil/sov2ex/pkg/log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type GinRecoverErrorWriter struct{}
func (*GinRecoverErrorWriter) Write(p []byte) (n int, err error) {
log.Error(string(p))
return len(p), nil
}
func SetupEngine() *gin.Engine {
engine := gin.New()
engine.Use(Ginzap(log.GetLogger(), false))
engine.Use(gin.RecoveryWithWriter(&GinRecoverErrorWriter{}))
if config.C.EnableCORS {
corsConfig := cors.DefaultConfig()
corsConfig.AllowCredentials = true
corsConfig.AllowHeaders = append(corsConfig.AllowHeaders, "Cache-Control", "Pragma", "Authorization")
corsConfig.AllowOriginFunc = func(origin string) bool {
return true
}
engine.Use(cors.New(corsConfig))
}
InitCollection()
registerRouters(engine)
return engine
}
func registerRouters(engine *gin.Engine) {
engine.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
engine.GET("/api/search", searchHandler)
}