fir

package module
v0.0.22 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: MIT Imports: 36 Imported by: 0

README

Fir

A Go toolkit to build reactive web interfaces using: Go, html/template and alpinejs.

Status: This is a work in progress. Checkout examples to see what works today: examples

Documentation

Index

Constants

View Source
const UserIDKey = "key_user_id"

UserIDKey is the key for the user id in the request context. It is used in the default channel function.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

type Controller interface {
	Route(route Route) http.HandlerFunc
	RouteFunc(options RouteFunc) http.HandlerFunc
}

Controller is an interface which encapsulates a group of views. It routes requests to the appropriate view. It routes events to the appropriate view. It also provides a way to register views.

func NewController

func NewController(name string, options ...ControllerOption) Controller

NewController creates a new controller.

type ControllerOption added in v0.0.13

type ControllerOption func(*opt)

ControllerOption is an option for the controller.

func DevelopmentMode

func DevelopmentMode(enable bool) ControllerOption

DevelopmentMode is an option to enable development mode. It enables debug logging, template watching, and disables template caching.

func DisableTemplateCache

func DisableTemplateCache() ControllerOption

DisableTemplateCache is an option to disable template caching. This is useful for development.

func EnableDebugLog

func EnableDebugLog() ControllerOption

EnableDebugLog is an option to enable debug logging.

func EnableWatch

func EnableWatch(rootDir string, extensions ...string) ControllerOption

EnableWatch is an option to enable watching template files for changes.

func WithChannel added in v0.0.8

func WithChannel(f func(r *http.Request, viewID string) *string) ControllerOption

WithChannelFunc is an option to set a function to construct the channel name for the controller's views.

func WithEmbedFS added in v0.0.8

func WithEmbedFS(fs embed.FS) ControllerOption

WithEmbedFS is an option to set the embed.FS for the controller.

func WithFormDecoder added in v0.0.13

func WithFormDecoder(decoder *schema.Decoder) ControllerOption

WithFormDecoder is an option to set the form decoder(gorilla/schema) for the controller.

func WithPublicDir added in v0.0.13

func WithPublicDir(path string) ControllerOption

WithPublicDir is the path to directory containing the public html template files.

func WithPubsubAdapter added in v0.0.8

func WithPubsubAdapter(pubsub pubsub.Adapter) ControllerOption

WithPubsubAdapter is an option to set a pubsub adapter for the controller's views.

func WithValidator added in v0.0.13

func WithValidator(validator *validator.Validate) ControllerOption

WithValidator is an option to set the validator(go-playground/validator) for the controller.

func WithWebsocketUpgrader added in v0.0.13

func WithWebsocketUpgrader(upgrader websocket.Upgrader) ControllerOption

WithWebsocketUpgrader is an option to set the websocket upgrader for the controller

type Event

type Event struct {
	// Name is the name of the event
	ID string `json:"event_id"`
	// Params is the json rawmessage to be passed to the event
	Params   json.RawMessage `json:"params"`
	Patchset dom.Patchset    `json:"patchset"`
	FormID   *string         `json:"form_id,omitempty"`
}

Event is a struct that holds the data for an event

func NewEvent added in v0.0.13

func NewEvent(id string, params any) Event

NewEvent creates a new event

func (Event) String

func (e Event) String() string

String returns the string representation of the event

type OnEventFunc added in v0.0.13

type OnEventFunc func(ctx RouteContext) error

OnEventFunc is a function that handles an http event request

type Route added in v0.0.13

type Route interface{ Options() RouteOptions }

Route is an interface that represents a route

type RouteContext added in v0.0.13

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

RouteContext is the context for a route handler. Its methods are used to return data or patch operations to the client.

func (RouteContext) Bind added in v0.0.18

func (c RouteContext) Bind(v any) error

Bind decodes the event params into the given struct

func (RouteContext) BindEventParams added in v0.0.18

func (c RouteContext) BindEventParams(v any) error

func (RouteContext) BindPathParams added in v0.0.18

func (c RouteContext) BindPathParams(v any) error

func (RouteContext) BindQueryParams added in v0.0.18

func (c RouteContext) BindQueryParams(v any) error

func (RouteContext) DOM added in v0.0.18

func (c RouteContext) DOM() dom.Patcher

func (RouteContext) Data added in v0.0.18

func (c RouteContext) Data(data any) error

Data sets the data to be hydrated into the route's template

func (RouteContext) Event added in v0.0.18

func (c RouteContext) Event() Event

func (RouteContext) FieldError added in v0.0.18

