Defining Routes
To add routes to a package, scaffold a router with the CLI:pkg/rockets/router.go, registers the router in pkg/app/handler.go, and adds it to the package’s wire module. A router looks like this:
pkg/rockets/router.go
http.HandlerFuncs. Anything your handlers need, such as services, a *chttp.JSONReaderWriter, or config, may be added as a field on NewRouterParams, and wire will inject it for you.
Path Parameters
Parameters are declared in the path with{name} and read using chttp.URLParams:
{id:[0-9]+} only matches numeric IDs. Since routing is backed by gorilla/mux, its full pattern syntax is available.
Query Parameters
Query parameters may be read directly from the request:Route Matching
You never need to worry about the order in which routes are declared. Copper will automatically sort them so that deeper paths match first and literal segments take priority over parameters at the same depth. For example,/rockets/featured always wins over /rockets/{id}, while a catch-all such as /{path:.*} matches last.
Registering Routers
copper scaffold:router handles this for you, but it is useful to know where routers live. The app’s HTTP handler is assembled in pkg/app/handler.go:
pkg/app/handler.go
Serving Under a Base Path
If you would like to mount your entire app under a prefix, you may setbase_path in your config:
When
base_path is set, routes that don’t start with the prefix are skipped. You may set RegisterWithBasePath: true on a route to have the prefix joined onto its path instead, which is how Copper’s static file routes work.