package lazycontroller
import "golazy.dev/lazycontroller"
Functions
func Error
func Error(status int, err error) error
func ErrorHandler
func ErrorHandler(ctx context.Context) func(http.Handler) http.Handler
func PanicError
func PanicError(recovered any) error
func RegisterFormat
func RegisterFormat(format Format, contentTypes ...string)
func RegisterFormatSuffix
RegisterFormatSuffix maps one or more URL suffixes to a controller format.
func RegisterFormatSuffix(format Format, suffixes ...string)
func ReportController
func ReportController(r *http.Request, controller any) bool
func ReportError
func ReportError(r *http.Request, controller any, err error) bool
func ResetResponse
func ResetResponse(w http.ResponseWriter) bool
func StatusCode
func StatusCode(err error) int
func WasResponseSent
func WasResponseSent(w http.ResponseWriter) bool
func WithErrorPages
func WithErrorPages(ctx context.Context, files fs.FS) context.Context
func WithFormat
WithFormat returns a context that carries an explicitly requested format.
func WithFormat(ctx context.Context, format Format) context.Context
func WithPathFor
WithPathFor returns a context carrying the application's route path helper.
func WithPathFor(ctx context.Context, pathFor PathForFunc) context.Context
func WithRenderer
func WithRenderer(ctx context.Context, renderer *Renderer) context.Context
func WriteError
func WriteError(w http.ResponseWriter, _ *http.Request, err error)
func WriteErrorFallback
func WriteErrorFallback(ctx context.Context, w http.ResponseWriter, r *http.Request) bool
func WriteErrorPage
func WriteErrorPage(ctx context.Context, w http.ResponseWriter, r *http.Request, status int) bool
func WriteFile
func WriteFile(ctx context.Context, w http.ResponseWriter, r *http.Request, file string, status int) error
Types
type Base
type Base struct {
// contains filtered or unexported fields
}
func NewBase
func NewBase(ctx context.Context, viewPath ...string) (Base, error)
func (b *Base) BindRequest
func (b *Base) BindRequest(w http.ResponseWriter, r *http.Request, route lazyview.Route) error
func (b *Base) ContentType
ContentType sets the response Content-Type header.
func (b *Base) ContentType(value string)
func (b *Base) Format
Format returns the negotiated response format for the current request.
func (b *Base) Format() Format
func (b *Base) HandleError
func (b *Base) HandleError(w http.ResponseWriter, r *http.Request, err error) error
func (b *Base) Header
Header returns the response header map for the current controller request.
func (b *Base) Header() http.Header
func (b *Base) Helper
func (b *Base) Helper(name string, helper any)
func (b *Base) Helpers
func (b *Base) Helpers(helpers map[string]any)
func (b *Base) Is
Is reports whether the current request resolved to format.
func (b *Base) Is(format Format) bool
func (b *Base) IsGIF
func (b *Base) IsGIF() bool
func (b *Base) IsHTML
func (b *Base) IsHTML() bool
func (b *Base) IsImage
func (b *Base) IsImage() bool
func (b *Base) IsJPEG
func (b *Base) IsJPEG() bool
func (b *Base) IsJSON
func (b *Base) IsJSON() bool
func (b *Base) IsPNG
func (b *Base) IsPNG() bool
func (b *Base) IsSSE
func (b *Base) IsSSE() bool
func (b *Base) IsTurboFrame
func (b *Base) IsTurboFrame() bool
func (b *Base) IsTurboStream
func (b *Base) IsTurboStream() bool
func (b *Base) Layout
func (b *Base) Layout(layout string)
func (b *Base) NoLayout
func (b *Base) NoLayout()
func (b *Base) PathFor
PathFor builds a path from a named route and route parameter values.
func (b *Base) PathFor(name string, values ...any) (string, error)
func (b *Base) Redirect
Redirect sends an HTTP redirect and marks the controller response as written. It defaults to 302 Found. Pass a single 3xx status such as http.StatusMovedPermanently or http.StatusSeeOther to override it.
func (b *Base) Redirect(location string, status ...int) error
func (b *Base) RedirectBack
RedirectBack is an alias for RedirectBackOrTo.
func (b *Base) RedirectBack(fallbackLocation string, status ...int) error
func (b *Base) RedirectBackOrTo
RedirectBackOrTo redirects to the same-host Referer header when present, otherwise it redirects to fallbackLocation.
func (b *Base) RedirectBackOrTo(fallbackLocation string, status ...int) error
func (b *Base) RedirectTo
RedirectTo is an alias for Redirect for controllers that prefer Rails-style wording in action code.
func (b *Base) RedirectTo(location string, status ...int) error
func (b *Base) Render
func (b *Base) Render(view string) error
func (b *Base) RenderTurboFrame
func (b *Base) RenderTurboFrame(id string, opts ...lazyturbo.FrameOption) error
func (b *Base) Request
func (b *Base) Request() *http.Request
func (b *Base) ResetRequest
func (b *Base) ResetRequest()
func (b *Base) Respond
Respond runs the response handler matching the request format.
func (b *Base) Respond(responses Responses) error
func (b *Base) ReturnFile
func (b *Base) ReturnFile(file string, status int) error
func (b *Base) SSEStream
SSEStream starts a Server-Sent Events response for the current controller request.
func (b *Base) SSEStream(opts ...lazysse.Option) (*lazysse.Stream, error)
func (b *Base) ServeErrorPage
func (b *Base) ServeErrorPage(w http.ResponseWriter, r *http.Request, status int) bool
func (b *Base) Set
func (b *Base) Set(name string, value any)
func (b *Base) SetLayout
func (b *Base) SetLayout(layout string)
func (b *Base) SetTurboFrameOptions
func (b *Base) SetTurboFrameOptions(opts ...lazyturbo.FrameOption)
func (b *Base) Status
Status sets the HTTP status code used by the next controller render.
It does not write the response immediately, so actions can still rely on automatic rendering after setting a non-200 status.
func (b *Base) Status(code int)
func (b *Base) URLFrom
URLFrom returns location when it is safe to use as an internal redirect target for the current request. Absolute URLs must match the request host; relative URLs must be absolute paths such as "/posts".
func (b *Base) URLFrom(location string) string
func (b *Base) Wants
Wants runs the response handler matching the request format.
func (b *Base) Wants(formats Formats) error
type BeforeAction
BeforeAction runs after request binding and before the routed action.
type BeforeAction interface {
BeforeAction() error
}
type Format
Format identifies the response representation selected for a request.
type Format string
func FormatFromContentType
FormatFromContentType resolves a content type to a registered format.
func FormatFromContentType(contentType string) (Format, bool)
func FormatFromRequest
FormatFromRequest returns the request format inferred from Turbo headers, explicit route metadata, and Accept.
func FormatFromRequest(r *http.Request) Format
func FormatFromSuffix
FormatFromSuffix resolves a URL suffix to a registered format.
func FormatFromSuffix(suffix string) (Format, bool)
func NewFormat
NewFormat registers a custom MIME type and returns its symbolic format.
func NewFormat(contentType string, options ...FormatOption) Format
type FormatOption
FormatOption configures a custom format created by NewFormat.
type FormatOption func(*newFormatOptions)
func As
As sets the symbolic format name returned by NewFormat.
func As(name string) FormatOption
func Suffix
Suffix registers one or more URL suffixes for a custom format.
func Suffix(suffixes ...string) FormatOption
type Formats
type Formats map[Format]func() error
type HTTPError
type HTTPError struct {
Status int
Err error
}
func (e *HTTPError) Error
func (e *HTTPError) Error() string
func (e *HTTPError) Unwrap
func (e *HTTPError) Unwrap() error
type PathForFunc
PathForFunc builds a path from a named route and route parameter values.
type PathForFunc func(name string, values ...any) (string, error)
type Renderer
Renderer is the application view renderer.
type Renderer = lazyview.Views
func NewRenderer
func NewRenderer(views fs.FS) (*Renderer, error)
type RequestBinder
RequestBinder prepares a controller instance for one request.
type RequestBinder interface {
BindRequest(http.ResponseWriter, *http.Request, lazyview.Route) error
}
type RequestResetter
RequestResetter clears request-specific references before a controller returns to a pool.
type RequestResetter interface {
ResetRequest()
}
type Responses
type Responses = Formats
RegisterFormat maps one or more content types to a controller format.