Documentation
¶
Overview ¶
Package controller is a pure-Go, CGO-free port of the AbstractController and ActionController::Metal dispatch core: a controller class model with action dispatch, before/after/around filters (with only/except/if/unless conditions), rescue_from error handling, and render/redirect_to/head as seams over the deferred view layer.
Action bodies and view rendering are Ruby seams. An action is either a registered ActionFunc or is handled by the class-wide RunAction seam (mapping to Rails' `def <action>; ...; end`). Rendering is produced by an optional Renderer seam; without one, render records its options and writes its :plain body, leaving template rendering to a later ActionView port.
Index ¶
- type ActionFunc
- type ActionNotFound
- type Base
- func (b *Base) Action() string
- func (b *Base) Class() *Class
- func (b *Base) Head(status int) error
- func (b *Base) Params() *parameters.Parameters
- func (b *Base) Performed() bool
- func (b *Base) Process(action string) error
- func (b *Base) RedirectTo(url string, status ...int) error
- func (b *Base) Render(opts RenderOptions) error
- func (b *Base) RenderPlain(text string) error
- func (b *Base) Request() *dispatch.Request
- func (b *Base) Response() *dispatch.Response
- func (b *Base) SetParams(p *parameters.Parameters)
- type Class
- func (cl *Class) Action(name string, fn ActionFunc) *Class
- func (cl *Class) AfterAction(fn ActionFunc, opts ...FilterOption) *Class
- func (cl *Class) AroundAction(fn func(c *Base, yield func() error) error, opts ...FilterOption) *Class
- func (cl *Class) BeforeAction(fn ActionFunc, opts ...FilterOption) *Class
- func (cl *Class) New(req *dispatch.Request, resp *dispatch.Response, params *parameters.Parameters) *Base
- func (cl *Class) RescueFrom(match func(err error) bool, handle func(c *Base, err error) error) *Class
- func (cl *Class) RescueFromIs(target error, handle func(c *Base, err error) error) *Class
- func (cl *Class) RunAction(fn func(c *Base, action string) error) *Class
- func (cl *Class) SetRenderer(r Renderer) *Class
- type DoubleRenderError
- type FilterOption
- type RenderOptions
- type Renderer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionFunc ¶
ActionFunc is the body of a controller action (a seam for Ruby code).
type ActionNotFound ¶
ActionNotFound is returned by Process when the action has no registered ActionFunc and the class has no RunAction seam, the analogue of AbstractController::ActionNotFound.
func (*ActionNotFound) Error ¶
func (e *ActionNotFound) Error() string
type Base ¶
type Base struct {
// Rendered records the options of the render that produced the response,
// or nil when the response came from redirect_to/head.
Rendered *RenderOptions
// RedirectedTo records the target of a redirect_to, or "".
RedirectedTo string
// contains filtered or unexported fields
}
Base is a controller instance handling one request.
func (*Base) Head ¶
Head sets an empty response with the given status, marking the controller performed. Performing twice returns *DoubleRenderError.
func (*Base) Params ¶
func (b *Base) Params() *parameters.Parameters
Params returns the request parameters.
func (*Base) Performed ¶
Performed reports whether a render/redirect/head has produced the response.
func (*Base) Process ¶
Process dispatches the action through the filter chain and rescue handlers, mirroring ActionController::Metal#process. It returns the (possibly rescued) error from the action or a filter.
func (*Base) RedirectTo ¶
RedirectTo sets a Location header and a redirect status (302 by default), marking the controller performed. Redirecting after performing returns *DoubleRenderError.
func (*Base) Render ¶
func (b *Base) Render(opts RenderOptions) error
Render writes a response body per opts, marking the controller performed. A Renderer seam, if installed, produces the body; otherwise the Plain option is used. Rendering twice returns *DoubleRenderError.
func (*Base) RenderPlain ¶
RenderPlain is the shorthand for Render(RenderOptions{Plain: text}).
func (*Base) SetParams ¶
func (b *Base) SetParams(p *parameters.Parameters)
SetParams replaces the parameters (e.g. after require/permit).
type Class ¶
type Class struct {
Name string
// contains filtered or unexported fields
}
Class is the controller class: the shared configuration (name, action seams, filter chains, rescuers, renderer) from which per-request Base instances are created. It is the union of AbstractController::Base and ActionController::Metal in one value.
func RescueType ¶
RescueType registers a handler for errors of the concrete type T (matched with errors.As), the closest Go analogue of rescue_from SomeError. Example: RescueType[*parameters.ParameterMissing](cl, handler).
func (*Class) Action ¶
func (cl *Class) Action(name string, fn ActionFunc) *Class
Action registers the body seam for a named action, returning the class for chaining.
func (*Class) AfterAction ¶
func (cl *Class) AfterAction(fn ActionFunc, opts ...FilterOption) *Class
AfterAction registers an after filter.
func (*Class) AroundAction ¶
func (cl *Class) AroundAction(fn func(c *Base, yield func() error) error, opts ...FilterOption) *Class
AroundAction registers an around filter. The filter must call yield to run the remaining chain and the action; not calling yield skips them.
func (*Class) BeforeAction ¶
func (cl *Class) BeforeAction(fn ActionFunc, opts ...FilterOption) *Class
BeforeAction registers a before filter, returning the class for chaining. A before filter that renders or redirects halts the chain.
func (*Class) New ¶
func (cl *Class) New(req *dispatch.Request, resp *dispatch.Response, params *parameters.Parameters) *Base
New builds a controller instance for a request. Any of req/resp/params may be nil; a nil response is replaced with a fresh empty one and nil params with an empty set.
func (*Class) RescueFrom ¶
func (cl *Class) RescueFrom(match func(err error) bool, handle func(c *Base, err error) error) *Class
RescueFrom registers a handler for errors matching match. Later registrations take precedence, mirroring rescue_from's last-declared-wins ordering.
func (*Class) RescueFromIs ¶
RescueFromIs registers a handler for errors that wrap the sentinel target (matched with errors.Is).
func (*Class) RunAction ¶
RunAction sets the fallback dispatch seam invoked for any action without a registered ActionFunc. This is the primary Ruby seam: it receives the Base (params, request, response, and the render/redirect/head methods) and the action name, mirroring ActionController's `process(action)` reaching the Ruby action method.
func (*Class) SetRenderer ¶
SetRenderer installs the view-layer seam.
type DoubleRenderError ¶
type DoubleRenderError struct{}
DoubleRenderError is returned when a render/redirect/head is attempted after the response was already produced, the analogue of AbstractController::DoubleRenderError.
func (*DoubleRenderError) Error ¶
func (e *DoubleRenderError) Error() string
type FilterOption ¶
type FilterOption func(*filter)
FilterOption constrains when a filter runs.
func Except ¶
func Except(actions ...string) FilterOption
Except runs a filter for every action but the listed ones.
func If ¶
func If(cond func(c *Base) bool) FilterOption
If runs a filter only when cond returns true.
func Only ¶
func Only(actions ...string) FilterOption
Only restricts a filter to the listed actions.
func Unless ¶
func Unless(cond func(c *Base) bool) FilterOption
Unless runs a filter only when cond returns false.
type RenderOptions ¶
type RenderOptions struct {
// Template names a view template ("posts/show").
Template string
// Action names an action whose template to render (defaults to the
// current action when a Renderer resolves templates).
Action string
// Plain is an inline text/plain body.
Plain string
// JSON, when non-nil, is a value the Renderer serialises as JSON.
JSON any
// Status is the HTTP status (defaults to 200).
Status int
// ContentType overrides the response content type.
ContentType string
// Layout names the layout template, for the view seam.
Layout string
}
RenderOptions describes a render call. The actual template/view rendering is deferred to a later ActionView port; without a Renderer seam only the Plain body is written. Options mirror the common ActionController render keys.