Pages plugin: file-based routing
The built-in pages build plugin scans a directory (default pages/) and
generates route registrations from its structure, so the file tree is the
route table:
pages/
index.go // -> /
about.go // -> /about
posts/[id].go // -> /posts/:id
How it works
Before every build the plugin walks the pages directory, derives a route
for each .go file (test and generated files are skipped) and writes a
temporary routes_gen.go in the same package:
// Code generated by pages plugin. DO NOT EDIT.
func init() {
router.RegisterRoute(router.Route{Path: "/", Component: Index})
router.RegisterRoute(router.Route{Path: "/about", Component: About})
router.RegisterRoute(router.Route{Path: "/posts/:id", Component: PostsId})
}
The file is removed again after the build, so it never lands in your
repository.
Naming rules
index.gomaps to the directory root (pages/index.gois/).
[param].gobecomes a:paramroute segment.
- Every file must expose a constructor named after the PascalCase form of
its path, e.g.func About() core.Componentforpages/about.go.
Wiring it up
Enable the plugin in rfw.json and blank-import the generated package so
the init registrations run:
import _ "your/module/pages"
The plugin rebuilds whenever a file under the pages directory changes. A
custom directory can be configured with {"dir": "routes"} in the plugin
configuration.