This example demonstrates various routing patterns in DotWeb.
- HTTP methods (GET, POST, PUT, DELETE, ANY)
- Path parameters (
:id,:userId/:postId) - Wildcard routes (
*filepath) - Route groups (
/api,/api/v1)
cd example/routing
go run main.go# GET request
curl http://localhost:8080/
# Output: GET / - Home page
# POST request
curl -X POST http://localhost:8080/users
# Output: POST /users - Create user
# PUT request
curl -X PUT http://localhost:8080/users/123
# Output: PUT /users/123 - Update user
# DELETE request
curl -X DELETE http://localhost:8080/users/123
# Output: DELETE /users/123 - Delete user
# Any method
curl -X POST http://localhost:8080/any
# Output: ANY /any - Method: POST# Single parameter
curl http://localhost:8080/users/42
# Output: User ID: 42
# Multiple parameters
curl http://localhost:8080/users/42/posts/100
# Output: User: 42, Post: 100
# Wildcard (catch-all)
curl http://localhost:8080/files/path/to/file.txt
# Output: File path: /path/to/file.txt# API group
curl http://localhost:8080/api/health
# Output: {"status": "ok"}
curl http://localhost:8080/api/version
# Output: {"version": "1.0.0"}
# API v1 group
curl http://localhost:8080/api/v1/users
# Output: {"users": ["Alice", "Bob"]}
curl -X POST http://localhost:8080/api/v1/users
# Output: {"created": true}Use :name to capture path segments:
// /users/123 -> id = "123"
app.HttpServer.GET("/users/:id", handler)
// /users/42/posts/100 -> userId = "42", postId = "100"
app.HttpServer.GET("/users/:userId/posts/:postId", handler)Get parameter value:
id := ctx.GetRouterName("id")Use *name to capture everything after the prefix:
// /files/path/to/file.txt -> filepath = "/path/to/file.txt"
app.HttpServer.GET("/files/*filepath", handler)Organize routes with common prefix:
// All routes under /api
api := app.HttpServer.Group("/api")
api.GET("/health", healthHandler)
api.GET("/users", listUsersHandler)
// Nested groups
v1 := app.HttpServer.Group("/api/v1")
v1.GET("/users", listUsersV1Handler)Apply middleware to a group:
api := app.HttpServer.Group("/api")
api.Use(authMiddleware) // Apply to all /api/* routes
api.GET("/users", listUsersHandler)| Method | Description |
|---|---|
app.HttpServer.GET(path, handler) |
Register GET route |
app.HttpServer.POST(path, handler) |
Register POST route |
app.HttpServer.PUT(path, handler) |
Register PUT route |
app.HttpServer.DELETE(path, handler) |
Register DELETE route |
app.HttpServer.ANY(path, handler) |
Match all HTTP methods |
app.HttpServer.Group(prefix) |
Create route group |
ctx.GetRouterName(name) |
Get path parameter value |
- Parameters are extracted from the path and can be accessed via
ctx.GetRouterName() - Wildcard captures the rest of the URL including slashes
- Route groups can be nested
- Use
app.SetNotFoundHandle()for custom 404 handling