Router¶
HTTP routing with radix tree implementation.
Route Registration¶
app.GET(path string, h Handler, mw ...Middleware)
app.POST(path string, h Handler, mw ...Middleware)
app.PUT(path string, h Handler, mw ...Middleware)
app.DELETE(path string, h Handler, mw ...Middleware)
app.PATCH(path string, h Handler, mw ...Middleware)
app.HEAD(path string, h Handler, mw ...Middleware)
app.OPTIONS(path string, h Handler, mw ...Middleware)
app.Handle(method, path string, h Handler, mw ...Middleware)
Path Parameters¶
Named parameters with :name:
app.GET("/users/:id", func(c *marten.Ctx) error {
id := c.Param("id")
return c.OK(marten.M{"id": id})
})
app.GET("/posts/:year/:month", func(c *marten.Ctx) error {
year := c.Param("year")
month := c.Param("month")
return c.OK(marten.M{"year": year, "month": month})
})
Wildcard Routes¶
Capture remaining path with *name:
app.GET("/files/*filepath", func(c *marten.Ctx) error {
path := c.Param("filepath")
// /files/images/logo.png → filepath = "images/logo.png"
return c.OK(marten.M{"path": path})
})
Route Priority¶
- Static segments (exact match)
- Named parameters (
:id) - Wildcards (
*filepath)
app.GET("/users/new", newUser) // Matches first
app.GET("/users/:id", getUser) // Matches /users/123
app.GET("/files/*path", serveFile) // Matches /files/any/path
Route Groups¶
api := app.Group("/api/v1")
api.Use(authMiddleware)
api.GET("/users", listUsers)
api.POST("/users", createUser)
Middleware¶
Global Middleware¶
Route-Specific Middleware¶
Group Middleware¶
Custom 404¶
app.NotFound(func(c *marten.Ctx) error {
return c.JSON(404, marten.M{
"error": "endpoint not found",
"path": c.Path(),
})
})
405 Method Not Allowed¶
When a path exists but the HTTP method doesn't match, Marten returns 405 with an Allow header:
Trailing Slash Handling¶
Configure how trailing slashes are handled:
// Ignore (default) - /users and /users/ match the same route
app.SetTrailingSlash(marten.TrailingSlashIgnore)
// Redirect - /users/ redirects to /users with 301
app.SetTrailingSlash(marten.TrailingSlashRedirect)
// Strict - /users and /users/ are different routes
app.SetTrailingSlash(marten.TrailingSlashStrict)
Route Conflict Detection¶
Marten detects conflicting parameter routes at registration time:
Same parameter name is allowed for different methods: