Skip to content

Commit

Permalink
Merge pull request #119 from lordspinach/multipart_support
Browse files Browse the repository at this point in the history
Add GetMultipartForm method to huma context
  • Loading branch information
danielgtaylor authored Sep 5, 2023
2 parents ed0a877 + e1e4739 commit 0c10966
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 164 deletions.
66 changes: 36 additions & 30 deletions adapters/humachi/humachi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humachi
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"time"
Expand All @@ -18,68 +19,73 @@ type chiContext struct {
w http.ResponseWriter
}

func (ctx *chiContext) Operation() *huma.Operation {
return ctx.op
func (c *chiContext) Operation() *huma.Operation {
return c.op
}

func (ctx *chiContext) Context() context.Context {
return ctx.r.Context()
func (c *chiContext) Context() context.Context {
return c.r.Context()
}

func (ctx *chiContext) Method() string {
return ctx.r.Method
func (c *chiContext) Method() string {
return c.r.Method
}

func (ctx *chiContext) Host() string {
return ctx.r.Host
func (c *chiContext) Host() string {
return c.r.Host
}

func (ctx *chiContext) URL() url.URL {
return *ctx.r.URL
func (c *chiContext) URL() url.URL {
return *c.r.URL
}

func (ctx *chiContext) Param(name string) string {
return chi.URLParam(ctx.r, name)
func (c *chiContext) Param(name string) string {
return chi.URLParam(c.r, name)
}

func (ctx *chiContext) Query(name string) string {
return queryparam.Get(ctx.r.URL.RawQuery, name)
func (c *chiContext) Query(name string) string {
return queryparam.Get(c.r.URL.RawQuery, name)
}

func (ctx *chiContext) Header(name string) string {
return ctx.r.Header.Get(name)
func (c *chiContext) Header(name string) string {
return c.r.Header.Get(name)
}

func (ctx *chiContext) EachHeader(cb func(name, value string)) {
for name, values := range ctx.r.Header {
func (c *chiContext) EachHeader(cb func(name, value string)) {
for name, values := range c.r.Header {
for _, value := range values {
cb(name, value)
}
}
}

func (ctx *chiContext) BodyReader() io.Reader {
return ctx.r.Body
func (c *chiContext) BodyReader() io.Reader {
return c.r.Body
}

func (ctx *chiContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(ctx.w, deadline)
func (c *chiContext) GetMultipartForm() (*multipart.Form, error) {
err := c.r.ParseMultipartForm(8 * 1024)
return c.r.MultipartForm, err
}

func (ctx *chiContext) SetStatus(code int) {
ctx.w.WriteHeader(code)
func (c *chiContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(c.w, deadline)
}

func (ctx *chiContext) AppendHeader(name string, value string) {
ctx.w.Header().Add(name, value)
func (c *chiContext) SetStatus(code int) {
c.w.WriteHeader(code)
}

func (ctx *chiContext) SetHeader(name string, value string) {
ctx.w.Header().Set(name, value)
func (c *chiContext) AppendHeader(name string, value string) {
c.w.Header().Add(name, value)
}

func (ctx *chiContext) BodyWriter() io.Writer {
return ctx.w
func (c *chiContext) SetHeader(name string, value string) {
c.w.Header().Set(name, value)
}

func (c *chiContext) BodyWriter() io.Writer {
return c.w
}

type chiAdapter struct {
Expand Down
13 changes: 9 additions & 4 deletions adapters/humafiber/humafiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humafiber
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
Expand All @@ -17,8 +18,8 @@ type fiberCtx struct {
orig *fiber.Ctx
}

func (ctx *fiberCtx) Operation() *huma.Operation {
return ctx.op
func (c *fiberCtx) Operation() *huma.Operation {
return c.op
}

func (c *fiberCtx) Matched() string {
Expand All @@ -33,8 +34,8 @@ func (c *fiberCtx) Method() string {
return c.orig.Method()
}

func (ctx *fiberCtx) Host() string {
return ctx.orig.Hostname()
func (c *fiberCtx) Host() string {
return c.orig.Hostname()
}

func (c *fiberCtx) URL() url.URL {
Expand Down Expand Up @@ -64,6 +65,10 @@ func (c *fiberCtx) BodyReader() io.Reader {
return c.orig.Request().BodyStream()
}

func (c *fiberCtx) GetMultipartForm() (*multipart.Form, error) {
return c.orig.MultipartForm()
}

func (c *fiberCtx) SetReadDeadline(deadline time.Time) error {
// Note: for this to work properly you need to do two things:
// 1. Set the Fiber app's `StreamRequestBody` to `true`
Expand Down
18 changes: 12 additions & 6 deletions adapters/humagin/humagin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humagin
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
Expand All @@ -17,8 +18,8 @@ type ginCtx struct {
orig *gin.Context
}

func (ctx *ginCtx) Operation() *huma.Operation {
return ctx.op
func (c *ginCtx) Operation() *huma.Operation {
return c.op
}

func (c *ginCtx) Context() context.Context {
Expand All @@ -29,8 +30,8 @@ func (c *ginCtx) Method() string {
return c.orig.Request.Method
}

func (ctx *ginCtx) Host() string {
return ctx.orig.Request.Host
func (c *ginCtx) Host() string {
return c.orig.Request.Host
}

func (c *ginCtx) URL() url.URL {
Expand Down Expand Up @@ -61,8 +62,13 @@ func (c *ginCtx) BodyReader() io.Reader {
return c.orig.Request.Body
}

func (ctx *ginCtx) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(ctx.orig.Writer, deadline)
func (c *ginCtx) GetMultipartForm() (*multipart.Form, error) {
err := c.orig.Request.ParseMultipartForm(8 * 1024)
return c.orig.Request.MultipartForm, err
}

func (c *ginCtx) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(c.orig.Writer, deadline)
}

func (c *ginCtx) SetStatus(code int) {
Expand Down
66 changes: 36 additions & 30 deletions adapters/humahttprouter/humahttprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package humahttprouter
import (
"context"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
Expand All @@ -20,68 +21,73 @@ type httprouterContext struct {
ps httprouter.Params
}

func (ctx *httprouterContext) Operation() *huma.Operation {
return ctx.op
func (c *httprouterContext) Operation() *huma.Operation {
return c.op
}

func (ctx *httprouterContext) Context() context.Context {
return ctx.r.Context()
func (c *httprouterContext) Context() context.Context {
return c.r.Context()
}

func (ctx *httprouterContext) Method() string {
return ctx.r.Method
func (c *httprouterContext) Method() string {
return c.r.Method
}

func (ctx *httprouterContext) Host() string {
return ctx.r.Host
func (c *httprouterContext) Host() string {
return c.r.Host
}

func (ctx *httprouterContext) URL() url.URL {
return *ctx.r.URL
func (c *httprouterContext) URL() url.URL {
return *c.r.URL
}

func (ctx *httprouterContext) Param(name string) string {
return ctx.ps.ByName(name)
func (c *httprouterContext) Param(name string) string {
return c.ps.ByName(name)
}

func (ctx *httprouterContext) Query(name string) string {
return queryparam.Get(ctx.r.URL.RawQuery, name)
func (c *httprouterContext) Query(name string) string {
return queryparam.Get(c.r.URL.RawQuery, name)
}

func (ctx *httprouterContext) Header(name string) string {
return ctx.r.Header.Get(name)
func (c *httprouterContext) Header(name string) string {
return c.r.Header.Get(name)
}

func (ctx *httprouterContext) EachHeader(cb func(name, value string)) {
for name, values := range ctx.r.Header {
func (c *httprouterContext) EachHeader(cb func(name, value string)) {
for name, values := range c.r.Header {
for _, value := range values {
cb(name, value)
}
}
}

func (ctx *httprouterContext) BodyReader() io.Reader {
return ctx.r.Body
func (c *httprouterContext) BodyReader() io.Reader {
return c.r.Body
}

func (ctx *httprouterContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(ctx.w, deadline)
func (c *httprouterContext) GetMultipartForm() (*multipart.Form, error) {
err := c.r.ParseMultipartForm(8 * 1024)
return c.r.MultipartForm, err
}

func (ctx *httprouterContext) SetStatus(code int) {
ctx.w.WriteHeader(code)
func (c *httprouterContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(c.w, deadline)
}

func (ctx *httprouterContext) AppendHeader(name string, value string) {
ctx.w.Header().Add(name, value)
func (c *httprouterContext) SetStatus(code int) {
c.w.WriteHeader(code)
}

func (ctx *httprouterContext) SetHeader(name string, value string) {
ctx.w.Header().Set(name, value)
func (c *httprouterContext) AppendHeader(name string, value string) {
c.w.Header().Add(name, value)
}

func (ctx *httprouterContext) BodyWriter() io.Writer {
return ctx.w
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 {
Expand Down
Loading

0 comments on commit 0c10966

Please sign in to comment.