-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using scs with Echo #5
Comments
Unfortunately, I believe this is a bug with Echo which will affect anyone who wants to use the new net/http For example, here's a more generalized example, with no SCS stuff in it: package main
import (
"context"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
)
func fooMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "foo", "bar")
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func main() {
e := echo.New()
e.Use(standard.WrapMiddleware(fooMiddleware))
e.GET("/", func(c echo.Context) error {
foo, ok := c.Request().(*standard.Request).Request.Context().Value("foo").(string)
if !ok {
return c.String(http.StatusInternalServerError, "could not find foo")
}
return c.String(http.StatusOK, foo)
})
e.Run(standard.New(":1323"))
} You would expect the handler to output a 200 status and the content I've opened an issue on the Echo repository: labstack/echo#685 |
@alexedwards Thanks for your work! |
@cxjava You're welcome. There's no changes that need to be made to SCS on this (just Echo), so I'll close this. |
@alexedwards I try to use this one into echo v3. For the example 1, works fine, the result as expected. But for the example 2, I can't get the expected message
|
@cxjava Did you solve the problem? I also want to integrate echo v3 and SCS. |
@dxvgef no... |
Sorry for the (very) slow reply. This is happening because the session cookie is never being written by the SCS middleware. I think the session cookie isn't being written because Echo's WrapMiddleware doesn't actually pass the underlying http.ResponseWriter onwards -- it passes it's own c.Response() object instead. So the session manager's Write method is never being called. func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc {
return func(next HandlerFunc) HandlerFunc {
return func(c Context) (err error) {
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.SetRequest(r)
err = next(c)
})).ServeHTTP(c.Response(), c.Request())
return
}
}
} The only 'fix' I can see is to call e.GET("/put", func(c echo.Context) error {
err := session.PutString(c.Request(), "message", "Hello world!")
if err != nil {
log.Println(err.Error())
}
err = session.Save(c.Response(), c.Request())
if err != nil {
log.Println(err.Error())
}
return c.String(http.StatusOK, "put")
}) This essentially writes the session data and cookie, instead of relying on the middleware to do it. |
Is there a way to use the middle ware provided by scs within echo? I try to do as below way, but failed. Can you give me some suggestion?
The text was updated successfully, but these errors were encountered: