-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Match API #2786
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
Comments
This seems at little bit too specific. What are you doing with this logic? I would like to understand the use-case. You can achieve same today with func match(e *echo.Echo, req *http.Request) (bool, error) {
router, ok := e.Routers()[req.Host]
if !ok {
router = e.Router()
}
c := e.AcquireContext()
defer e.ReleaseContext(c)
router.Find(req.Method, echo.GetPath(req), c)
return c.Handler() != nil, nil
} |
I am not using echos host map. though its also not clear that echo supports host based routing based on prev issues and lack of docs.
Im basically needing to have a set of api routes globally available, which would be on a default router echo instance. And they get routed to if the host specific router doesn't match... but I currently cant know if it does or not... |
your first example is using but
one way to achieve this requirement would be middleware that decides if current middleware chain should be executed or start executing default router chain. package main
import (
"errors"
"github.com/labstack/echo/v4"
"net/http"
)
func main() {
e := echo.New()
// first middleware in chain will decide if default router should be executed instead
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// path is empty when there is no route match (404/405 routes have path value)
// when we are already executing "___default___" use next(c) not do get into infinite loop
if c.Request().Host == "___default___" || c.Path() != "" {
return next(c)
}
// set host to some value that will not match any defined "host" values
c.Request().Host = "___default___"
e.ServeHTTP(c.Response(), c.Request())
return nil
}
})
e.RouteNotFound("/", func(c echo.Context) error {
return c.String(http.StatusNotFound, "not found")
})
e.GET("/test", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello from default host!\n")
})
h1 := e.Host("host1")
h1.GET("/test", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello from host1!\n")
})
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
e.Logger.Fatal(err)
}
} test with curl -H "host:host1" http://localhost:8080/test
curl -H "host:host1" http://localhost:8080/testa
curl http://localhost:8080/test
curl http://localhost:8080/ |
Thanks though that would not really work easily in my design. need to have testing from the outside, not from within. im working with the example you gave though im also getting some odd outcomes. |
Ive ended up at
I tried doing |
use IDE debugger to check what is being compared. |
I am, all the time... Right now im running into issues with the wildcard catch all routes kind of interfering? |
:) wildcard routes are meant to catch all. |
What im seeing though is they may be taking priority over more specific routes. |
it is not impossible but highly unlikely, there are small gotchas here and there definitively. Without small example it is not possible comment more. |
np, im stopping for the day, so ill be having to deal with my left over routing issues tomorrow. Thanks for the fast responses. |
So my issue is that I have 2 isolated routers and one has
this solves basic detection, but the rest requires checking if the path is a wildcard. ive landed on the route to pass an escape hatch to a library I have forked to use a servehttp callback so i have more explicit control on the routing logic. Since the routers are unaware I have to decide who gets what routing request and that is tricky because I need some routes to accessable across vhosts,and the root, and some to only be the vhost when either might have a wildcard, and not all routes are registered in every router. So it seems an abstraction to decide that above echo is needed. and since echos host support is a simple map of |
I would like to see something like this in echo. If the maintainers are ok with it, I can submit a PR. I am in need to know if a router has a route blindly, from the outside. I am using it as part of higher abstractions.
The text was updated successfully, but these errors were encountered: