-
|
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 4 is going to support this.) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Where I need to make my routes in echo framework. |
Beta Was this translation helpful? Give feedback.
-
|
You can get image name and tag by registering route with path variable and cut it in handler package main
import (
"errors"
"fmt"
"log/slog"
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
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 {
return errors.New("path does not contain colon")
}
return c.JSON(http.StatusOK, map[string]string{
"before": before,
"after": after,
})
})
e.GET(`/foo/bar/baz\:latest`, func(c echo.Context) error {
return c.String(http.StatusOK, fmt.Sprintf("Hello from: `%s`\n", c.Path()))
})
if err := 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 {
return c.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$ |
Beta Was this translation helpful? Give feedback.
You can get image name and tag by registering route with path variable and cut it in handler