-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a temporary context for the first middleware to run for not se…
…nding content to client if the middleware fail
- Loading branch information
1 parent
61f2247
commit d0ec91b
Showing
4 changed files
with
86 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mixed | ||
|
||
import "net/http" | ||
|
||
// tempResponseWriter implement the http.ResponseWriter interface | ||
// It used when you need to temporary hold the content and use it later | ||
type tempResponseWriter struct { | ||
header http.Header | ||
StatusCode int | ||
Content []byte | ||
} | ||
|
||
func (rw *tempResponseWriter) Header() http.Header { | ||
return rw.header | ||
} | ||
|
||
func (rw *tempResponseWriter) Write(content []byte) (int, error) { | ||
rw.Content = append(rw.Content, content...) | ||
return len(content), nil | ||
} | ||
|
||
func (rw *tempResponseWriter) WriteHeader(code int) { | ||
rw.StatusCode = code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package mixed | ||
|
||
import ( | ||
"net/http" | ||
|
||
echo "github.com/labstack/echo/v4" | ||
) | ||
|
||
// copyContext create a new echo.Context with the same Request, Path, Params and Handler | ||
// but with a Response that contain an in-memory ResponseWriter | ||
func copyContext(c echo.Context) echo.Context { | ||
cc := c.Echo().AcquireContext() | ||
cc.SetRequest(c.Request()) | ||
cc.SetPath(c.Path()) | ||
cc.SetParamNames(c.ParamNames()...) | ||
cc.SetParamValues(c.ParamValues()...) | ||
cc.SetHandler(c.Handler()) | ||
|
||
rw := tempResponseWriter{ | ||
header: make(http.Header), | ||
Content: []byte{}, | ||
} | ||
resp := echo.NewResponse(&rw, c.Echo()) | ||
cc.SetResponse(resp) | ||
return cc | ||
} | ||
|
||
// copyResponse copy c2 headers and content into c1 | ||
func copyResponse(c1, c2 echo.Context) { | ||
for k, v := range c2.Response().Header() { | ||
for _, vv := range v { | ||
c1.Response().Header().Set(k, vv) | ||
} | ||
} | ||
|
||
if c2.Response().Status > 0 { | ||
c1.Response().WriteHeader(c2.Response().Status) | ||
} | ||
|
||
c1.Response().Write(c2.Response().Writer.(*tempResponseWriter).Content) | ||
} |