Documentation ¶
Index ¶
- Constants
- Variables
- func RedirectUnauthorisedWebSocket(w http.ResponseWriter, r *http.Request, redirect string) bool
- type ContextKey
- type Controller
- type ControllerOption
- func DevelopmentMode(enable bool) ControllerOption
- func DisableTemplateCache() ControllerOption
- func EnableDebugLog() ControllerOption
- func EnableWatch(rootDir string, extensions ...string) ControllerOption
- func WithChannelFunc(f func(r *http.Request, viewID string) *string) ControllerOption
- func WithDisableWebsocket() ControllerOption
- func WithDropDuplicateInterval(interval time.Duration) ControllerOption
- func WithEmbedFS(fs embed.FS) ControllerOption
- func WithFormDecoder(decoder *schema.Decoder) ControllerOption
- func WithFuncMap(funcMap template.FuncMap) ControllerOption
- func WithOnSocketConnect(f func(userOrSessionID string) error) ControllerOption
- func WithOnSocketDisconnect(f func(userOrSessionID string)) ControllerOption
- func WithPathParamsFunc(f func(r *http.Request) PathParams) ControllerOption
- func WithPublicDir(path string) ControllerOption
- func WithPubsubAdapter(pubsub pubsub.Adapter) ControllerOption
- func WithSessionName(name string) ControllerOption
- func WithSessionSecrets(hashKey []byte, blockKey []byte) ControllerOption
- func WithWebsocketUpgrader(upgrader websocket.Upgrader) ControllerOption
- type Event
- type OnEventFunc
- type PathParams
- type Route
- type RouteContext
- func (c RouteContext) Bind(v any) error
- func (c RouteContext) BindEventParams(v any) error
- func (c RouteContext) BindPathParams(v any) error
- func (c RouteContext) BindQueryParams(v any) error
- func (c RouteContext) Data(dataset ...any) error
- func (c RouteContext) Event() Event
- func (c RouteContext) FieldError(field string, err error) error
- func (c RouteContext) FieldErrors(fields map[string]error) error
- func (c RouteContext) GetUserFromContext() string
- func (c RouteContext) KV(key string, data any) error
- func (c RouteContext) Redirect(url string, status int) error
- func (c RouteContext) Request() *http.Request
- func (c RouteContext) Response() http.ResponseWriter
- func (c RouteContext) State(dataset ...any) error
- func (c RouteContext) StateKV(key string, data any) error
- func (c RouteContext) Status(code int, err error) error
- type RouteDOMContext
- type RouteFunc
- type RouteOption
- func Content(content string) RouteOption
- func ErrorContent(content string) RouteOption
- func ErrorLayout(layout string) RouteOption
- func ErrorLayoutContentName(name string) RouteOption
- func EventSender(eventSender chan Event) RouteOption
- func Extensions(extensions ...string) RouteOption
- func FuncMap(funcMap template.FuncMap) RouteOption
- func ID(id string) RouteOption
- func Layout(layout string) RouteOption
- func LayoutContentName(name string) RouteOption
- func OnEvent(name string, onEventFunc OnEventFunc) RouteOption
- func OnLoad(f OnEventFunc) RouteOption
- func Partials(partials ...string) RouteOption
- type RouteOptions
- type SocketStatus
- type Todo
Constants ¶
const ( EventSocketConnected = "fir_socket_connected" EventSocketDisconnected = "fir_socket_disconnected" )
Variables ¶
var ErrorEventFilterFormat = fmt.Errorf("error parsing event filter. must match ^[a-zA-Z0-9-]+:(ok|pending|error|done)$")
Functions ¶
func RedirectUnauthorisedWebSocket ¶ added in v0.5.1
RedirectUnauthorisedWebSocket sends a 4001 close message to the client It sends the redirect url in the close message payload If the request is not a websocket request or has error upgrading and writing the close message, it returns false redirect url must be less than 123 bytes
Types ¶
type ContextKey ¶ added in v0.1.5
type ContextKey int
const ( // PathParamsKey is the key for the path params in the request context. PathParamsKey ContextKey = iota // UserKey is the key for the user id/name in the request context. It is used in the default channel function. UserKey )
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 ¶
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 WithChannelFunc ¶ added in v0.1.5
func WithChannelFunc(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 WithDisableWebsocket ¶ added in v0.1.2
func WithDisableWebsocket() ControllerOption
WithDisableWebsocket is an option to disable websocket.
func WithDropDuplicateInterval ¶ added in v0.6.0
func WithDropDuplicateInterval(interval time.Duration) ControllerOption
WithDropDuplicateInterval is an option to set the interval to drop duplicate events received by the websocket.
func WithEmbedFS ¶
func WithEmbedFS(fs embed.FS) ControllerOption
WithEmbedFS is an option to set the embed.FS for the controller.
func WithFormDecoder ¶
func WithFormDecoder(decoder *schema.Decoder) ControllerOption
WithFormDecoder is an option to set the form decoder(gorilla/schema) for the controller.
func WithFuncMap ¶ added in v0.2.11
func WithFuncMap(funcMap template.FuncMap) ControllerOption
func WithOnSocketConnect ¶ added in v0.6.0
func WithOnSocketConnect(f func(userOrSessionID string) error) ControllerOption
WithOnSocketConnect takes a function that is called when a new websocket connection is established. The function should return an error if the connection should be rejected. The user or fir's browser session id is passed to the function. user must be set in request.Context with the key UserKey by a developer supplied authentication mechanism. It can be used to track user connections and disconnections. It can be be used to reject connections based on user or session id. It can be used to refresh the page data when a user re-connects.
func WithOnSocketDisconnect ¶ added in v0.6.0
func WithOnSocketDisconnect(f func(userOrSessionID string)) ControllerOption
WithOnSocketDisconnect takes a function that is called when a websocket connection is disconnected.
func WithPathParamsFunc ¶ added in v0.1.5
func WithPathParamsFunc(f func(r *http.Request) PathParams) ControllerOption
WithPathParamsFunc is an option to set a function to construct the path params for the controller's views.
func WithPublicDir ¶
func WithPublicDir(path string) ControllerOption
WithPublicDir is the path to directory containing the public html template files.
func WithPubsubAdapter ¶
func WithPubsubAdapter(pubsub pubsub.Adapter) ControllerOption
WithPubsubAdapter is an option to set a pubsub adapter for the controller's views.
func WithSessionName ¶ added in v0.1.2
func WithSessionName(name string) ControllerOption
WithSessionName is an option to set the session name/cookie name for the controller.
func WithSessionSecrets ¶ added in v0.4.0
func WithSessionSecrets(hashKey []byte, blockKey []byte) ControllerOption
WithSessionSecrets is an option to set the session secrets for the controller. used to sign and encrypt the session cookie.
func WithWebsocketUpgrader ¶
func WithWebsocketUpgrader(upgrader websocket.Upgrader) ControllerOption
WithWebsocketUpgrader is an option to set the websocket upgrader for the controller
type Event ¶
type Event struct { // ID is the event id ID string `json:"event_id"` // Params is the data to be passed to the event Params json.RawMessage `json:"params"` // Target is the dom element id which emitted the event. The DOM events generated by the event will be targeted to this element. Target *string `json:"target,omitempty"` // IsForm is a boolean that indicates whether the event was triggered by a form submission IsForm bool `json:"is_form,omitempty"` // SessionID is the id of the session that the event was triggered for SessionID *string `json:"session_id,omitempty"` ElementKey *string `json:"element_key,omitempty"` Timestamp int64 `json:"ts,omitempty"` }
Event is a struct that holds the data for an incoming user event
type OnEventFunc ¶
type OnEventFunc func(ctx RouteContext) error
OnEventFunc is a function that handles an http event request
type PathParams ¶ added in v0.1.5
type Route ¶
type Route interface{ Options() RouteOptions }
Route is an interface that represents a route
type RouteContext ¶
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 ¶
func (c RouteContext) Bind(v any) error
Bind decodes the event params into the given struct
func (RouteContext) BindEventParams ¶
func (c RouteContext) BindEventParams(v any) error
func (RouteContext) BindPathParams ¶
func (c RouteContext) BindPathParams(v any) error
func (RouteContext) BindQueryParams ¶
func (c RouteContext) BindQueryParams(v any) error
func (RouteContext) Data ¶
func (c RouteContext) Data(dataset ...any) error
Data sets the data to be hydrated into the route's template or an event's associated template/block action It accepts either a map or struct type so that fir can inject utility functions: fir.Error, fir.ActiveRoute etc. If the data is a struct, it will be converted to a map using github.com/fatih/structs If the data is a pointer to a struct, it will be dereferenced and converted to a map using github.com/fatih/structs If the data is a map, it will be used as is If the data is a pointer to a map, it will be dereferenced and used as is The function will return nil if no data is passed The function accepts variadic arguments so that you can pass multiple structs or maps which will be merged
func (RouteContext) Event ¶
func (c RouteContext) Event() Event
func (RouteContext) FieldError ¶
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 ¶
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) GetUserFromContext ¶ added in v0.1.8
func (c RouteContext) GetUserFromContext() string
func (RouteContext) KV ¶
func (c RouteContext) KV(key string, data any) error
KV is a wrapper for ctx.Data(map[string]any{key: data})
func (RouteContext) Redirect ¶
func (c RouteContext) Redirect(url string, status int) error
Redirect redirects the client to the given url
func (RouteContext) Request ¶
func (c RouteContext) Request() *http.Request
Request returns the http.Request for the current context
func (RouteContext) Response ¶
func (c RouteContext) Response() http.ResponseWriter
Response returns the http.ResponseWriter for the current context
func (RouteContext) State ¶ added in v0.2.8
func (c RouteContext) State(dataset ...any) error
State data is only passed to event receiver without a bound template it can be acccessed in the event receiver via $event.detail e.g. @fir:myevent:ok="console.log('$event.detail.mykey')"
type RouteDOMContext ¶
type RouteDOMContext struct { Name string Development bool 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 ¶
func (rc *RouteDOMContext) ActiveRoute(path, class string) string
ActiveRoute returns the class if the route is active
func (*RouteDOMContext) Error ¶
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 ¶
func (rc *RouteDOMContext) NotActiveRoute(path, class string) string
NotActive returns the class if the route is not active
type RouteOption ¶
type RouteOption func(*routeOpt)
RouteOption is a function that sets route options
func ErrorContent ¶ added in v0.1.5
func ErrorContent(content string) RouteOption
ErrorContent sets the content for the route
func ErrorLayout ¶ added in v0.1.5
func ErrorLayout(layout string) RouteOption
ErrorLayout sets the layout for the route's template engine
func ErrorLayoutContentName ¶ added in v0.1.5
func ErrorLayoutContentName(name string) RouteOption
ErrorLayoutContentName 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 EventSender ¶
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 ¶
func Extensions(extensions ...string) RouteOption
Extensions sets the template file extensions read for the route's template engine
func FuncMap ¶
func FuncMap(funcMap template.FuncMap) RouteOption
FuncMap appends to the default template function map for the route's template engine
func ID ¶
func ID(id string) RouteOption
ID sets the route unique identifier. This is used to identify the route in pubsub.
func Layout ¶
func Layout(layout string) RouteOption
Layout sets the layout for the route's template engine
func LayoutContentName ¶
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 Partials ¶
func Partials(partials ...string) RouteOption
Partials sets the template partials for the route's template engine