1

I testing a redirect about echo when I just use the method to redirect it can show the login.html page successfully, but if I use a middleware test, it always shows empty page of login.html, what am I missing?

    e.Group("*.html", func(next echo.HandlerFunc) echo.HandlerFunc {  //1
        return func(c echo.Context) error {
            uri := c.Request().URL.String()
            log.Println("uri:" + uri)
            if uri != "/login.html" && uri != "/favicon.ico" {
                c.Redirect(http.StatusSeeOther, "login.html")
                return nil
            }
            return nil
        }
    })
    e.Use(session.Middleware(sessions.NewCookieStore([]byte("secret"))))
    e.GET("/aa", func(c echo.Context) error {   //2
        c.Redirect(http.StatusSeeOther, "login.html")
        return nil
    })
smark
  • 43
  • 1
  • 8

1 Answers1

0

I see that you are missing the call of next to continue the request chain.

See the example here: https://echo.labstack.com/cookbook/middleware/

// ServerHeader middleware adds a `Server` header to the response.
func ServerHeader(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        c.Response().Header().Set(echo.HeaderServer, "Echo/3.0")
        return next(c)
    }
}

See the return next(c), it continues to process the request throw all middlewares and eventually the login.html static handler.

As you are not calling, it stops the chain and does nothing.

Lucas Katayama
  • 4,445
  • 27
  • 34