controller

package
v0.1.14-beta38 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package controller provides a controller / action based http.Handler for the router. A Controller can have different renderer and is easy to extend. Data, Redirects and Errors can be set directly in the controller. A Context with some helpers for the response and request is provided.

Index

Constants

View Source
const (
	MSG   = "msg"
	ERROR = "error"
)
View Source
const (
	RenderJSON = "json"
)

predefined render types

Variables

View Source
var (
	ErrAction = "controller: action %v does not exist in %v"
)

Error messages

Functions

This section is empty.

Types

type Base

type Base struct {
	// contains filtered or unexported fields
}

Base struct

func (*Base) Action

func (c *Base) Action() string

Action name.

func (*Base) CallAction

func (c *Base) CallAction(name string) (func(), error)

CallAction will call a controller function by name. Error will return if the controller method does not exist.

func (*Base) CheckBrowserCancellation

func (c *Base) CheckBrowserCancellation() bool

CheckBrowserCancellation checking if the browser canceled the request

func (*Base) Context

func (c *Base) Context() *context.Context

Context returns the controller context.

func (*Base) Error

func (c *Base) Error(code int, err error)

Error calls the renderer Error function with the given error. As fallback a normal http.Error will be triggered.

func (*Base) HasError

func (c *Base) HasError() bool

HasError will be true if the function Error was called. If set, the Render function will not be called.

func (*Base) Initialize

func (c *Base) Initialize(caller Interface)

Initialize the controller. Its required to set the correct reference.

func (*Base) Name

func (c *Base) Name() string

Name returns the controller incl. package name.

func (*Base) Redirect

func (c *Base) Redirect(status int, url string)

Redirect sets a HTTP location header and status code. On a redirect the old controller data will be lost. TODO check if this should be changed and the controller data copied?

func (*Base) RenderType

func (c *Base) RenderType() string

RenderType of the controller. Default json.

func (*Base) ServeHTTP

func (c *Base) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handler.

func (*Base) Set

func (c *Base) Set(key string, value interface{})

Set a controller variable by key and value.

func (*Base) SetContext

func (c *Base) SetContext(ctx *context.Context)

SetContext to the controller.

func (*Base) SetRenderType

func (c *Base) SetRenderType(s string)

SetRenderType of the controller.

func (*Base) T

func (c *Base) T(name string, template ...map[string]interface{}) string

T is a helper to translate a string.

func (*Base) TP

func (c *Base) TP(name string, count int, template ...map[string]interface{}) string

TP is a helper to translate a plural string.

type Interface

type Interface interface {
	Initialize(caller Interface) // needed to set the caller reference.
	ServeHTTP(http.ResponseWriter, *http.Request)

	// Context
	Context() *context.Context
	SetContext(ctx *context.Context)

	// render type
	RenderType() string
	SetRenderType(string)

	// controller helpers
	Name() string
	Action() string
	Set(key string, value interface{})
	Error(status int, err error)
	Redirect(status int, url string)

	// Translation
	T(string, ...map[string]interface{}) string
	TP(string, int, ...map[string]interface{}) string

	// helpers
	CheckBrowserCancellation() bool
	CallAction(action string) (func(), error)
	HasError() bool // returns true if Error(int,err) was called.
}

Interface of the controller.

Directories

Path Synopsis
Package context provides a request and response struct which can be used in the controller.
Package context provides a request and response struct which can be used in the controller.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