-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
humahttprouter.go
126 lines (100 loc) · 2.81 KB
/
humahttprouter.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
package humahttprouter
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/queryparam"
"github.com/julienschmidt/httprouter"
)
// MultipartMaxMemory is the maximum memory to use when parsing multipart
// form data.
var MultipartMaxMemory int64 = 8 * 1024
type httprouterContext struct {
op *huma.Operation
r *http.Request
w http.ResponseWriter
ps httprouter.Params
status int
}
// check that httprouterContext implements huma.Context
var _ huma.Context = &httprouterContext{}
func (c *httprouterContext) Operation() *huma.Operation {
return c.op
}
func (c *httprouterContext) Context() context.Context {
return c.r.Context()
}
func (c *httprouterContext) Method() string {
return c.r.Method
}
func (c *httprouterContext) Host() string {
return c.r.Host
}
func (c *httprouterContext) URL() url.URL {
return *c.r.URL
}
func (c *httprouterContext) Param(name string) string {
return c.ps.ByName(name)
}
func (c *httprouterContext) Query(name string) string {
return queryparam.Get(c.r.URL.RawQuery, name)
}
func (c *httprouterContext) Header(name string) string {
return c.r.Header.Get(name)
}
func (c *httprouterContext) EachHeader(cb func(name, value string)) {
for name, values := range c.r.Header {
for _, value := range values {
cb(name, value)
}
}
}
func (c *httprouterContext) BodyReader() io.Reader {
return c.r.Body
}
func (c *httprouterContext) GetMultipartForm() (*multipart.Form, error) {
err := c.r.ParseMultipartForm(MultipartMaxMemory)
return c.r.MultipartForm, err
}
func (c *httprouterContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(c.w, deadline)
}
func (c *httprouterContext) SetStatus(code int) {
c.status = code
c.w.WriteHeader(code)
}
func (c *httprouterContext) Status() int {
return c.status
}
func (c *httprouterContext) AppendHeader(name string, value string) {
c.w.Header().Add(name, value)
}
func (c *httprouterContext) SetHeader(name string, value string) {
c.w.Header().Set(name, value)
}
func (c *httprouterContext) BodyWriter() io.Writer {
return c.w
}
type httprouterAdapter struct {
router *httprouter.Router
}
func (a *httprouterAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
// Convert {param} to :param
path := op.Path
path = strings.ReplaceAll(path, "{", ":")
path = strings.ReplaceAll(path, "}", "")
a.router.Handle(op.Method, path, func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
handler(&httprouterContext{op: op, r: r, w: w, ps: ps})
})
}
func (a *httprouterAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r)
}
func New(r *httprouter.Router, config huma.Config) huma.API {
return huma.NewAPI(config, &httprouterAdapter{router: r})
}