Skip to content

Commit

Permalink
Merge pull request #111 from danielgtaylor/v2
Browse files Browse the repository at this point in the history
Initial v2 Beta
  • Loading branch information
danielgtaylor committed Aug 25, 2023
2 parents 2a9c8f8 + 332f25e commit 536a097
Show file tree
Hide file tree
Showing 102 changed files with 9,107 additions and 11,558 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
- name: Setup go
uses: actions/setup-go@v1
with:
go-version: "1.17"
go-version: "1.20"
- run: go test -coverprofile=coverage.txt -covermode=atomic ./...
- uses: codecov/codecov-action@v1
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
coverage.txt
.vscode
*.test
cpu.out
mem.out
1,624 changes: 624 additions & 1,000 deletions README.md

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions adapters/humachi/humachi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package humachi

import (
"context"
"io"
"net/http"
"net/url"
"time"

"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/queryparam"
"github.com/go-chi/chi"
)

type chiContext struct {
op *huma.Operation
r *http.Request
w http.ResponseWriter
}

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

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

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

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

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

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

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

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

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

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

func (ctx *chiContext) SetReadDeadline(deadline time.Time) error {
return huma.SetReadDeadline(ctx.w, deadline)
}

func (ctx *chiContext) SetStatus(code int) {
ctx.w.WriteHeader(code)
}

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

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

func (ctx *chiContext) BodyWriter() io.Writer {
return ctx.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)
}

func New(r chi.Router, config huma.Config) huma.API {
return huma.NewAPI(config, &chiAdapter{router: r})
}
Loading

0 comments on commit 536a097

Please sign in to comment.