controller

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: May 15, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

README

goliveview/controller

Todos

  • Use layout and partials at controller level
  • Refresh browser when templates are modified
  • go stats
  • SessionStore implementation using badger
  • LiveList: sorting, pagination, patching
  • Add unit tests
  • Add benchmarks
  • Handle user inactivity: delete session store for inactive connections
  • Structured logging
  • Redirect to error page
  • Compile time validation of data attributes: if target == dataset
  • Describe dataset as json files in template/partials/target.dataset
  • (stimulus)Handle 4XX, 5XX types error to go to a /4xx,5xx page.
  • broadcast to clients over pubsusb.
  • Support template.ParseFS
  • morphdom diffing support
  • csrf token https://coletiv.com/blog/using-websockets-with-cookie-based-authentication/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultUserErrorMessage = "internal error"
View Source
var DefaultViewExtensions = []string{".gohtml", ".gotmpl", ".html", ".tmpl"}
View Source
var DefaultWatchExtensions = []string{".go", ".gohtml", ".gotmpl", ".html", ".tmpl"}

Functions

func DefaultFuncMap

func DefaultFuncMap() template.FuncMap

func UserError added in v0.0.2

func UserError(err error) string

Types

type Context

type Context interface {
	Event() Event
	DOM() DOM
	Store() Store
	Temporary(keys ...string)
	Request() *http.Request
	ResponseWriter() http.ResponseWriter
}

type Controller

type Controller interface {
	Handler(view View) http.HandlerFunc
}

func Websocket

func Websocket(name string, options ...Option) Controller

type DOM

type DOM interface {
	SetDataset(selector string, data M)
	SetAttributes(selector string, data M)
	SetValue(selector string, value interface{})
	SetInnerHTML(selector string, value interface{})
	RemoveAttributes(selector string, data []string)
	ToggleClassList(selector string, classList map[string]bool)
	AddClass(selector, class string)
	RemoveClass(selector, class string)
	Morph(selector, template string, data M)
	Reload()
}

type DefaultErrorView added in v0.0.4

type DefaultErrorView struct{}

func (DefaultErrorView) Content added in v0.0.4

func (d DefaultErrorView) Content() string

func (DefaultErrorView) Extensions added in v0.0.4

func (d DefaultErrorView) Extensions() []string

func (DefaultErrorView) FuncMap added in v0.0.4

func (d DefaultErrorView) FuncMap() template.FuncMap

func (DefaultErrorView) Layout added in v0.0.4

func (d DefaultErrorView) Layout() string

func (DefaultErrorView) LayoutContentName added in v0.0.4

func (d DefaultErrorView) LayoutContentName() string

func (DefaultErrorView) LiveEventReceiver added in v0.0.9

func (d DefaultErrorView) LiveEventReceiver() <-chan Event

func (DefaultErrorView) OnLiveEvent added in v0.0.9

func (d DefaultErrorView) OnLiveEvent(ctx Context) error

func (DefaultErrorView) OnMount added in v0.0.4

func (d DefaultErrorView) OnMount(ctx Context) (Status, M)

func (DefaultErrorView) Partials added in v0.0.4

func (d DefaultErrorView) Partials() []string

type DefaultView added in v0.0.3

type DefaultView struct{}

func (DefaultView) Content added in v0.0.3

func (d DefaultView) Content() string

Content returns either path to the content or a html string content

func (DefaultView) Extensions added in v0.0.3

func (d DefaultView) Extensions() []string

Extensions returns the view extensions. Defaults to: html, tmpl, gohtml, gotmpl

func (DefaultView) FuncMap added in v0.0.3

func (d DefaultView) FuncMap() template.FuncMap

FuncMap configures the html/template.FuncMap

func (DefaultView) Layout added in v0.0.3

func (d DefaultView) Layout() string

