Documentation
¶
Overview ¶
Package router provides Laravel-style declarative routing that is independent of any HTTP framework. You declare routes once; each adapter (Gin/Fiber/Echo/Chi/net/http) mounts them with a one-liner.
Example:
r := router.New()
r.Get("/health", healthHandler)
r.Resource("posts", postsCtrl)
r.Group("/api/v1", func(g *router.Router) {
g.Use(authMiddleware)
g.Resource("users", usersCtrl)
})
Then mount onto your framework:
r.MountStd(http.NewServeMux()) // net/http
r.Apply(func(method, path string, h http.HandlerFunc) {
ginEngine.Handle(method, path, gin.WrapF(h))
})
Index ¶
- type Handler
- type Middleware
- type ResourceController
- type Route
- type Router
- func (r *Router) APIResource(name string, ctrl ResourceController)
- func (r *Router) Any(path string, h Handler)
- func (r *Router) Apply(register func(method, path string, h Handler))
- func (r *Router) Delete(path string, h Handler)
- func (r *Router) Get(path string, h Handler)
- func (r *Router) Group(prefix string, fn func(g *Router))
- func (r *Router) Head(path string, h Handler)
- func (r *Router) MountStd(mux *http.ServeMux)
- func (r *Router) Options(path string, h Handler)
- func (r *Router) Patch(path string, h Handler)
- func (r *Router) Post(path string, h Handler)
- func (r *Router) Put(path string, h Handler)
- func (r *Router) Resource(name string, ctrl ResourceController)
- func (r *Router) Routes() []Route
- func (r *Router) Use(mw ...Middleware)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler = http.HandlerFunc
Handler is the framework-agnostic handler signature. It is identical to http.HandlerFunc — controllers generated by `lago make:controller` already conform.
type Middleware ¶
Middleware wraps a Handler. Middleware compose left-to-right.
func Chain ¶
func Chain(mw ...Middleware) Middleware
Chain composes middleware as a single Middleware. Useful when you want to apply a stable bundle (logging + recovery + auth) in one call.
type ResourceController ¶
type ResourceController interface {
Index(w http.ResponseWriter, r *http.Request)
Show(w http.ResponseWriter, r *http.Request)
Store(w http.ResponseWriter, r *http.Request)
Update(w http.ResponseWriter, r *http.Request)
Destroy(w http.ResponseWriter, r *http.Request)
}
ResourceController is the contract for `r.Resource("posts", ctrl)`. Implementations may leave any method as a no-op; the router only wires the methods present.
type Route ¶
Route is one row in the route table. It is exposed so adapters can introspect (e.g. for printing the route list).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router collects routes, groups, and middleware in declaration order. It is not goroutine-safe — declare routes from a single goroutine at startup, then mount onto your server.
func (*Router) APIResource ¶
func (r *Router) APIResource(name string, ctrl ResourceController)
APIResource is identical to Resource — kept as a convenience alias to match the user-facing Laravel naming convention.
func (*Router) Apply ¶
Apply invokes register for every route. Frameworks register routes via a one-line callback:
r.Apply(func(method, path string, h http.HandlerFunc) {
ginEngine.Handle(method, path, gin.WrapF(h))
})
func (*Router) Get ¶
HTTP verb shortcuts. Each registers a route with the router's prefix and applies its middleware chain to the handler.
func (*Router) Group ¶
Group runs the callback against a child router with the given path prefix. Middleware on the parent applies to the child; new Use() calls inside fn add to the child only.
func (*Router) MountStd ¶
MountStd registers every route on a net/http ServeMux using the "METHOD /path" syntax introduced in Go 1.22.
func (*Router) Resource ¶
func (r *Router) Resource(name string, ctrl ResourceController)
Resource registers the five canonical RESTful routes for a controller:
GET /posts → ctrl.Index
GET /posts/{id} → ctrl.Show
POST /posts → ctrl.Store
PUT /posts/{id} → ctrl.Update
PATCH /posts/{id} → ctrl.Update
DELETE /posts/{id} → ctrl.Destroy
The `name` argument is the plural resource name (`posts`, `users`, …).
func (*Router) Routes ¶
Routes returns a snapshot of every registered route in declaration order. Useful for printing the route table at startup.
func (*Router) Use ¶
func (r *Router) Use(mw ...Middleware)
Use installs middleware that wraps every subsequent handler registered on this router (and its children via Group).