golazy.dev golazy.dev / lazycontroller Index | Files

package lazycontroller

import "golazy.dev/lazycontroller"

Package lazycontroller provides request-local controller state, rendering helpers, response helpers, typed HTTP errors, redirects, content negotiation, SEO convenience methods, and Server-Sent Events entrypoints.

In a normal GoLazy app, lazyapp creates a renderer, stores it in context, and lazyroutes binds a fresh controller instance to each request. Controllers then embed Base, call Set before rendering, return errors for framework error handling, and let empty successful actions render the matching view automatically.

The package can also be used without lazyapp. Create a Renderer from an fs.FS, put it in context with WithRenderer, construct Base with NewBase, and bind each request with BindRequest before rendering.

Functions

func Error

func Error(status int, err error) error

func ErrorHandler

func ErrorHandler(ctx context.Context) func(http.Handler) http.Handler

func RegisterFormat

RegisterFormat maps one or more content types to a controller format.

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 ReportError

func ReportError(r *http.Request, controller any, err error) bool

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 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) Alternate
func (b *Base) Alternate(language, url string) *lazyseo.Meta
func (b *Base) Author
func (b *Base) Author(value string) *lazyseo.Meta
func (b *Base) BindRequest
func (b *Base) BindRequest(w http.ResponseWriter, r *http.Request, route lazyview.Route) error
func (b *Base) Canonical
func (b *Base) Canonical(value string) *lazyseo.Meta
func (b *Base) ContentType

ContentType sets the response Content-Type header.

func (b *Base) ContentType(value string)
func (b *Base) Decode

Decode parses the current request form and decodes submitted fields into target.

func (b *Base) Decode(target any) error
func (b *Base) Description
func (b *Base) Description(value string) *lazyseo.Meta
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) JSONLD
func (b *Base) JSONLD(value any) *lazyseo.Meta
func (b *Base) Kind
func (b *Base) Kind(kind lazyseo.PageKind) *lazyseo.Meta
func (b *Base) Language
func (b *Base) Language(value string) *lazyseo.Meta
func (b *Base) LastUpdated
func (b *Base) LastUpdated(value time.Time) *lazyseo.Meta
func (b *Base) Layout
func (b *Base) Layout(layout string)
func (b *Base) Locale
func (b *Base) Locale(value string) *lazyseo.Meta
func (b *Base) Metadata
func (b *Base) Metadata(model any) *lazyseo.Meta
func (b *Base) MustPathFor

MustPathFor builds a path and panics when the route cannot be generated.

func (b *Base) MustPathFor(name string, values ...any) string
func (b *Base) NoLayout
func (b *Base) NoLayout()
func (b *Base) OpenGraph
func (b *Base) OpenGraph(value lazyseo.OpenGraph) *lazyseo.Meta
func (b *Base) OpenGraphType
func (b *Base) OpenGraphType(value string) *lazyseo.Meta
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) PublishedTime
func (b *Base) PublishedTime(value time.Time) *lazyseo.Meta
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) RenderHTML
func (b *Base) RenderHTML(view string) error
func (b *Base) RenderSVGString
func (b *Base) RenderSVGString(view string, variants ...string) (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) SEO
func (b *Base) SEO(options ...lazyseo.Option) *lazyseo.Meta
func (b *Base) SEOImage
func (b *Base) SEOImage(value string) *lazyseo.Meta
func (b *Base) SEOImageAlt
func (b *Base) SEOImageAlt(value string) *lazyseo.Meta
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) SchemaType
func (b *Base) SchemaType(value string) *lazyseo.Meta
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) Title
func (b *Base) Title(value string) *lazyseo.Meta
func (b *Base) TwitterCard
func (b *Base) TwitterCard(value lazyseo.TwitterCard) *lazyseo.Meta
func (b *Base) TwitterCardType
func (b *Base) TwitterCardType(value string) *lazyseo.Meta
func (b *Base) Type
func (b *Base) Type(value string) *lazyseo.Meta
func (b *Base) URL
func (b *Base) URL(value string) *lazyseo.Meta
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) Variants
func (b *Base) Variants(variants ...string)
func (b *Base) Wants

Wants runs the response handler matching the request format.

func (b *Base) Wants(formats Formats) 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 PathForFunc

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

type PathForFunc func(name string, values ...any) (string, error)

type RequestResetter

RequestResetter clears request-specific references before a controller returns to a pool.

type RequestResetter interface {
	ResetRequest()
}