-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
29 lines (26 loc) · 785 Bytes
/
handler.go
File metadata and controls
29 lines (26 loc) · 785 Bytes
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
package fun
import "net/http"
// HandlerFunc is like http.HandlerFunc but returns a *Response.
// The returned response is automatically sent — no need to call .Send(w).
// Returning nil is a no-op.
//
// Usage:
//
// r.Get("/users/{id}", fun.Handler(GetUser))
//
// func GetUser(w http.ResponseWriter, r *http.Request) *fun.Response {
// id, err := req.Path("id").UUID()
// if err != nil {
// return fun.Error(err)
// }
// return fun.OK().WithData(user)
// }
type HandlerFunc func(http.ResponseWriter, *http.Request) *Response
// Handler wraps a HandlerFunc into a standard http.HandlerFunc.
func Handler(h HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if resp := h(w, r); resp != nil {
resp.Send(w)
}
}
}