-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
mux.go
238 lines (204 loc) · 8.13 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package fuego
import (
"log/slog"
"net/http"
"reflect"
"runtime"
"strings"
"github.com/getkin/kin-openapi/openapi3"
)
// Group allows to group 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(s *Server, path string) *Server {
if path == "/" {
path = ""
} else if path != "" && path[len(path)-1] == '/' {
slog.Warn("Group path should not end with a slash.", "path", path+"/", "new", path)
}
ss := *s
newServer := &ss
newServer.basePath += path
newServer.groupTag = strings.TrimLeft(path, "/")
return newServer
}
type Route[ResponseBody any, RequestBody any] struct {
Operation *openapi3.Operation
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
Handler http.Handler // handler executed for this route
FullName string // namespace and name of the function to execute
}
// Capture all methods (GET, POST, PUT, PATCH, DELETE) and register a controller.
func All[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
func Get[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Method: http.MethodGet,
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
func Post[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Method: http.MethodPost,
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
func Delete[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Method: http.MethodDelete,
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
func Put[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Method: http.MethodPut,
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
func Patch[T, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register(s, Route[T, B]{
Method: http.MethodPatch,
Path: path,
FullName: FuncName(controller),
}, HTTPHandler(s, controller), middlewares...)
}
// Register registers a controller into the default mux and documents it in the OpenAPI spec.
func Register[T, B any](s *Server, route Route[T, B], controller http.Handler, middlewares ...func(http.Handler) http.Handler) Route[T, B] {
route.Handler = controller
fullPath := s.basePath + route.Path
if route.Method != "" {
fullPath = route.Method + " " + fullPath
}
slog.Debug("registering controller " + fullPath)
allMiddlewares := append(s.middlewares, middlewares...)
s.Mux.Handle(fullPath, withMiddlewares(route.Handler, allMiddlewares...))
if s.DisableOpenapi || route.Method == "" {
return route
}
var err error
route.Operation, err = RegisterOpenAPIOperation[T, B](s, route.Method, s.basePath+route.Path)
if err != nil {
slog.Warn("error documenting openapi operation", "error", err)
}
if route.FullName == "" {
route.FullName = route.Path
}
route.Operation.Summary = route.NameFromNamespace(camelToHuman)
route.Operation.Description = "controller: `" + route.FullName + "`\n\n---\n\n"
route.Operation.OperationID = route.Method + " " + s.basePath + route.Path + ":" + route.NameFromNamespace()
return route
}
func UseStd(s *Server, middlewares ...func(http.Handler) http.Handler) {
Use(s, middlewares...)
}
func Use(s *Server, middlewares ...func(http.Handler) http.Handler) {
s.middlewares = append(s.middlewares, middlewares...)
}
// Handle registers a standard http handler into the default mux.
// Use this function if you want to use a standard http handler instead of a fuego controller.
func Handle(s *Server, path string, controller http.Handler, middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Path: path,
FullName: FuncName(controller),
}, controller, middlewares...)
}
func AllStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func GetStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Method: http.MethodGet,
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func PostStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Method: http.MethodPost,
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func DeleteStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Method: http.MethodDelete,
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func PutStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Method: http.MethodPut,
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func PatchStd(s *Server, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
return Register(s, Route[any, any]{
Method: http.MethodPatch,
Path: path,
FullName: FuncName(controller),
}, http.HandlerFunc(controller), middlewares...)
}
func withMiddlewares(controller http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler {
for i := len(middlewares) - 1; i >= 0; i-- {
controller = middlewares[i](controller)
}
return controller
}
// FuncName returns the name of a function and the name with package path
func FuncName(f interface{}) string {
return strings.TrimSuffix(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), "-fm")
}
// NameFromNamespace returns the Route's FullName final string
// delimited by `.`. Essentially getting the name of the function
// and leaving the package path
//
// The output can be further modified with a list of optional
// string manipulation funcs (i.e func(string) string)
func (r Route[T, B]) NameFromNamespace(opts ...func(string) string) string {
ss := strings.Split(r.FullName, ".")
name := ss[len(ss)-1]
for _, o := range opts {
name = o(name)
}
return name
}
// transform camelCase to human readable string
func camelToHuman(s string) string {
result := strings.Builder{}
for i, r := range s {
if 'A' <= r && r <= 'Z' {
if i > 0 {
result.WriteRune(' ')
}
result.WriteRune(r + 'a' - 'A') // 'A' -> 'a
} else {
result.WriteRune(r)
}
}
return result.String()
}