Skip to content

Commit

Permalink
Merge pull request #99 from sorenmat/echo
Browse files Browse the repository at this point in the history
adding echo integration
  • Loading branch information
ggicci authored Mar 15, 2024
2 parents 63aead2 + df0d1cc commit c8657ee
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ require (
github.com/go-chi/chi/v5 v5.0.11
github.com/gorilla/mux v1.8.1
github.com/justinas/alice v1.2.0
github.com/labstack/echo/v4 v4.11.4
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 23 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,33 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/justinas/alice v1.2.0 h1:+MHSA/vccVCF4Uq37S42jwlkvI2Xzl7zTPCN5BnZNVo=
github.com/justinas/alice v1.2.0/go.mod h1:fN5HRH/reO/zrUflLfTN43t3vXvKzvZIENsNEe7i7qA=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
63 changes: 63 additions & 0 deletions integration/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package integration

import (
"mime/multipart"

"github.com/ggicci/httpin/core"
"github.com/labstack/echo/v4"
)

// UseEchoRouter registers a new directive executor which can extract values
// from path variables.
// https://ggicci.github.io/httpin/integrations/echo
//
// Usage:
//
// import httpin_integration "github.com/ggicci/httpin/integration"
//
// func init() {
// e := echo.New()
// httpin_integration.UseEchoRouter("path", e)
//
// // or
//
// httpin_integration.UseEchoPathRouter(e)
// }
func UseEchoRouter(name string, e *echo.Echo) {
core.RegisterDirective(
name,
core.NewDirectivePath((&echoRouterExtractor{e}).Execute),
true,
)
}

func UseEchoPathRouter(e *echo.Echo) {
UseEchoRouter("path", e)
}

// echoRouterExtractor is an extractor for mux.Vars
type echoRouterExtractor struct {
e *echo.Echo
}

func (mux *echoRouterExtractor) Execute(rtm *core.DirectiveRuntime) error {
req := rtm.GetRequest()
kvs := make(map[string][]string)

c := mux.e.NewContext(req, nil)
c.SetRequest(req)

mux.e.Router().Find(req.Method, req.URL.Path, c)

for _, key := range c.ParamNames() {
kvs[key] = []string{c.Param(key)}
}

extractor := &core.FormExtractor{
Runtime: rtm,
Form: multipart.Form{
Value: kvs,
},
}
return extractor.Extract()
}
49 changes: 49 additions & 0 deletions integration/echo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package integration

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/ggicci/httpin"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

type GetPostOfUserInput struct {
Username string `in:"path=username"`
PostID int64 `in:"path=pid"`
}

func TestUseEchoMux(t *testing.T) {

e := echo.New()
UseEchoPathRouter(e)

req := httptest.NewRequest(http.MethodGet, "/users/ggicci/posts/123", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()

c := e.NewContext(req, rec)

handler := func(ctx echo.Context) error {
param := &GetPostOfUserInput{}
core, err := httpin.New(param)
if err != nil {
return err
}
v, err := core.Decode(ctx.Request())
if err != nil {
return err
}
fmt.Println(param)
return c.JSON(http.StatusOK, v)
}
e.GET("/users/:username/posts/:pid", handler)
err := handler(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, `{"Username":"ggicci","PostID":123}`, strings.TrimSpace(rec.Body.String()))
}

0 comments on commit c8657ee

Please sign in to comment.