Routing
Controller Routes
Connect HTTP verbs to request-local controller actions.
Bind a verb
Controller routes use the route scope verb methods:
func Draw(router *lazyroutes.Scope) {
router.Get("/posts", posts.New, (*posts.PostsController).Index)
router.Post("/posts", posts.New, (*posts.PostsController).Create)
router.Get("/posts/{post_id}", posts.New, (*posts.PostsController).Show)
}
Each route receives a constructor and a method expression. GoLazy constructs a fresh controller for each request.
Read path values
Named path values use the standard request API:
func (c *PostsController) Show(
_ http.ResponseWriter,
r *http.Request,
) error {
postID := r.PathValue("post_id")
c.Set("postID", postID)
return nil
}
Use this shape for most HTML routes. Use generator arguments only when typed arguments remove useful boilerplate.
Use route helpers
Route names are inferred from method and path. Templates can use path_for:
<a href="{{path_for "posts"}}">Posts</a>
<a href="{{path_for "post" .post.Param}}">{{.post.Title}}</a>
Controller route metadata is attached to the request before the action runs, so helpers and rendering know the current route, controller, action, and namespace.
Split plain handlers when useful
Use a plain handler route for endpoints that do not need a controller:
router.HandleFunc(http.MethodGet, "/up", func(w http.ResponseWriter, _ *http.Request) error {
w.WriteHeader(http.StatusNoContent)
return nil
})
Use HandlerFunc Routes for those endpoints.