Echo: Group(): Routing problems

Created on 24 Sep 2017  路  3Comments  路  Source: labstack/echo

Issue Description

There is an issue with routing requests when utilizing the group() method. When registering routes without group() the router is working as expected.

Expected behaviour

When registering a route in a sub-router the url path should be mapped to the registered handler method.

Actual behaviour

See the provided example code.

Steps to reproduce

Run the applicatins and start requests.

Working code to debug

Working example

package main

import (
    "net/http"
    "github.com/labstack/echo"
    "github.com/labstack/echo/middleware"
)

func adminIndex(c echo.Context) error {
    return c.String(http.StatusOK, "Hello, Admin!")
}

func main() {
    e := echo.New()

    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })

    e.GET("/admin*", adminIndex)
    e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
        if username == "joe" && password == "123" {
            return true, nil
        }
        return false, nil
    }))

    e.Logger.Fatal(e.Start(":1323"))
}

Expamle with routing issue


package main

import (
    "net/http"
    "github.com/labstack/echo"
    "github.com/labstack/echo/middleware"
)

func adminIndex(c echo.Context) error {
    return c.String(http.StatusOK, "Hello, Admin!")
}

func main() {
    e := echo.New()
    //e.Pre(middleware.RemoveTrailingSlash())
    //e.Pre(middleware.AddTrailingSlash())

    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })

    admin := e.Group("/admin")
    admin.GET("", adminIndex)
    admin.GET("/*", adminIndex)

    admin.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
        if username == "joe" && password == "123" {
            return true, nil
        }
        return false, nil
    }))

    e.Logger.Fatal(e.Start(":1323"))
}

Version/commit

Echo v3.2.3 and Golang 1.9.

question

Most helpful comment

Move your middleware before you define any handler and it should work.

All 3 comments

Move your middleware before you define any handler and it should work.

Thank you for your comments.

  • What do you mean when saying 'Move your middleware'?
  • Do you have an idea what lines to move or what to add to the provided example?

You should define middleware first and then any handlers. So, in your example admin.Use should come before admin.GET.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neutronstein picture neutronstein  路  3Comments

montanaflynn picture montanaflynn  路  3Comments

ellisonleao picture ellisonleao  路  3Comments

leoycx picture leoycx  路  4Comments

syntaqx picture syntaqx  路  3Comments