Documentation
¶
Overview ¶
Package web is a web framework for Vuka: routes, views and services are declared with decorators, and web.New gathers them into an app.
@web.Get("/pets/{id}")
@web.Template("pet.html")
func ShowPet(id int, store *PetStore) Result[Pet] { … }
func main() { web.New().Run(":8080") }
Everything a decorator declares can also be registered from plain Go (App.Get, App.Provide, …); see the README.
Index ¶
- Variables
- func Controller(prefix string) func(*vuka.Type)
- func Delete(path string) func(*vuka.Decl)
- func Get(path string) func(*vuka.Decl)
- func Handle(method, path string) func(*vuka.Decl)
- func Patch(path string) func(*vuka.Decl)
- func Post(path string) func(*vuka.Decl)
- func Put(path string) func(*vuka.Decl)
- func Request(ctx context.Context) *http.Request
- func Resolve[T any](a *App) (T, error)
- func ResponseWriter(ctx context.Context) http.ResponseWriter
- func Service(t *vuka.Type)
- func Template(f vuka.File, name ...string) func(*vuka.Decl)
- type App
- func (a *App) Build() error
- func (a *App) Delete(path string, fn any, opts ...RouteOption) *App
- func (a *App) Get(path string, fn any, opts ...RouteOption) *App
- func (a *App) Handle(method, path string, fn any, opts ...RouteOption) *App
- func (a *App) Handler() http.Handler
- func (a *App) Patch(path string, fn any, opts ...RouteOption) *App
- func (a *App) Post(path string, fn any, opts ...RouteOption) *App
- func (a *App) Provide(ctors ...any) *App
- func (a *App) Put(path string, fn any, opts ...RouteOption) *App
- func (a *App) Run(addr string) error
- func (a *App) Start(ctx context.Context) error
- func (a *App) Static(prefix string, fsys fs.FS) *App
- func (a *App) Stop(ctx context.Context) error
- func (a *App) Use(mw ...Middleware) *App
- type Error
- type Middleware
- type Option
- type Redirect
- type RouteOption
- type Router
- type ServeMux
- type Starter
- type Stopper
- type Uses
Constants ¶
This section is empty.
Variables ¶
var ( NotFound = &Error{Status: http.StatusNotFound, Message: "not found"} Forbidden = &Error{Status: http.StatusForbidden, Message: "forbidden"} Conflict = &Error{Status: http.StatusConflict, Message: "conflict"} )
Functions ¶
func Controller ¶
Controller makes a struct a service whose route methods are served under prefix.
func Handle ¶
Handle declares a route for method and path: @web.Handle("OPTIONS", "/x"). Path segments in braces bind parameters of the same name: "/pets/{id}"; "{rest...}" matches the rest of the path.
func Request ¶
Request is the request being served, from a context.Context a handler received: call decorators reach it through c.Context().
func ResponseWriter ¶
func ResponseWriter(ctx context.Context) http.ResponseWriter
ResponseWriter is the response being written, from a handler's context.
func Service ¶
Service makes a struct a dependency: a lazy singleton built from its fields, each injected by type.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is a web application: the routes, services and controllers declared with decorators, and whatever was added in code.
func New ¶
New makes an app of every route, service and controller declared so far (each app takes its own snapshot, with its own singletons).
func (*App) Build ¶
Build resolves the app: every route's parameters, results and view, and the dependency graph. It reports every problem at once, before anything is served; Handler, Start and Run call it.
func (*App) Handle ¶
func (a *App) Handle(method, path string, fn any, opts ...RouteOption) *App
Handle registers fn for method and path, bound as a decorated handler is.
func (*App) Handler ¶
Handler is the app as an http.Handler, to serve or mount anywhere. It panics when Build fails.
func (*App) Run ¶
Run serves the app on addr (":" + $PORT, else ":8080", when empty) until SIGINT or SIGTERM, then shuts down gracefully and stops the services.
func (*App) Start ¶
Start builds every dependency the routes need, then calls Start on each provided value implementing Starter, in dependency order.
func (*App) Stop ¶
Stop calls Stop on each built value implementing Stopper, in reverse order of construction.
func (*App) Use ¶
func (a *App) Use(mw ...Middleware) *App
Use adds app-wide middleware; the first added runs first.
type Error ¶
Error is an error with an HTTP status. Returned by a handler (wrapped or not), it answers with that status and message: an HTML page for an HTML route, JSON otherwise.
type Middleware ¶
Middleware wraps an http.Handler.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures an App.
func Dev ¶
func Dev() Option
Dev shows the messages of internal errors in responses; so does the environment variable VUKA_ENV=development.
func Only ¶
Only limits the declared routes, services and controllers an app takes to those of the given packages (import paths; "main" for the main package).
func Provide ¶
Provide adds constructors to the app's container: func(deps…) T or func(deps…) (T, error). A value that isn't a function is provided as is.
func WithRouter ¶
WithRouter serves the app's routes with r instead of net/http's ServeMux.
type Redirect ¶
type Redirect string
Redirect, returned by a handler as its result or as its error, redirects to the URL: 302 for GET and HEAD, 303 See Other otherwise. It is also a vuka.Node, so a handler returning a page can redirect instead.
type RouteOption ¶
type RouteOption func(*routeSpec)
RouteOption configures a route registered in code.
func Params ¶
func Params(names ...string) RouteOption
Params names a handler's parameters in order, which reflection can't see: scalar parameters bind by name.
func View ¶
func View(f vuka.File, name ...string) RouteOption
View renders the route's result with a view, as @web.Template does.
func Wrap ¶
func Wrap(mw ...Middleware) RouteOption
Wrap adds middleware to the route, as @web.Use does.
type Router ¶
type Router interface {
http.Handler
Handle(method, pattern string, h http.Handler)
Param(r *http.Request, name string) string
}
Router matches requests to routes. Patterns use the canonical syntax: "/pets/{id}" binds a segment, "/files/{path...}" the rest of the path, and a pattern matches its path exactly. Param returns a bound value for a request the router dispatched.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is the default Router: net/http's ServeMux with Go 1.22 patterns.
func NewServeMux ¶
func NewServeMux() *ServeMux
type Starter ¶
Starter is a provided value with work to do when the app starts: Run calls Start in dependency order.
type Stopper ¶
Stopper is a provided value with work to do when the app stops: Stop is called in the reverse order of construction.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
pets/model
Package model holds the pets app's data types, shared by its handlers and its views.
|
Package model holds the pets app's data types, shared by its handlers and its views. |