-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
server.go
425 lines (366 loc) · 13.6 KB
/
server.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package fuego
import (
"html/template"
"io"
"io/fs"
"log/slog"
"net/http"
"os"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-playground/validator/v10"
"github.com/golang-jwt/jwt/v5"
)
type OpenAPIConfig struct {
DisableSwagger bool // If true, the server will not serve the Swagger UI nor the OpenAPI JSON spec
DisableSwaggerUI bool // If true, the server will not serve the Swagger UI
DisableLocalSave bool // If true, the server will not save the OpenAPI JSON spec locally
SwaggerUrl string // URL to serve the swagger UI
UIHandler func(specURL string) http.Handler // Handler to serve the OpenAPI UI from spec URL
JsonUrl string // URL to serve the OpenAPI JSON spec
JsonFilePath string // Local path to save the OpenAPI JSON spec
PrettyFormatJson bool // Pretty prints the OpenAPI spec with proper JSON indentation
}
var defaultOpenAPIConfig = OpenAPIConfig{
SwaggerUrl: "/swagger",
JsonUrl: "/swagger/openapi.json",
JsonFilePath: "doc/openapi.json",
UIHandler: DefaultOpenAPIHandler,
}
type Server struct {
// The underlying HTTP server
*http.Server
// Will be plugged into the Server field.
// Not using directly the Server field so
// [http.ServeMux.Handle] can also be used to register routes.
Mux *http.ServeMux
// Not stored with the other middlewares because it is a special case :
// it applies on routes that are not registered.
// For example, it allows OPTIONS /foo even if it is not declared (only GET /foo is declared).
corsMiddleware func(http.Handler) http.Handler
// routeOptions is used to store the options
// that will be applied of the route.
routeOptions []func(*BaseRoute)
middlewares []func(http.Handler) http.Handler
disableStartupMessages bool
disableAutoGroupTags bool
basePath string // Base path of the group
*Engine
Security Security
autoAuth AutoAuthConfig
fs fs.FS
template *template.Template // TODO: use preparsed templates
// If true, the server will return an error if the request body contains unknown fields. Useful for quick debugging in development.
DisallowUnknownFields bool
maxBodySize int64
// Custom serializer that overrides the default one.
Serialize Sender
// Used to serialize the error response. Defaults to [SendError].
SerializeError ErrorSender
startTime time.Time
OpenAPIConfig OpenAPIConfig
isTLS bool
}
// NewServer creates a new server with the given options.
// For example:
//
// app := fuego.NewServer(
// fuego.WithAddr(":8080"),
// fuego.WithoutLogger(),
// )
//
// Option all begin with `With`.
// Some default options are set in the function body.
func NewServer(options ...func(*Server)) *Server {
s := &Server{
Server: &http.Server{
ReadTimeout: 30 * time.Second,
ReadHeaderTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
},
Mux: http.NewServeMux(),
Engine: NewEngine(),
OpenAPIConfig: defaultOpenAPIConfig,
Security: NewSecurity(),
}
// Default options that can be overridden
defaultOptions := [...]func(*Server){
WithAddr("localhost:9999"),
WithDisallowUnknownFields(true),
WithSerializer(Send),
WithErrorSerializer(SendError),
WithRouteOptions(
OptionAddResponse(http.StatusBadRequest, "Bad Request _(validation or deserialization error)_", Response{Type: HTTPError{}}),
OptionAddResponse(http.StatusInternalServerError, "Internal Server Error _(panics)_", Response{Type: HTTPError{}}),
),
}
options = append(defaultOptions[:], options...)
for _, option := range options {
option(s)
}
s.startTime = time.Now()
if s.autoAuth.Enabled {
Post(s, "/auth/login", s.Security.LoginHandler(s.autoAuth.VerifyUserInfo),
OptionTags("Auth"),
OptionSummary("Login"),
)
PostStd(s, "/auth/logout", s.Security.CookieLogoutHandler,
OptionTags("Auth"),
OptionSummary("Logout"),
)
s.middlewares = []func(http.Handler) http.Handler{
s.Security.TokenToContext(TokenFromCookie, TokenFromHeader),
}
PostStd(s, "/auth/refresh", s.Security.RefreshHandler,
OptionTags("Auth"),
OptionSummary("Refresh"),
)
}
return s
}
// WithTemplateFS sets the filesystem used to load templates.
// To be used with [WithTemplateGlobs] or [WithTemplates].
// For example:
//
// WithTemplateFS(os.DirFS("./templates"))
//
// or with embedded templates:
//
// //go:embed templates
// var templates embed.FS
// ...
// WithTemplateFS(templates)
func WithTemplateFS(fs fs.FS) func(*Server) {
return func(c *Server) { c.fs = fs }
}
// WithCorsMiddleware registers a middleware to handle CORS.
// It is not handled like other middlewares with [Use] because it applies routes that are not registered.
// For example:
//
// import "github.com/rs/cors"
//
// s := fuego.NewServer(
// WithCorsMiddleware(cors.New(cors.Options{
// AllowedOrigins: []string{"*"},
// AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
// AllowedHeaders: []string{"*"},
// AllowCredentials: true,
// }).Handler)
// )
func WithCorsMiddleware(corsMiddleware func(http.Handler) http.Handler) func(*Server) {
return func(c *Server) { c.corsMiddleware = corsMiddleware }
}
// WithGlobalResponseTypes adds default response types to the server.
// For example:
//
// app := fuego.NewServer(
// fuego.WithGlobalResponseTypes(400, "Bad Request _(validation or deserialization error)_", HTTPError{}),
// fuego.WithGlobalResponseTypes(401, "Unauthorized _(authentication error)_", HTTPError{}),
// fuego.WithGlobalResponseTypes(500, "Internal Server Error _(panics)_", HTTPError{}),
// fuego.WithGlobalResponseTypes(204, "No Content", Empty{}),
// )
//
// Deprecated: Please use [OptionAddResponse] with [WithRouteOptions]
func WithGlobalResponseTypes(code int, description string, response Response) func(*Server) {
return func(c *Server) {
WithRouteOptions(
OptionAddResponse(code, description, response),
)(c)
}
}
// WithSecurity configures security schemes in the OpenAPI specification.
// It allows setting up authentication methods like JWT Bearer tokens, API keys, OAuth2, etc.
// For example:
//
// app := fuego.NewServer(
// fuego.WithSecurity(map[string]*openapi3.SecuritySchemeRef{
// "bearerAuth": &openapi3.SecuritySchemeRef{
// Value: openapi3.NewSecurityScheme().
// WithType("http").
// WithScheme("bearer").
// WithBearerFormat("JWT").
// WithDescription("Enter your JWT token in the format: Bearer <token>"),
// },
// }),
// )
func WithSecurity(schemes openapi3.SecuritySchemes) func(*Server) {
return func(s *Server) {
if s.OpenAPI.Description().Components.SecuritySchemes == nil {
s.OpenAPI.Description().Components.SecuritySchemes = openapi3.SecuritySchemes{}
}
for name, scheme := range schemes {
s.OpenAPI.Description().Components.SecuritySchemes[name] = scheme
}
}
}
// WithoutAutoGroupTags disables the automatic grouping of routes by tags.
// By default, routes are tagged by group.
// For example:
//
// recipeGroup := fuego.Group(s, "/recipes")
// fuego.Get(recipeGroup, "/", func(ContextNoBody) (ans, error) {
// return ans{}, nil
// })
//
// RecipeThis route will be tagged with "recipes" by default, but with this option, they will not be tagged.
func WithoutAutoGroupTags() func(*Server) {
return func(c *Server) { c.disableAutoGroupTags = true }
}
// WithTemplates loads the templates used to render HTML.
// To be used with [WithTemplateFS]. If not set, it will use the os filesystem, at folder "./templates".
func WithTemplates(templates *template.Template) func(*Server) {
return func(s *Server) {
if s.fs == nil {
s.fs = os.DirFS("./templates")
slog.Warn("No template filesystem set. Using os filesystem at './templates'.")
}
s.template = templates
slog.Debug("Loaded templates", "templates", s.template.DefinedTemplates())
}
}
// WithTemplateGlobs loads templates matching the given patterns from the server filesystem.
// If the server filesystem is not set, it will use the OS filesystem, at folder "./templates".
// For example:
//
// WithTemplateGlobs("*.html, */*.html", "*/*/*.html")
// WithTemplateGlobs("pages/*.html", "pages/admin/*.html")
//
// for reference about the glob patterns in Go (no ** support for example): https://pkg.go.dev/path/filepath?utm_source=godoc#Match
func WithTemplateGlobs(patterns ...string) func(*Server) {
return func(s *Server) {
if s.fs == nil {
s.fs = os.DirFS("./templates")
slog.Warn("No template filesystem set. Using os filesystem at './templates'.")
}
err := s.loadTemplates(patterns...)
if err != nil {
slog.Error("Error loading templates", "error", err)
panic(err)
}
slog.Debug("Loaded templates", "templates", s.template.DefinedTemplates())
}
}
func WithBasePath(basePath string) func(*Server) {
return func(c *Server) { c.basePath = basePath }
}
func WithMaxBodySize(maxBodySize int64) func(*Server) {
return func(c *Server) { c.maxBodySize = maxBodySize }
}
func WithAutoAuth(verifyUserInfo func(user, password string) (jwt.Claims, error)) func(*Server) {
return func(c *Server) {
c.autoAuth.Enabled = true
c.autoAuth.VerifyUserInfo = verifyUserInfo
}
}
// WithDisallowUnknownFields sets the DisallowUnknownFields option.
// If true, the server will return an error if the request body contains unknown fields.
// Useful for quick debugging in development.
// Defaults to true.
func WithDisallowUnknownFields(b bool) func(*Server) {
return func(c *Server) { c.DisallowUnknownFields = b }
}
// WithAddr optionally specifies the TCP address for the server to listen on, in the form "host:port".
// If not specified addr ':9999' will be used.
func WithAddr(addr string) func(*Server) {
return func(c *Server) { c.Server.Addr = addr }
}
// WithXML sets the serializer to XML
//
// Deprecated: fuego supports automatic XML serialization when using the header "Accept: application/xml".
func WithXML() func(*Server) {
return func(c *Server) {
c.Serialize = SendXML
c.SerializeError = SendXMLError
}
}
// WithLogHandler sets the log handler of the server.
func WithLogHandler(handler slog.Handler) func(*Server) {
return func(c *Server) {
if handler != nil {
slog.SetDefault(slog.New(handler))
}
}
}
// WithRequestContentType sets the accepted content types for the server.
// By default, the accepted content types is */*.
func WithRequestContentType(consumes ...string) func(*Server) {
return func(s *Server) { s.acceptedContentTypes = consumes }
}
// WithSerializer sets a custom serializer of type Sender that overrides the default one.
// Please send a PR if you think the default serializer should be improved, instead of jumping to this option.
func WithSerializer(serializer Sender) func(*Server) {
return func(c *Server) { c.Serialize = serializer }
}
// WithErrorSerializer sets a custom serializer of type ErrorSender that overrides the default one.
// Please send a PR if you think the default serializer should be improved, instead of jumping to this option.
func WithErrorSerializer(serializer ErrorSender) func(*Server) {
return func(c *Server) { c.SerializeError = serializer }
}
// WithErrorHandler sets a customer error handler for the server
func WithErrorHandler(errorHandler func(err error) error) func(*Server) {
return func(c *Server) { c.ErrorHandler = errorHandler }
}
// WithoutStartupMessages disables the startup message
func WithoutStartupMessages() func(*Server) {
return func(c *Server) { c.disableStartupMessages = true }
}
// WithoutLogger disables the default logger.
func WithoutLogger() func(*Server) {
return func(c *Server) {
slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
}
}
func WithOpenAPIConfig(openapiConfig OpenAPIConfig) func(*Server) {
return func(s *Server) {
if openapiConfig.JsonUrl != "" {
s.OpenAPIConfig.JsonUrl = openapiConfig.JsonUrl
}
if openapiConfig.SwaggerUrl != "" {
s.OpenAPIConfig.SwaggerUrl = openapiConfig.SwaggerUrl
}
if openapiConfig.JsonFilePath != "" {
s.OpenAPIConfig.JsonFilePath = openapiConfig.JsonFilePath
}
if openapiConfig.UIHandler != nil {
s.OpenAPIConfig.UIHandler = openapiConfig.UIHandler
}
s.OpenAPIConfig.DisableSwagger = openapiConfig.DisableSwagger
s.OpenAPIConfig.DisableSwaggerUI = openapiConfig.DisableSwaggerUI
s.OpenAPIConfig.DisableLocalSave = openapiConfig.DisableLocalSave
s.OpenAPIConfig.PrettyFormatJson = openapiConfig.PrettyFormatJson
if !validateJsonSpecUrl(s.OpenAPIConfig.JsonUrl) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", s.OpenAPIConfig.JsonUrl)
return
}
if !validateSwaggerUrl(s.OpenAPIConfig.SwaggerUrl) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIConfig.SwaggerUrl' option is not valid", "url", s.OpenAPIConfig.SwaggerUrl)
return
}
}
}
// WithValidator sets the validator to be used by the fuego server.
// If no validator is provided, a default validator will be used.
//
// Note: If you are using the default validator, you can add tags to your structs using the `validate` tag.
// For example:
//
// type MyStruct struct {
// Field1 string `validate:"required"`
// Field2 int `validate:"min=10,max=20"`
// }
//
// The above struct will be validated using the default validator, and if any errors occur, they will be returned as part of the response.
func WithValidator(newValidator *validator.Validate) func(*Server) {
if newValidator == nil {
panic("new validator not provided")
}
return func(s *Server) {
v = newValidator
}
}
func WithRouteOptions(options ...func(*BaseRoute)) func(*Server) {
return func(s *Server) {
s.routeOptions = append(s.routeOptions, options...)
}
}