Basic Routing
Createroute.go files to define API endpoints:
This creates:
app/api/users/route.go→/api/usersapp/api/posts/route.go→/api/postsapp/route.go→/
Handler Functions
Export functions named after HTTP methods:All handlers must have the signature
func(c *fuego.Context) error. Invalid signatures are skipped with a warning.Dynamic Routes
Use[param] folders for dynamic segments:
This creates:
/api/users- static route/api/users/:id- dynamic route
Multiple Parameters
Maps to/api/orgs/:orgId/teams/:teamId
Catch-All Routes
Use[...param] for catch-all routes:
Maps to /api/docs/*
/api/docs/hello→slug = "hello"/api/docs/2024/01/my-post→slug = "2024/01/my-post"
Optional Catch-All
Use[[...param]] for optional catch-all (matches with or without segments):
Matches:
/api/shop→ categories =""/api/shop/electronics→ categories ="electronics"/api/shop/electronics/phones→ categories ="electronics/phones"
Route Groups
Use(groupname) folders to organize without affecting URLs:
Creates:
/api/about(not/api/marketing/about)/api/blog/api/products/api/cart
Private Folders
Folders starting with_ are ignored:
The _components and _utils folders are ignored by the router.
Route Priority
Routes are matched in order of specificity:-
Static routes (highest priority)
/api/users/mematches before/api/users/:id
-
Dynamic routes
/api/users/:idmatches after static routes
-
Catch-all routes (lowest priority)
/docs/*matches last
Viewing Routes
Use the CLI to list all routes:--json for machine-readable output:
Handler Signature
All handlers must have this signature:Complete Example
Routes created:| Method | Route | File |
|---|---|---|
| GET | / | app/page.templ |
| GET | /api/health | app/api/health/route.go |
| GET,POST | /api/users | app/api/users/route.go |
| GET,PUT,DELETE | /api/users/:id | app/api/users/[id]/route.go |
| GET | /api/posts/* | app/api/posts/[...slug]/route.go |
| GET,PUT | /api/settings | app/api/(admin)/settings/route.go |