engine

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: MPL-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormXSRF   = "_xsrf"
	CookieXSRF = "_xsrf"
)

Variables

View Source
var ErrDuplicatedName = errors.New("duplicated form name")

Functions

func New

func New(cfg Config, options ...FormOption) (http.Handler, error)

func XSRF

func XSRF(writer http.ResponseWriter) string

XSRF protection token. Returned token should be submitted as _xsrf form value. Panics if crypto generator is not available.

Types

type Config

type Config struct {
	Forms      []schema.Form
	Storage    Storage
	Dispatcher WebhooksDispatcher
	Listing    bool
}

type Form

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

func NewForm

func NewForm(config FormConfig, options ...FormOption) *Form

func (*Form) ServeHTTP

func (f *Form) ServeHTTP(writer http.ResponseWriter, request *http.Request)

type FormConfig

type FormConfig struct {
	Definition         schema.Form        // schema definition
	Renderer           *Renderer          // renderer for template blocks
	ViewForm           *template.Template // template to show main form
	ViewResult         *template.Template // template to show result after submit
	Storage            Storage            // where to store data
	WebhooksDispatcher WebhooksDispatcher // how to execute webhooks
	XSRF               bool               // check XSRF token. Disable if form is exposed as API.
}

type FormOption

type FormOption func(config *FormConfig)

func WithXSRF

func WithXSRF(enable bool) FormOption

type Renderer

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

func (*Renderer) Render

func (r *Renderer) Render(value string, req *http.Request, result map[string]any, dataErr error) (string, error)

Render single value as golang template. Caches parsed template.

func (*Renderer) View

func (r *Renderer) View(req *http.Request) *View

type ResultContext

type ResultContext struct {
	Form   schema.Form
	Error  error
	Result map[string]any
	// contains filtered or unexported fields
}

func (*ResultContext) Render

func (rc *ResultContext) Render(value string) (string, error)

type Storage

type Storage interface {
	Store(ctx context.Context, table string, fields map[string]any) (map[string]any, error)
}

type View

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

func (*View) Render

func (v *View) Render(value string) (string, error)

type ViewContext

type ViewContext struct {
	*View
	XSRF   string
	Form   schema.Form
	Errors []fieldError
}

func (*ViewContext) FieldError

func (vc *ViewContext) FieldError(name string) error

func (*ViewContext) LastValue

func (vc *ViewContext) LastValue(name string) string

func (*ViewContext) LastValues

func (vc *ViewContext) LastValues(name string) utils.Set[string]

type WebhooksDispatcher

type WebhooksDispatcher interface {
	Dispatch(ctx context.Context, webhook schema.Webhook, payload []byte) error
}

Jump to

Keyboard shortcuts

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