-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
humachi.go
143 lines (113 loc) · 3.16 KB
/
humachi.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
package humachi
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"time"
"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/queryparam"
chiV4 "github.com/go-chi/chi"
"github.com/go-chi/chi/v5"
)
type chiContext struct {
op *huma.Operation
r *http.Request
w http.ResponseWriter
v4 bool
}
func (c *chiContext) Operation() *huma.Operation {
return c.op
}
func (c *chiContext) Context() context.Context {
return c.r.Context()
}
func (c *chiContext) Method() string {
return c.r.Method
}
func (c *chiContext) Host() string {
return c.r.Host
}
func (c *chiContext) URL() url.URL {
return *c.r.URL
}
func (c *chiContext) Param(name string) string {
if !c.v4 {
return chi.URLParam(c.r, name)
}
return chiV4.URLParam(c.r, name)
}
func (c *chiContext) Query(name string) string {
return queryparam.Get(c.r.URL.RawQuery, name)
}
func (c *chiContext) Header(name string) string {
return c.r.Header.Get(name)
}
func (c *chiContext) EachHeader(cb func(name, value string)) {
for name, values := range c.r.Header {
for _, value := range values {
cb(name, value)
}
}
}
func (c *chiContext) BodyReader() io.Reader {
return c.r.Body
}
func (c *chiContext) GetMultipartForm() (*multipart.Form, error) {
err := c.r.ParseMultipartForm(8 * 1024)
return c.r.MultipartForm, err
}
func (c *chiContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(c.w, deadline)
}
func (c *chiContext) SetStatus(code int) {
c.w.WriteHeader(code)
}
func (c *chiContext) AppendHeader(name string, value string) {
c.w.Header().Add(name, value)
}
func (c *chiContext) SetHeader(name string, value string) {
c.w.Header().Set(name, value)
}
func (c *chiContext) BodyWriter() io.Writer {
return c.w
}
// NewContext creates a new Huma context from an HTTP request and response.
func NewContext(op *huma.Operation, r *http.Request, w http.ResponseWriter) huma.Context {
return &chiContext{op: op, r: r, w: w}
}
type chiAdapter struct {
router chi.Router
}
func (a *chiAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
a.router.MethodFunc(op.Method, op.Path, func(w http.ResponseWriter, r *http.Request) {
handler(&chiContext{op: op, r: r, w: w})
})
}
func (a *chiAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r)
}
// NewAdapter creates a new adapter for the given chi router.
func NewAdapter(r chi.Router) huma.Adapter {
return &chiAdapter{router: r}
}
// New creates a new Huma API using the latest v5.x.x version of Chi.
func New(r chi.Router, config huma.Config) huma.API {
return huma.NewAPI(config, &chiAdapter{router: r})
}
type chiAdapterV4 struct {
router chiV4.Router
}
func (a *chiAdapterV4) Handle(op *huma.Operation, handler func(huma.Context)) {
a.router.MethodFunc(op.Method, op.Path, func(w http.ResponseWriter, r *http.Request) {
handler(&chiContext{op: op, r: r, w: w, v4: true})
})
}
func (a *chiAdapterV4) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r)
}
// NewV4 creates a new Huma API using the older v4.x.x version of Chi.
func NewV4(r chiV4.Router, config huma.Config) huma.API {
return huma.NewAPI(config, &chiAdapterV4{router: r})
}