package lazyroutes

import "golazy.dev/lazyroutes"

Package lazyroutes provides the GoLazy route scope, named route helpers, REST resources, request route metadata, and controller action binding.

In a normal app, package lazyapp creates the root Scope, passes it to the application's Draw function, stores Scope.PathFor in the application context for lazycontroller.Base.PathFor, and passes Scope.RegisterHelpers to lazyview. Those helpers make path_for, link_to, attr, data, and unless_current available in templates. lazyforms also consumes the router's PathFor and PathForModel methods when form helpers build resource actions.

A Scope is also an http.Handler. It wraps an http.ServeMux, stores the registered RouteTable, and attaches matched Route metadata plus named parameter values to each request context. RouteFromRequest and RouteFromContext read that metadata for lower-level integrations.

Drawing routes

HandleFunc registers a route without a controller. Get, Post, Put, Patch, and Delete register a controller constructor and action. The constructor must have the form:

func(context.Context) (*PostsController, error)

Standard actions may be methods such as:

func (c *PostsController) Show(http.ResponseWriter, *http.Request) error

Actions can also ask for request-derived inputs. The action planner resolves http.ResponseWriter, *http.Request, context.Context, route parameters converted to strings, ints, or slices, and values returned by controller methods named GenX. Generator methods may depend on other generated values, and their errors are handled through lazycontroller's error path.

Resources registers conventional REST routes for controller methods named Index, New, Create, Show, Edit, Update, and Delete when those methods exist. Resource custom routes add collection or member actions, nested resources build paths under the parent member path, and Model records the create, update, and delete route names used by PathForModel.

Namespace prefixes the URL path, route name, and view namespace. Path prefixes only the URL path. As prefixes only route names. The namespace value is passed through lazycontroller to lazyview, where it changes view and layout lookup directories for namespaced controllers.

Paths and helpers

PathFor builds URLs from route names and path parameter values. Path parameters are escaped, and lazypath.URLParams values are appended as query parameters. Route names are inferred from the route path when no explicit name is provided: "/" becomes "root", "/posts/{post_id}" becomes "posts", and resource routes use conventional names such as "posts", "post", "new_post", and "edit_post". RegisterHelpers exposes PathFor and safe link helpers to lazyview templates; lazyapp calls it automatically.

The router also understands registered format suffixes from lazycontroller. For example, a request to "/posts/1.json" can match a route registered as "/posts/{post_id}"; the request path passed to the handler is stripped to the logical route path and the format is stored in context for lazycontroller.FormatFromRequest.

The package can be used without lazyapp for small HTTP services, tests, or custom application shells:

router := lazyroutes.New(context.Background())
router.HandleFunc(http.MethodGet, "/health", func(w http.ResponseWriter, r *http.Request) error {
	_, err := w.Write([]byte("ok"))
	return err
})
http.ListenAndServe(":3000", router)

For a full GoLazy application, lazyapp layers lazydispatch around the router, adds public-file and asset fallbacks, installs lazycontroller rendering, and registers framework helpers before templates are cached.

Development hooks

RegisterLazyDevHandlers exists only in files built with the lazydev build tag. lazyapp registers it on the lazycontrolplane.ControlPlane in development builds. The current endpoint is GET /routes, which returns the registered RouteTable as JSON for the development panel; production builds do not expose that route.

Constants

Functions

Types

type Resource

