-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_generic.go
More file actions
62 lines (57 loc) · 1.91 KB
/
handler_generic.go
File metadata and controls
62 lines (57 loc) · 1.91 KB
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
// Copyright 2025 coregx. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fursy
// Handler is a type-safe handler function for HTTP requests with typed request/response bodies.
//
// Type parameters:
// - Req: The expected request body type (use Empty if no body)
// - Res: The response body type (use Empty if no structured response)
//
// The handler receives a Box[Req, Res] with automatically bound request body (ReqBody)
// and provides type-safe methods for sending responses.
//
// Example:
//
// type CreateUserRequest struct {
// Name string `json:"name"`
// Email string `json:"email"`
// }
//
// type UserResponse struct {
// ID int `json:"id"`
// Name string `json:"name"`
// }
//
// func createUser(c *Box[CreateUserRequest, UserResponse]) error {
// req := c.ReqBody // Type: *CreateUserRequest
// user := db.Create(req.Name, req.Email)
// return c.Created("/users/"+user.ID, UserResponse{
// ID: user.ID,
// Name: user.Name,
// })
// }
//
// router.POST[CreateUserRequest, UserResponse]("/users", createUser)
type Handler[Req, Res any] func(*Box[Req, Res]) error
// adaptGenericHandler converts a generic Handler[Req, Res] to a non-generic HandlerFunc.
//
// This adapter:
// 1. Creates a generic Box[Req, Res] from Context
// 2. Binds the request body to Box.ReqBody (based on Content-Type)
// 3. Calls the generic handler
// 4. Returns any error from binding or handler execution
//
// This is used internally by Router.GET, Router.POST, etc. to support generic handlers.
func adaptGenericHandler[Req, Res any](handler Handler[Req, Res]) HandlerFunc {
return func(base *Context) error {
// Create generic context
ctx := newBox[Req, Res](base)
// Bind request body
if err := ctx.Bind(); err != nil {
return err
}
// Call generic handler
return handler(ctx)
}
}