forked from cherry-game/cherry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
67 lines (52 loc) · 1.46 KB
/
controller.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
package cherryGin
import (
cfacade "github.com/cherry-game/cherry/facade"
"github.com/gin-gonic/gin"
)
type GinHandlerFunc func(ctx *Context)
type IController interface {
PreInit(app cfacade.IApplication, engine *gin.Engine)
Init()
Stop()
}
func BindHandlers(handlers []GinHandlerFunc) []gin.HandlerFunc {
var list []gin.HandlerFunc
for _, handler := range handlers {
list = append(list, BindHandler(handler))
}
return list
}
func BindHandler(handler func(ctx *Context)) gin.HandlerFunc {
return func(c *gin.Context) {
context := new(Context)
context.Context = c
handler(context)
}
}
type BaseController struct {
App cfacade.IApplication
Engine *gin.Engine
}
func (b *BaseController) PreInit(app cfacade.IApplication, engine *gin.Engine) {
b.App = app
b.Engine = engine
}
func (b *BaseController) Init() {
}
func (b *BaseController) Stop() {
}
func (b *BaseController) Group(relativePath string, handlers ...GinHandlerFunc) *Group {
group := &Group{
RouterGroup: b.Engine.Group(relativePath, BindHandlers(handlers)...),
}
return group
}
func (b *BaseController) Any(relativePath string, handlers ...GinHandlerFunc) {
b.Engine.Any(relativePath, BindHandlers(handlers)...)
}
func (b *BaseController) GET(relativePath string, handlers ...GinHandlerFunc) {
b.Engine.GET(relativePath, BindHandlers(handlers)...)
}
func (b *BaseController) POST(relativePath string, handlers ...GinHandlerFunc) {
b.Engine.POST(relativePath, BindHandlers(handlers)...)
}