/
server.go
57 lines (50 loc) · 1.14 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
46
47
48
49
50
51
52
53
54
55
56
57
package rest
import (
"context"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
)
type (
httpRegisterFunc func(*gin.Engine)
)
// HTTPConf config define
type HttpConf struct {
Listen string
Actived bool
PProf bool
}
// HTTPServer ...
type HTTPServer struct {
conf *HttpConf
}
// NewHTTPServer new HTTPServer
func NewHTTPServer(conf *HttpConf) *HTTPServer {
return &HTTPServer{
conf: conf,
}
}
// Start start grpc service
func (s *HTTPServer) Start(ctx *context.Context, register httpRegisterFunc) error {
router := gin.Default()
router.Use(
cors.New(cors.Config{
AllowOrigins: []string{"http://", "https://"},
AllowMethods: []string{"OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"Authorization", "Content-Length", "Content-Type"},
ExposeHeaders: []string{"Authorization"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return true
},
MaxAge: 12 * time.Hour,
}),
)
router.Use(gin.Recovery())
if s.conf.PProf {
pprof.Register(router)
}
register(router)
return router.Run(s.conf.Listen)
}