-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
53 lines (41 loc) · 1002 Bytes
/
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
package server
import (
"github.com/gin-gonic/gin"
"github.com/nanhao/highgin/handler"
"github.com/nanhao/highgin/env"
"log"
)
type RouteSetterType func (engine *gin.Engine, w HdlWrapperType)
type AppServer struct {
engine *gin.Engine
routeSetter RouteSetterType
errHandler handler.ErrHandlerType
}
type HdlWrapperType func (handler.Hdl) gin.HandlerFunc
func New(routeSetter RouteSetterType) *AppServer {
env.Load()
engine := gin.Default()
server := &AppServer {
engine: engine,
routeSetter: routeSetter,
errHandler: handler.ErrHandler,
}
routeSetter(engine, server.W)
return server
}
func (ins *AppServer) SetErrHandler(errHandler handler.ErrHandlerType) {
ins.errHandler = errHandler
}
func (ins *AppServer) Run() {
listen, err := env.GetStrict("LISTEN")
if err != nil {
log.Fatal(err)
}
ins.engine.Run(listen)
}
func (ins *AppServer) W(hdl handler.Hdl) gin.HandlerFunc {
return func (c *gin.Context) {
ctx := handler.NewCtx(c, ins.errHandler)
hdl(ctx)
}
}