Layout returns either path to the layout or a html string layout Layout represents the path to the base layout to be used.

	layout.html e.g.
	<!DOCTYPE html>
	<html lang="en">
	<head>
		<title>{{.app_name}}</title>
		{{template "header" .}}
	</head>
	<body>
	{{template "navbar" .}}
	<div>
		{{template "content" .}}
	</div>
	{{template "footer" .}}
	</body>
	</html>
 The {{template "content" .}} directive is replaced by the page in the path exposed by `Content`

func (DefaultView) LayoutContentName added in v0.0.3

func (d DefaultView) LayoutContentName() string

LayoutContentName is the defined template of the "content" to be replaced. Defaults to "content"

e.g.

type SimpleView struct {
	glv.DefaultView
}
func (s *SimpleView) Content() string {
	return `{{define "content"}}<div>world</div>{{ end }}`
}
func (s *SimpleView) Layout() string {
	return `<div>Hello: {{template "content" .}}</div>`
}

func (DefaultView) LiveEventReceiver added in v0.0.9

func (d DefaultView) LiveEventReceiver() <-chan Event

LiveEventReceiver is used to configure a receive only channel for receiving events from concurrent goroutines. e.g. a concurrent goroutine sends a tick event every second to the returned channel which is then handled in OnLiveEvent.

func (DefaultView) OnLiveEvent added in v0.0.9

func (d DefaultView) OnLiveEvent(ctx Context) error

OnLiveEvent handles the events sent from the browser or received on the LiveEventReceiver channel

func (DefaultView) OnMount added in v0.0.3

func (d DefaultView) OnMount(ctx Context) (Status, M)

OnMount is called when the page is first loaded for the http route.

func (DefaultView) Partials added in v0.0.3

func (d DefaultView) Partials() []string

Partials returns path to any partials used in the view. Defaults to "./templates/partials"

type Event

type Event struct {
	ID       string          `json:"id"`
	Selector string          `json:"selector"`
	Template string          `json:"template"`
	Params   json.RawMessage `json:"params"`
}

func (Event) DecodeParams

func (e Event) DecodeParams(v interface{}) error

func (Event) String

func (e Event) String() string

type EventHandler

type EventHandler func(ctx Context) error

type M

type M map[string]interface{}

type Op

type Op string
const (
	ClassList        Op = "classlist"
	Dataset          Op = "dataset"
	SetAttributes    Op = "setAttributes"
	RemoveAttributes Op = "removeAttributes"
	Morph            Op = "morph"
	Reload           Op = "reload"
	AddClass         Op = "addClass"
	RemoveClass      Op = "removeClass"
	SetValue         Op = "setValue"
	SetInnerHTML     Op = "setInnerHTML"
)

type Operation

type Operation struct {
	Op       Op          `json:"op"`
	Selector string      `json:"selector"`
	Value    interface{} `json:"value"`
}

func (*Operation) Bytes

func (m *Operation) Bytes() []byte

type Option

type Option func(*controlOpt)

func DevelopmentMode added in v0.0.4

func DevelopmentMode(enable bool) Option

func DisableTemplateCache

func DisableTemplateCache() Option

func EnableDebugLog

func EnableDebugLog() Option

func EnableHTMLFormatting

func EnableHTMLFormatting() Option

func EnableWatch

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

func WithErrorView added in v0.0.4

func WithErrorView(view View) Option

func WithSubscribeTopic

func WithSubscribeTopic(f func(r *http.Request) *string) Option

func WithUpgrader

func WithUpgrader(upgrader websocket.Upgrader) Option

type Status added in v0.0.4

type Status struct {
	Code    int    `json:"statusCode"`
	Message string `json:"statusMessage"`
}

type Store added in v0.0.6

type Store interface {
	Put(m M) error
	Get(key string, data interface{}) error
}

type View added in v0.0.3

type View interface {
	Content() string
	Layout() string
	LayoutContentName() string
	Partials() []string
	Extensions() []string
	FuncMap() template.FuncMap
	OnMount(ctx Context) (Status, M)
	OnLiveEvent(ctx Context) error
	LiveEventReceiver() <-chan Event
}

Jump to

Keyboard shortcuts

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