-
Notifications
You must be signed in to change notification settings - Fork 1
/
ginserver.go
121 lines (94 loc) · 3.04 KB
/
ginserver.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ginserver
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
)
type (
GinServer struct {
cfg *Config
engine *gin.Engine
server *http.Server
}
)
var (
default404Body = []byte(http.StatusText(http.StatusNotFound))
default405Body = []byte(http.StatusText(http.StatusMethodNotAllowed))
)
func newGinServer(cfg *Config) *GinServer {
gin.SetMode(gin.ReleaseMode) // init mode is release.
engine := gin.New()
server := &http.Server{
Addr: cfg.Addr,
Handler: engine,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.IdleTimeout,
}
gin.SetMode(cfg.Mode)
if cfg.BasicAuth != nil && len(cfg.BasicAuth) != 0 {
engine.Use(gin.BasicAuth(cfg.BasicAuth))
}
engine.NoRoute(func(c *gin.Context) {
c.Data(http.StatusNotFound, "text/plain", default404Body)
})
engine.NoMethod(func(c *gin.Context) {
c.Data(http.StatusMethodNotAllowed, "text/plain", default405Body)
})
return &GinServer{
cfg: cfg,
engine: engine,
server: server,
}
}
func (server *GinServer) Name() string {
return "gin-server"
}
func (server *GinServer) Serve(ctx context.Context) error {
return server.Run()
}
func (server *GinServer) Shutdown(ctx context.Context) error {
return server.server.Shutdown(ctx)
}
func (server *GinServer) Run() error {
if err := server.server.ListenAndServe(); err != http.ErrServerClosed {
return err
}
return nil
}
func (server *GinServer) Use(middleware ...gin.HandlerFunc) gin.IRoutes {
return server.engine.Use(middleware...)
}
func (server *GinServer) Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.Any(relativePath, handlers...)
}
func (server *GinServer) DELETE(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.DELETE(relativePath, handlers...)
}
func (server *GinServer) GET(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.GET(relativePath, handlers...)
}
func (server *GinServer) HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.HEAD(relativePath, handlers...)
}
func (server *GinServer) OPTIONS(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.OPTIONS(relativePath, handlers...)
}
func (server *GinServer) PATCH(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.PATCH(relativePath, handlers...)
}
func (server *GinServer) POST(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.POST(relativePath, handlers...)
}
func (server *GinServer) PUT(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {
return server.engine.PUT(relativePath, handlers...)
}
func (server *GinServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
server.engine.ServeHTTP(w, req)
}
func (server *GinServer) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {
return server.engine.Group(relativePath, handlers...)
}
func (server *GinServer) RoutesInfo() gin.RoutesInfo {
return server.engine.Routes()
}