Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.3k
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Folks - How would one go about matching routes with variable segments in which the left-most, and, the right-most have a fixed interpretation but the "inside" segments are everything else. E.g.:
I currently handle this with explicit routes but I'm wondering if there's a more flexible way - such as one route with "wildcards". I'm generating the Echo code using oapi-codegen. OAPI 3 does not support this but if Echo did, that might lead me to move away from oapi at least in the short time. (I understand OAPI 4is going to support this.) |
BetaWas this translation helpful?Give feedback.
All reactions
You can get image name and tag by registering route with path variable and cut it in handler
package mainimport ("errors""fmt""log/slog""net/http""strings""github.com/labstack/echo/v4""github.com/labstack/echo/v4/middleware")funcmain() {e:=echo.New()e.Use(middleware.RequestLogger())e.Use(middleware.Recover())e.GET(`/foo/:tag`,func(c echo.Context)error {before,after,ok:=strings.Cut(c.Param(`tag`),":")if!ok {returnerrors.New("path does not contain colon")}returnc.JSON(http.StatusOK,map[string]string{"before":before,"after":after,})})e.GET(`/foo/bar/baz\:latest`,func(c echo.Context)error {returnc.String(http.S…
Replies: 2 comments 1 reply
-
Where I need to make my routes in echo framework. |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can get image name and tag by registering route with path variable and cut it in handler package mainimport ("errors""fmt""log/slog""net/http""strings""github.com/labstack/echo/v4""github.com/labstack/echo/v4/middleware")funcmain() {e:=echo.New()e.Use(middleware.RequestLogger())e.Use(middleware.Recover())e.GET(`/foo/:tag`,func(c echo.Context)error {before,after,ok:=strings.Cut(c.Param(`tag`),":")if!ok {returnerrors.New("path does not contain colon")}returnc.JSON(http.StatusOK,map[string]string{"before":before,"after":after,})})e.GET(`/foo/bar/baz\:latest`,func(c echo.Context)error {returnc.String(http.StatusOK,fmt.Sprintf("Hello from: `%s`\n",c.Path()))})iferr:=e.Start(":8080");err!=nil {slog.Error(err.Error())}} if you want define route that contain e.GET(`/foo/bar/baz\:latest`,func(c echo.Context)error {returnc.String(http.StatusOK,fmt.Sprintf("Hello from: `%s`\n",c.Path()))}) output: x@home:~/code$ curl"http://localhost:8080/foo/bar:latest"{"after":"latest","before":"bar"}x@home:~/code$ curl"http://localhost:8080/foo/image:2025"{"after":"2025","before":"image"}x@home:~/code$ curl"http://localhost:8080/foo/bar/baz:latest"Hello from:`/foo/bar/baz\:latest`x@home:~/code$ |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thank you for replying. The "latest" was just an example - it could be anything (1.2.3, v2.3.4, v1.0-experimental, etc) I think there's no easy way except with explicit paths for the various permutations. Thanks again though. |
BetaWas this translation helpful?Give feedback.