Skip to content

Commit

Permalink
refactor: new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Aug 27, 2020
1 parent 3a76a87 commit 0ffe290
Show file tree
Hide file tree
Showing 53 changed files with 2,923 additions and 6,430 deletions.
943 changes: 330 additions & 613 deletions README.md

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions benchmark/fiber/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"strconv"
"strings"

"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

// Item tracks the price of a good.
type Item struct {
ID int `json:"id"`
Name string `json:"name"`
Price float32 `json:"price"`
IsOffer bool `json:"is_offer,omitempty"`
}

func main() {
app := fiber.New()
app.Use(middleware.Recover())

d := func(c *fiber.Ctx) string {
return strings.Split(c.Get("authorization"), " ")[0]
}

app.Get("/items/:id", func(c *fiber.Ctx) {
tmp := c.Params("id")
id, err := strconv.Atoi(tmp)
if err != nil {
c.Status(500)
return
}

authInfo := d(c)

c.Set("x-authinfo", authInfo)
c.Status(200)
c.JSON(&Item{
ID: id,
Name: "Hello",
Price: 1.25,
IsOffer: false,
})
})

app.Listen("127.0.0.1:8000")
}
12 changes: 12 additions & 0 deletions benchmark/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/danielgtaylor/huma/benchmark

go 1.14

replace github.com/danielgtaylor/huma => ../

require (
github.com/danielgtaylor/huma v0.0.0-20200821183705-0c275cfd3c4a
github.com/gin-gonic/gin v1.5.0
github.com/gofiber/fiber v1.13.3
github.com/labstack/echo/v4 v4.1.15
)
247 changes: 247 additions & 0 deletions benchmark/go.sum

Large diffs are not rendered by default.

49 changes: 23 additions & 26 deletions benchmark/huma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"strings"

"github.com/danielgtaylor/huma"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/danielgtaylor/huma/cli"
"github.com/danielgtaylor/huma/middleware"
"github.com/danielgtaylor/huma/responses"
)

// Item tracks the price of a good.
Expand All @@ -17,34 +18,30 @@ type Item struct {
IsOffer bool `json:"is_offer,omitempty"`
}

type Input struct {
AuthInfo string
ID int `path:"id"`
}

func (i *Input) Resolve(ctx huma.Context, r *http.Request) {
i.AuthInfo = strings.Split(r.Header.Get("Authorization"), " ")[0]
}

func main() {
gin.SetMode(gin.ReleaseMode)
g := gin.New()
g.Use(huma.Recovery())
g.Use(cors.Default())
g.Use(huma.PreferMinimalMiddleware())

r := huma.NewRouter("Benchmark", "1.0.0", huma.Gin(g))

d := huma.Dependency(
huma.HeaderParam("authorization", "Auth header", ""),
func(auth string) (string, error) {
return strings.Split(auth, " ")[0], nil
},
)

r.Resource("/items", d,
huma.PathParam("id", "The item's unique ID"),
huma.ResponseHeader("x-authinfo", "..."),
huma.ResponseJSON(http.StatusOK, "Successful hello response", huma.Headers("x-authinfo")),
).Get("Huma benchmark test", func(authInfo string, id int) (string, *Item) {
return authInfo, &Item{
ID: id,
app := cli.New(huma.New("Benchmark", "1.0.0"))
app.Middleware(middleware.Recovery, middleware.ContentEncoding)

app.Resource("/items", "id").Get("get", "Huma benchmark test",
responses.OK().Headers("x-authinfo").Model(Item{}),
).Run(func(ctx huma.Context, input Input) {
ctx.Header().Set("x-authinfo", input.AuthInfo)
ctx.WriteModel(http.StatusOK, &Item{
ID: input.ID,
Name: "Hello",
Price: 1.24,
IsOffer: false,
}
})
})

r.Run()
app.Run()
}
147 changes: 0 additions & 147 deletions cli.go

This file was deleted.

Loading

0 comments on commit 0ffe290

Please sign in to comment.