func (c RouteContext) FieldError(field string, err error) error

FieldError sets the error message for the given field and can be looked up by {{.fir.Error "myevent.field"}}

func (RouteContext) FieldErrors added in v0.0.18

func (c RouteContext) FieldErrors(fields map[string]error) error

FieldErrors sets the error messages for the given fields and can be looked up by {{.fir.Error "myevent.field"}}

func (RouteContext) KV added in v0.0.18

func (c RouteContext) KV(k string, v any) error

KV sets a key value pair to be hydrated into the route's template

func (RouteContext) Redirect added in v0.0.18

func (c RouteContext) Redirect(url string, status int) error

Redirect redirects the client to the given url

func (*RouteContext) RenderBlock added in v0.0.18

func (c *RouteContext) RenderBlock(name string, data any) dom.TemplateRenderer

RenderBlock renders a partial template on the server and is an alias for RenderTemplate(...)

func (*RouteContext) RenderHTML added in v0.0.18

func (c *RouteContext) RenderHTML(html string) dom.TemplateRenderer

RenderHTML is a utility function for rendering raw html on the server

func (*RouteContext) RenderTemplate added in v0.0.18

func (c *RouteContext) RenderTemplate(name string, data any) dom.TemplateRenderer

RenderTemplate renders a partial template on the server

func (RouteContext) Request added in v0.0.18

func (c RouteContext) Request() *http.Request

Request returns the http.Request for the current context

func (RouteContext) Response added in v0.0.18

func (c RouteContext) Response() http.ResponseWriter

Response returns the http.ResponseWriter for the current context

type RouteDOMContext added in v0.0.18

type RouteDOMContext struct {
	Name    string
	URLPath string
	// contains filtered or unexported fields
}

RouteDOMContext is a struct that holds route context data and is passed to the template

func (*RouteDOMContext) ActiveRoute added in v0.0.18

func (rc *RouteDOMContext) ActiveRoute(path, class string) string

ActiveRoute returns the class if the route is active

func (*RouteDOMContext) Error added in v0.0.18

func (rc *RouteDOMContext) Error(paths ...string) any

Error can be used to lookup an error by name Example: {{.fir.Error "myevent.field"}} will return the error for the field myevent.field Example: {{.fir.Error "myevent" "field"}} will return the error for the event myevent.field It can be used in conjunction with ctx.FieldError to get the error for a field

func (*RouteDOMContext) NotActiveRoute added in v0.0.18

func (rc *RouteDOMContext) NotActiveRoute(path, class string) string

NotActive returns the class if the route is not active

type RouteFunc added in v0.0.13

type RouteFunc func() RouteOptions

RouteFunc is a function that handles a route

type RouteOption added in v0.0.13

type RouteOption func(*routeOpt)

RouteOption is a function that sets route options

func Content added in v0.0.13

func Content(content string) RouteOption

Content sets the content for the route

func EventSender added in v0.0.13

func EventSender(eventSender chan Event) RouteOption

EventSender sets the event sender for the route. It can be used to send events for the route without a corresponding user event. This is useful for sending events to the route event handler for use cases like: sending notifications, sending emails, etc.

func Extensions added in v0.0.8

func Extensions(extensions ...string) RouteOption

Extensions sets the template file extensions read for the route's template engine

func FuncMap added in v0.0.13

func FuncMap(funcMap template.FuncMap) RouteOption

FuncMap appends to the default template function map for the route's template engine

func ID added in v0.0.13

func ID(id string) RouteOption

ID sets the route unique identifier. This is used to identify the route in pubsub.

func Layout added in v0.0.13

func Layout(layout string) RouteOption

Layout sets the layout for the route's template engine

func LayoutContentName added in v0.0.13

func LayoutContentName(name string) RouteOption

LayoutContentName sets the name of the template which contains the content.

{{define "layout"}}
{{ define "content" }}
{{ end }}
{{end}}

Here "content" is the default layout content name

func OnEvent

func OnEvent(name string, onEventFunc OnEventFunc) RouteOption

OnEvent registers an event handler for the route per unique event name. It can be called multiple times to register multiple event handlers for the route.

func OnLoad added in v0.0.13

func OnLoad(f OnEventFunc) RouteOption

OnLoad sets the route's onload event handler

func Partials added in v0.0.13

func Partials(partials ...string) RouteOption

Partials sets the template partials for the route's template engine

type RouteOptions added in v0.0.13

type RouteOptions []RouteOption

RouteOptions is a slice of RouteOption

Jump to

Keyboard shortcuts

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