mirror of
https://github.com/labstack/echo.git
synced 2025-07-13 01:30:31 +02:00
Changed standard#WrapMiddleware
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -8,12 +8,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Engine defines an interface for HTTP server.
|
||||||
Engine interface {
|
Engine interface {
|
||||||
SetHandler(Handler)
|
SetHandler(Handler)
|
||||||
SetLogger(*log.Logger)
|
SetLogger(*log.Logger)
|
||||||
Start()
|
Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Request defines an interface for HTTP request.
|
||||||
Request interface {
|
Request interface {
|
||||||
TLS() bool
|
TLS() bool
|
||||||
Scheme() string
|
Scheme() string
|
||||||
@ -31,6 +33,7 @@ type (
|
|||||||
Object() interface{}
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Response defines an interface for HTTP response.
|
||||||
Response interface {
|
Response interface {
|
||||||
Header() Header
|
Header() Header
|
||||||
WriteHeader(int)
|
WriteHeader(int)
|
||||||
@ -43,6 +46,7 @@ type (
|
|||||||
Object() interface{}
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Header defines an interface for HTTP header.
|
||||||
Header interface {
|
Header interface {
|
||||||
Add(string, string)
|
Add(string, string)
|
||||||
Del(string)
|
Del(string)
|
||||||
@ -51,6 +55,7 @@ type (
|
|||||||
Object() interface{}
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URL defines an interface for HTTP request url.
|
||||||
URL interface {
|
URL interface {
|
||||||
SetPath(string)
|
SetPath(string)
|
||||||
Path() string
|
Path() string
|
||||||
@ -58,6 +63,7 @@ type (
|
|||||||
Object() interface{}
|
Object() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config defines engine configuration.
|
||||||
Config struct {
|
Config struct {
|
||||||
Address string
|
Address string
|
||||||
TLSCertfile string
|
TLSCertfile string
|
||||||
|
@ -137,9 +137,7 @@ func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
|
|||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
ctx := c.Request().Object().(*fasthttp.RequestCtx)
|
ctx := c.Request().Object().(*fasthttp.RequestCtx)
|
||||||
if !c.Response().Committed() {
|
|
||||||
h(ctx)
|
h(ctx)
|
||||||
}
|
|
||||||
return next.Handle(c)
|
return next.Handle(c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -132,16 +132,16 @@ func WrapHandler(h http.Handler) echo.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapMiddleware wraps `http.Handler` into `echo.MiddlewareFunc`
|
// WrapMiddleware wraps `func(http.Handler) http.Handler` into `echo.MiddlewareFunc`
|
||||||
func WrapMiddleware(h http.Handler) echo.MiddlewareFunc {
|
func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||||
w := c.Response().Object().(http.ResponseWriter)
|
w := c.Response().Object().(http.ResponseWriter)
|
||||||
r := c.Request().Object().(*http.Request)
|
r := c.Request().Object().(*http.Request)
|
||||||
if !c.Response().Committed() {
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
h.ServeHTTP(w, r)
|
err = next.Handle(c)
|
||||||
}
|
})).ServeHTTP(w, r)
|
||||||
return next.Handle(c)
|
return
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,4 +16,4 @@ menu:
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/crud)
|
### [Source Code]({{< source "crud" >}})
|
||||||
|
@ -9,12 +9,12 @@ menu:
|
|||||||
|
|
||||||
### With go.rice
|
### With go.rice
|
||||||
|
|
||||||
`rice.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "embed-resources/rice.go" >}}
|
{{< embed "embed-resources/main.go" >}}
|
||||||
|
|
||||||
### Maintainers
|
### Maintainers
|
||||||
|
|
||||||
- [caarlos0](https://github.com/caarlos0)
|
- [caarlos0](https://github.com/caarlos0)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/rice)
|
### [Source Code]({{< source "embed-resources" >}})
|
||||||
|
@ -36,9 +36,9 @@ if _, err = io.Copy(dst, file); err != nil {
|
|||||||
|
|
||||||
### Server
|
### Server
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "file-upload/server.go" >}}
|
{{< embed "file-upload/main.go" >}}
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
@ -50,4 +50,4 @@ if _, err = io.Copy(dst, file); err != nil {
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/file-upload)
|
### [Source Code]({{< source "file-upload" >}})
|
||||||
|
@ -132,4 +132,4 @@ but is outside the scope of this recipe.
|
|||||||
|
|
||||||
- [CaptainCodeman](https://github.com/CaptainCodeman)
|
- [CaptainCodeman](https://github.com/CaptainCodeman)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/google-app-engine)
|
### [Source Code]({{< source "google-app-engine" >}})
|
||||||
|
@ -8,15 +8,15 @@ menu:
|
|||||||
|
|
||||||
### With [grace](https://github.com/facebookgo/grace)
|
### With [grace](https://github.com/facebookgo/grace)
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "graceful-shutdown/grace/server.go" >}}
|
{{< embed "graceful-shutdown/grace/main.go" >}}
|
||||||
|
|
||||||
### With [graceful](https://github.com/tylerb/graceful)
|
### With [graceful](https://github.com/tylerb/graceful)
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "graceful-shutdown/graceful/server.go" >}}
|
{{< embed "graceful-shutdown/graceful/main.go" >}}
|
||||||
|
|
||||||
### Maintainers
|
### Maintainers
|
||||||
|
|
||||||
@ -24,6 +24,5 @@ menu:
|
|||||||
|
|
||||||
### Source Code
|
### Source Code
|
||||||
|
|
||||||
[graceful](https://github.com/vishr/echo-recipes/blob/master/v2/graceful-shutdown/graceful)
|
- [graceful]({{< source "graceful-shutdown/graceful" >}})
|
||||||
|
- [grace]({{< source "graceful-shutdown/grace" >}})
|
||||||
[grace](https://github.com/vishr/echo-recipes/blob/master/v2/graceful-shutdown/grace)
|
|
||||||
|
@ -16,4 +16,4 @@ menu:
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/hello-world)
|
### [Source Code]({{< source "hello-world" >}})
|
||||||
|
@ -24,4 +24,4 @@ JSONP is a method that allows cross-domain server calls. You can read more about
|
|||||||
|
|
||||||
- [willf](https://github.com/willf)
|
- [willf](https://github.com/willf)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/jsonp)
|
### [Source Code]({{< source "jsonp" >}})
|
||||||
|
@ -18,11 +18,11 @@ expects the token to be present in an Authorization HTTP header using the method
|
|||||||
or even the request body. We will use the HS236 signing method, note that several
|
or even the request body. We will use the HS236 signing method, note that several
|
||||||
other algorithms are available.
|
other algorithms are available.
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "jwt-authentication/server.go" >}}
|
{{< embed "jwt-authentication/main.go" >}}
|
||||||
|
|
||||||
Run `server.go` and making a request to the root path `/` returns a 200 OK response,
|
Run `main.go` and making a request to the root path `/` returns a 200 OK response,
|
||||||
as this route does not use our JWT authentication middleware. Sending requests to
|
as this route does not use our JWT authentication middleware. Sending requests to
|
||||||
`/restricted` (our authenticated route) with either no Authorization header or invalid
|
`/restricted` (our authenticated route) with either no Authorization header or invalid
|
||||||
Authorization headers / tokens will return 401 Unauthorized.
|
Authorization headers / tokens will return 401 Unauthorized.
|
||||||
@ -55,4 +55,4 @@ $ curl localhost:1323/restricted -H "Authorization: Bearer <token>" => Access g
|
|||||||
|
|
||||||
- [axdg](https://github.com/axdg)
|
- [axdg](https://github.com/axdg)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/jwt-authentication)
|
### [Source Code]({{< source "jwt-authentication" >}})
|
||||||
|
@ -17,4 +17,4 @@ menu:
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/middleware)
|
### [Source Code]({{< source "middleware" >}})
|
||||||
|
@ -11,9 +11,9 @@ menu:
|
|||||||
|
|
||||||
### Server
|
### Server
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "streaming-file-upload/server.go" >}}
|
{{< embed "streaming-file-upload/main.go" >}}
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
@ -25,4 +25,4 @@ menu:
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/streaming-file-upload)
|
### [Source Code]({{< source "streaming-file-upload" >}})
|
||||||
|
@ -11,9 +11,9 @@ menu:
|
|||||||
|
|
||||||
### Server
|
### Server
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "streaming-response/server.go" >}}
|
{{< embed "streaming-response/main.go" >}}
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
@ -35,4 +35,4 @@ $ curl localhost:1323
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/streaming-response)
|
### [Source Code]({{< source "streaming-response" >}})
|
||||||
|
@ -6,13 +6,13 @@ menu:
|
|||||||
weight: 10
|
weight: 10
|
||||||
---
|
---
|
||||||
|
|
||||||
`server.go`
|
`main.go`
|
||||||
|
|
||||||
{{< embed "subdomains/server.go" >}}
|
{{< embed "subdomains/main.go" >}}
|
||||||
|
|
||||||
### Maintainers
|
### Maintainers
|
||||||
|
|
||||||
- [axdg](https://github.com/axdg)
|
- [axdg](https://github.com/axdg)
|
||||||
- [vishr](https://github.com/axdg)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/subdomains)
|
### [Source Code]({{< source "subdomains" >}})
|
||||||
|
@ -10,7 +10,7 @@ menu:
|
|||||||
|
|
||||||
`server.go`
|
`server.go`
|
||||||
|
|
||||||
{{< embed "website/server.go" >}}
|
{{< embed "website/main.go" >}}
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
@ -22,4 +22,4 @@ menu:
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/website)
|
### [Source Code]({{< source "website" >}})
|
||||||
|
@ -10,7 +10,7 @@ menu:
|
|||||||
|
|
||||||
`server.go`
|
`server.go`
|
||||||
|
|
||||||
{{< embed "websocket/server.go" >}}
|
{{< embed "websocket/main.go" >}}
|
||||||
|
|
||||||
### Client
|
### Client
|
||||||
|
|
||||||
@ -44,4 +44,4 @@ Hello, Server!
|
|||||||
|
|
||||||
- [vishr](https://github.com/vishr)
|
- [vishr](https://github.com/vishr)
|
||||||
|
|
||||||
### [Source Code](https://github.com/vishr/echo-recipes/blob/master/v2/websocket)
|
### [Source Code]({{< source "websocket" >}})
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
<pre data-src="https://raw.githubusercontent.com/vishr/echo-recipes/master/v2/{{ .Get 0 }}">
|
<pre data-src="https://raw.githubusercontent.com/labstack/echox/master/recipe/{{ .Get 0 }}">
|
||||||
</pre>
|
</pre>
|
||||||
|
1
website/layouts/shortcodes/source.html
Normal file
1
website/layouts/shortcodes/source.html
Normal file
@ -0,0 +1 @@
|
|||||||
|
https://github.com/labstack/echox/tree/master/recipe/{{ .Get 0 }}
|
Reference in New Issue
Block a user