controller

package
v0.0.0-...-b970022 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionFunc

type ActionFunc func(c *Base) error

ActionFunc is the body of a controller action (a seam for Ruby code).

type ActionNotFound

type ActionNotFound struct {
	Controller string
	Action     string
}

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) Action

func (b *Base) Action() string

Action returns the action name currently being processed.

func (*Base) Class

func (b *Base) Class() *Class

Class returns the controller class.

func (*Base) Head

func (b *Base) Head(status int) error

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

func (b *Base) Performed() bool

Performed reports whether a render/redirect/head has produced the response.

func (*Base) Process

func (b *Base) Process(action string) error

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

func (b *Base) RedirectTo(url string, status ...int) error

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

func (b *Base) RenderPlain(text string) error

RenderPlain is the shorthand for Render(RenderOptions{Plain: text}).

func (*Base) Request

func (b *Base) Request() *dispatch.Request

Request returns the dispatch request (may be nil).

func (*Base) Response

func (b *Base) Response() *dispatch.Response

Response returns the dispatch response.

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 NewClass

func NewClass(name string) *Class

NewClass returns a controller class with the given name (e.g. "posts").

func RescueType

func RescueType[T error](cl *Class, handle func(c *Base, err error) error) *Class

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

func (cl *Class) RescueFromIs(target error, handle func(c *Base, err error) error) *Class

RescueFromIs registers a handler for errors that wrap the sentinel target (matched with errors.Is).

func (*Class) RunAction

func (cl *Class) RunAction(fn func(c *Base, action string) error) *Class

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

func (cl *Class) SetRenderer(r Renderer) *Class

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.

type Renderer

type Renderer func(c *Base, opts RenderOptions) (string, error)

Renderer is the view-layer seam. It receives the controller and the render options and returns the rendered body. When nil, render falls back to the :plain option.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL