-
Notifications
You must be signed in to change notification settings - Fork 34
/
template.go
134 lines (103 loc) · 2.91 KB
/
template.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
122
123
124
125
126
127
128
129
130
131
132
133
134
package quark
import (
"net/http"
"gorm.io/gorm"
)
// 模版接口
type Templater interface {
// 初始化
Init(ctx *Context) interface{}
// 初始化模板
TemplateInit(ctx *Context) interface{}
// 初始化路由
RouteInit() interface{}
// 自定义路由
Route() interface{}
// 获取路由
GetRouteMapping() []*RouteMapping
// 添加路由
AddRouteMapping(method string, path string, handler func(ctx *Context) error) *Template
// ANY请求
Any(path string, handler func(ctx *Context) error)
// GET请求
GET(path string, handler func(ctx *Context) error)
// HEAD请求
HEAD(path string, handler func(ctx *Context) error)
// OPTIONS请求
OPTIONS(path string, handler func(ctx *Context) error)
// POST请求
POST(path string, handler func(ctx *Context) error)
// PUT请求
PUT(path string, handler func(ctx *Context) error)
// PATCH请求
PATCH(path string, handler func(ctx *Context) error)
// DELETE请求
DELETE(path string, handler func(ctx *Context) error)
}
// 模板
type Template struct {
DB *gorm.DB // DB对象
RouteMapping []*RouteMapping // 路由映射
}
// 获取路由
func (p *Template) GetRouteMapping() []*RouteMapping {
return p.RouteMapping
}
// 自定义路由
func (p *Template) Route() interface{} {
return p
}
// 是否存在路由
func (p *Template) hasRouteMapping(method string, path string) bool {
has := false
for _, v := range p.RouteMapping {
if v.Method == method && v.Path == path {
has = true
}
}
return has
}
// 注册路由
func (p *Template) AddRouteMapping(method string, path string, handler func(ctx *Context) error) *Template {
if !p.hasRouteMapping(method, path) {
getRoute := &RouteMapping{
Method: method,
Path: path,
Handler: handler,
}
p.RouteMapping = append(p.RouteMapping, getRoute)
}
return p
}
// ANY请求
func (p *Template) Any(path string, handler func(ctx *Context) error) {
p.AddRouteMapping("Any", path, handler)
}
// GET请求
func (p *Template) GET(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodGet, path, handler)
}
// HEAD请求
func (p *Template) HEAD(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodHead, path, handler)
}
// OPTIONS请求
func (p *Template) OPTIONS(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodOptions, path, handler)
}
// POST请求
func (p *Template) POST(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodPost, path, handler)
}
// PUT请求
func (p *Template) PUT(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodPut, path, handler)
}
// PATCH请求
func (p *Template) PATCH(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodPatch, path, handler)
}
// DELETE请求
func (p *Template) DELETE(path string, handler func(ctx *Context) error) {
p.AddRouteMapping(http.MethodDelete, path, handler)
}