Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Routes with variable number of path segments#2845

Answeredbyaldas
aceeric asked this question inQ&A
Discussion options

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.:

docker pull docker.io/hello-world:latest.: left-most is registry, rightmost is tag, everything else is repository. Then that rule holds for all these:

docker pull docker.io/foo/bar:latest.
docker pull docker.io/foo/bar/baz:latest.
docker pull docker.io/foo/bar/baz/and/so/on:latest.

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.)

You must be logged in to vote
Answered by aldasDec 19, 2025

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

Comment options

Where I need to make my routes in echo framework.

You must be logged in to vote
0 replies
Comment options

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: you need to escape it.

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$
You must be logged in to vote
1 reply
@aceeric
Comment options

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.

Answer selected byaceeric
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@aceeric@aldas@waleed-haider38

[8]ページ先頭

©2009-2026 Movatter.jp