forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux.go
218 lines (184 loc) · 6.44 KB
/
mux.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package fuego
import (
"net/http"
"github.com/getkin/kin-openapi/openapi3"
"github.com/gin-gonic/gin"
)
type GroupOption struct {
HideTag bool
Tag openapi3.Tag
}
func WithoutTag() func(opt *GroupOption) {
return func(opt *GroupOption) {
opt.HideTag = true
}
}
func WithTag(tag openapi3.Tag) func(opt *GroupOption) {
return func(opt *GroupOption) {
opt.Tag = tag
}
}
func WithName(name string) func(opt *GroupOption) {
return func(opt *GroupOption) {
opt.Tag.Name = name
}
}
func WithDescription(desc string) func(opt *GroupOption) {
return func(opt *GroupOption) {
opt.Tag.Description = desc
}
}
// Group allows grouping routes under a common path.
// Middlewares are scoped to the group.
// For example:
//
// s := fuego.NewServer()
// viewsRoutes := fuego.Group(s, "")
// apiRoutes := fuego.Group(s, "/api")
// // Registering a middlewares scoped to /api only
// fuego.Use(apiRoutes, myMiddleware)
// // Registering a route under /api/users
// fuego.Get(apiRoutes, "/users", func(c fuego.ContextNoBody) (ans, error) {
// return ans{Ans: "users"}, nil
// })
// s.Run()
func (group *RouterGroup) Group(path string, opts ...func(*GroupOption)) *RouterGroup {
grpOpt := GroupOption{}
for _, opt := range opts {
opt(&grpOpt)
}
newGroup := group.newRouteGroup(path, grpOpt)
if newGroup.groupTag != "" && !group.server.disableAutoGroupTags && !group.DisableOpenapi {
if !grpOpt.HideTag {
group.server.OpenApiSpec.Tags = append(group.server.OpenApiSpec.Tags, &grpOpt.Tag)
}
}
return newGroup
}
func Group(group *RouterGroup, path string, opts ...func(*GroupOption)) *RouterGroup {
return group.Group(path, opts...)
}
type Route struct {
Operation *openapi3.Operation // GENERATED OpenAPI operation, do not set manually in Register function. You can change it after the route is registered.
All bool // define route to all HTTP methods. If true, ignore Method
Method string // HTTP method (GET, POST, PUT, PATCH, DELETE)
Path string // URL path. Will be prefixed by the base path of the server and the group path if any
mainRouter *Server // ref to the main router, used to register the route in the OpenAPI spec
Group *RouterGroup
Response *Schema
Request *Schema
Errors []openAPIError
}
type Schema struct {
Type any
Description string
ContentType []string
}
// openAPIError describes a response error in the OpenAPI spec.
type openAPIError struct {
Code int
Schema
}
// Capture all methods (GET, POST, PUT, PATCH, DELETE) and register a controller.
func All[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Path: path,
All: true,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func Get[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodGet,
Path: path,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func Post[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPost,
Path: path,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func Delete[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodDelete,
Path: path,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func Put[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPut,
Path: path,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func Patch[T, B any, Contexted ctx[B]](s *RouterGroup, path string, controller func(Contexted) (T, error), middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPatch,
Path: path,
Request: &Schema{Type: new(B)},
Response: &Schema{Type: new(T)},
}, FuegoHandler(s.server, controller), middlewares...)
}
func AllGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
All: true,
Path: path,
}, controller, middlewares...)
}
func GetGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodGet,
Path: path,
}, controller, middlewares...)
}
func PostGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPost,
Path: path,
}, controller, middlewares...)
}
func PutGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPut,
Path: path,
}, controller, middlewares...)
}
func DeleteGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPatch,
Path: path,
}, controller, middlewares...)
}
func PatchGin(s *RouterGroup, path string, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
return Register(s, Route{
Method: http.MethodPatch,
Path: path,
}, controller, middlewares...)
}
func Register(group *RouterGroup, route Route, controller gin.HandlerFunc, middlewares ...gin.HandlerFunc) Route {
route.mainRouter = group.server
route.Group = group
route.Operation = openapi3.NewOperation()
handlers := append([]gin.HandlerFunc{controller}, middlewares...)
if route.All || route.Method == "" {
group.rg.Any(route.Path, handlers...)
} else {
group.rg.Handle(route.Method, route.Path, handlers...)
}
return route
}
func Use(s *RouterGroup, middlewares ...gin.HandlerFunc) {
s.Use(middlewares...)
}
func (group *RouterGroup) Use(middlewares ...gin.HandlerFunc) {
group.rg.Use(middlewares...)
}