type Resource struct {
	// contains filtered or unexported fields
}
func (r *Resource) Delete
func (r *Resource) Delete(path string, action any)
func (r *Resource) Get
func (r *Resource) Get(path string, action any)
func (r *Resource) MemberDelete
func (r *Resource) MemberDelete(path string, action any)
func (r *Resource) MemberGet
func (r *Resource) MemberGet(path string, action any)
func (r *Resource) MemberPatch
func (r *Resource) MemberPatch(path string, action any)
func (r *Resource) MemberPost
func (r *Resource) MemberPost(path string, action any)
func (r *Resource) MemberPut
func (r *Resource) MemberPut(path string, action any)
func (r *Resource) Model
func (r *Resource) Model(models ...any) *Resource
func (r *Resource) Param
func (r *Resource) Param(name string) *Resource
func (r *Resource) Patch
func (r *Resource) Patch(path string, action any)
func (r *Resource) Path
func (r *Resource) Path(path string) *Resource
func (r *Resource) Plural
func (r *Resource) Plural(name string) *Resource
func (r *Resource) Post
func (r *Resource) Post(path string, action any)
func (r *Resource) Put
func (r *Resource) Put(path string, action any)
func (r *Resource) Resources
func (r *Resource) Resources(controller any, configure ...func(*Resource)) *Resource
func (r *Resource) Singular
func (r *Resource) Singular(name string) *Resource

type Route

Route is the metadata for one registered route.

type Route struct {
	Method		string		`json:"method"`
	Path		string		`json:"path"`
	Name		string		`json:"name"`
	Controller	string		`json:"controller,omitempty"`
	Action		string		`json:"action,omitempty"`
	Namespace	string		`json:"namespace,omitempty"`
	NamedParams	map[string]bool	`json:"params"`
}
func RouteFromContext

RouteFromContext returns the route metadata and parameter values attached to a request context.

func RouteFromContext(ctx context.Context) (Route, map[string]string, bool)
func RouteFromRequest

RouteFromRequest returns the route metadata and parameter values attached to the request context.

func RouteFromRequest(r *http.Request) (Route, map[string]string, bool)

type Scope

Scope is the routing DSL entrypoint used by application routes. It embeds the standard library ServeMux so the same object is directly usable as an http.Handler.

type Scope struct {
	*http.ServeMux
	Context	context.Context
	Routes	RouteTable
	// contains filtered or unexported fields
}
func New

New builds a scope with the framework's public-file fallback already wired.

func New(ctx context.Context) *Scope
func (s *Scope) As

As creates a child scope with a route-name prefix.

func (s *Scope) As(name string, draw ...func(*Scope)) *Scope
func (s *Scope) Delete
func (s *Scope) Delete(path string, controller any, action any)
func (s *Scope) Get
func (s *Scope) Get(path string, controller any, action any)
func (s *Scope) HandleFunc

HandleFunc registers a non-controller route action.

func (s *Scope) HandleFunc(method, path string, handlerAction Action)
func (s *Scope) HandlesPath
func (s *Scope) HandlesPath(path string) bool
func (s *Scope) ModelRoutesFor
func (s *Scope) ModelRoutesFor(model any) (ModelRoutes, bool)
func (s *Scope) Namespace

Namespace creates a child scope with path, route-name, and namespace prefixes.

func (s *Scope) Namespace(name string, draw ...func(*Scope)) *Scope
func (s *Scope) Patch
func (s *Scope) Patch(path string, controller any, action any)
func (s *Scope) Path

Path creates a child scope with a path prefix.

func (s *Scope) Path(path string, draw ...func(*Scope)) *Scope
func (s *Scope) PathFor

PathFor builds a path from a named route and route parameter values.

func (s *Scope) PathFor(name string, values ...any) (string, error)
func (s *Scope) PathForModel
func (s *Scope) PathForModel(model any, action string) (string, error)
func (s *Scope) Post
func (s *Scope) Post(path string, controller any, action any)
func (s *Scope) Put
func (s *Scope) Put(path string, controller any, action any)
func (s *Scope) RegisterHelpers

RegisterHelpers returns template helpers provided by the router.

func (s *Scope) RegisterHelpers() map[string]any
func (s *Scope) Resources
func (s *Scope) Resources(controller any, configure ...func(*Resource)) *Resource
func (s *Scope) ServeHTTP
func (s *Scope) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
lazyroutes/actioncall Package actioncall compiles and invokes controller action call plans for lazyroutes.