aerogo / aero

:bullettrain_side: High-performance web server for Go (2016). New alpha (2024) with even better performance is currently in development at https://git.akyoto.dev/go/web
MIT License
572 stars 33 forks source link

Fallback router not work properly with router within params #21

Open sakurayang opened 3 years ago

sakurayang commented 3 years ago

Code:

package main

import "github.com/aerogo/aero"

func echo(ctx aero.Context) error { return ctx.String("echo") }
func echoWithParams(ctx aero.Context) error { return ctx.String(ctx.Get("str")) }
func notFound(ctx aero.Context) error { return ctx.Error(404) }

func main() {
    app := aero.New()
    app.Config.Ports = aero.PortConfiguration{
        HTTP: 9000,
        HTTPS: 9001,
    }
    app.Any("/echo", echo)
    app.Any("/echo/", echo)
    app.Any("/echo/:str", echoWithParams)
    app.Any("/echo/:str/", echoWithParams)
    app.Any("/*",notFound)
    app.Any("/*/",notFound)
    app.Run()
}

What I expect:

Any other path not configure will return Not Found

What actually happens:

curl -v localhost:9000/echo
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#0)
> GET /echo HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 03 Jun 2021 04:41:13 GMT
< Content-Length: 4
< Content-Type: text/plain; charset=utf-8
<
echo* Connection #0 to host localhost left intact
curl -v localhost:9000/e
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#0)
> GET /e HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Thu, 03 Jun 2021 04:40:39 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
curl -v localhost:9000/e/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#0)
> GET /x HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Thu, 03 Jun 2021 04:45:39 GMT
< Content-Length: 9
< Content-Type: text/plain; charset=utf-8
<
Not Found* Connection #0 to host localhost left intact
curl -v localhost:9000/x/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#0)
> GET /x HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Thu, 03 Jun 2021 04:45:39 GMT
< Content-Length: 9
< Content-Type: text/plain; charset=utf-8
<
Not Found* Connection #0 to host localhost left intact

I don't know if is configure not properly or bug

but when I remove the params router it works again

package main

import "github.com/aerogo/aero"

func echo(ctx aero.Context) error { return ctx.String("echo") }
- func echoWithParams(ctx aero.Context) error { return ctx.String(ctx.Get("str")) }
func notFound(ctx aero.Context) error { return ctx.Error(404) }

func main() {
    app := aero.New()
    app.Config.Ports = aero.PortConfiguration{
        HTTP: 9000,
        HTTPS: 9001,
    }
    app.Any("/echo", echo)
    app.Any("/echo/", echo)
-   app.Any("/echo/:str", echoWithParams)
-   app.Any("/echo/:str/", echoWithParams)
    app.Any("/*",notFound)
    app.Any("/*/",notFound)
    app.Run()
}

...and when get /e

curl -v localhost:9000/e
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 9000 (#0)
> GET /e HTTP/1.1
> Host: localhost:9000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Thu, 03 Jun 2021 04:52:37 GMT
< Content-Length: 9
< Content-Type: text/plain; charset=utf-8
<
Not Found* Connection #0 to host localhost left intact