peji

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2020 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PlainHTML      = "text/html"
	VoidHTML       = "application/groundlayer+html"
	VoidHTMLDiff   = "application/groundlayer+html+diff"
	VoidJSON       = "application/groundlayer+json"
	VoidJSONStream = "application/groundlayer+json_stream"
)
View Source
const (
	DefaultMaxPageIdleness            = 5 * time.Minute
	DefaultPageIdlenessChecksInterval = 2 * time.Minute

	HeaderSessionIdName         = "X-Void-Id"
	QueryAndCookieSessionIdName = "_groundlayer_id"
)
View Source
const (
	// ParamStart is the character, as a string, which a path pattern starts to define its named parameter.
	ParamStart = ":"

	// WildcardParamStart is the character, as a string,
	// which a path pattern starts to define its named parameter for wildcards.
	// It allows everything else after that path prefix
	// but the Trie checks for static paths and named parameters before that in order
	// to support everything that other implementations do not,
	// and if nothing else found then it tries to find the closest wildcard path(super and unique).
	WildcardParamStart = "*"
)

Variables

View Source
var DefaultKeysSorter = func(list []string) func(i, j int) bool {
	return func(i, j int) bool {
		return len(strings.Split(list[i], pathSep)) < len(strings.Split(list[j], pathSep))
	}
}

DefaultKeysSorter sorts as: first the "key (the path)" with the lowest number of slashes.

Functions

func RenderVoidHTML

func RenderVoidHTML(node *domu.Node, target io.Writer) error

func RenderVoidHTMLDiff

func RenderVoidHTMLDiff(node *domu.Node, target io.Writer) error

func RenderVoidJSON

func RenderVoidJSON(node *domu.Node, target io.Writer) error

func RenderVoidJSONStream

func RenderVoidJSONStream(node *domu.Node, target io.Writer) error

Types

type DOM

type DOM interface {
	Render(ctx Data) *domu.Node
}

func DOMToLiveDOM

func DOMToLiveDOM(list []DOM) []DOM

func LiveDOMToDOM

func LiveDOMToDOM(list []*LiveDOM) []DOM

type DOMFunc

type DOMFunc func(ctx Data) *domu.Node

func Section

func Section(components ...DOM) DOMFunc

Section composes a set of components wrapped into a Section component.

func (DOMFunc) Render

func (pf DOMFunc) Render(ctx Data) *domu.Node

type DOMRegistry

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

DOMRegistry implements the a component single for DOM objects.

func NewDOMRegistry

func NewDOMRegistry() *DOMRegistry

func (*DOMRegistry) Add

func (k *DOMRegistry) Add(km DOM, refName string)

Add adds giving component into underline register using the type routePath for giving component.

func (*DOMRegistry) AddList

func (k *DOMRegistry) AddList(km []DOM, refName string)

AddList adds giving component into underline register using the type routePath for giving component.

func (*DOMRegistry) Clean

func (k *DOMRegistry) Clean(max time.Duration)

Clean up all LiveDOM elements with a dormancy equal or above provided duration.

func (*DOMRegistry) Delete

func (k *DOMRegistry) Delete(refName string)

Delete deletes any key in the singles map.

func (*DOMRegistry) DeleteList

func (k *DOMRegistry) DeleteList(refName string)

DeleteList deletes any key in the list map.

func (*DOMRegistry) Get

func (k *DOMRegistry) Get(refName string) DOM

Get returns the DOM matching giving type routePath.

func (*DOMRegistry) GetList

func (k *DOMRegistry) GetList(refName string) []DOM

GetList returns the DOM matching giving type routePath.

func (*DOMRegistry) Has

func (k *DOMRegistry) Has(refName string) bool

HasType returns true/false if giving typeName exists.

func (*DOMRegistry) HasList

func (k *DOMRegistry) HasList(refName string) bool

HasType returns true/false if giving typeName exists.

func (*DOMRegistry) Reset

func (k *DOMRegistry) Reset()

type DOMSet

type DOMSet []DOM

DOMSet defines a list of DOM items.

func (DOMSet) Render

func (dms DOMSet) Render(data Data) *domu.Node

Render implements the DOM interface for a DOM set (a list of DOM objects)

func (DOMSet) RenderTo

func (dms DOMSet) RenderTo(data Data, parent *domu.Node)

Render implements the DOM interface for a DOM set (a list of DOM objects)

type Data

type Data struct {
	Path      string
	SessionId string
	Data      interface{}
	Params    Params
	Message   *sabuhp.Message
}

type DefaultNotFound

type DefaultNotFound struct{}

func (DefaultNotFound) Handle

func (d DefaultNotFound) Handle(dt Data) *domu.Node

type Handler

type Handler interface {
	Handle(Data) *domu.Node
}

func LiveDOMHandler

func LiveDOMHandler(target string, page *Page) Handler

LiveDOMHandler returns a Handler which provides the params as the data to be giving to a Live DOM render method.

func StaticDOMHandler

func StaticDOMHandler(target string, page *Page) Handler

StaticDOMHandler returns a Handler which provides the params as the data to be giving to a Live DOM render method.

type HandlerFunc

type HandlerFunc func(Data) *domu.Node

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(d Data) *domu.Node
type Header map[string][]string

func (Header) Add

func (h Header) Add(k string, v string)

func (Header) Delete

func (h Header) Delete(k string)

func (Header) Get

func (h Header) Get(k string) string

func (Header) Set

func (h Header) Set(k string, v string)

func (Header) Values

func (h Header) Values(k string) []string

type InsertOption

type InsertOption func(*Node)

InsertOption is just a function which accepts a pointer to a Node which can alt its `Handler`, `Tag` and `Data` fields.

See `WithTransportResponse`, `WithTag` and `WithData`.

func WithData

func WithData(data interface{}) InsertOption

WithData sets the node's optionally `Data` field.

func WithHandler

func WithHandler(handler Handler) InsertOption

WithHandler sets the node's `Handler` field (useful for HTTP).

func WithTag

func WithTag(tag string) InsertOption

WithTag sets the node's `Tag` field (may be useful for HTTP).

func WithTransportResponse

func WithTransportResponse(handler Handler) InsertOption

WithTransportResponse sets the node's `Handler` field (useful for HTTP).

type Layout

type Layout interface {
	Render(p *Page, ctx Data, parent *domu.Node)
}

type LayoutFunc

type LayoutFunc func(p *Page, ctx Data, parent *domu.Node)

func (LayoutFunc) Render

func (pf LayoutFunc) Render(p *Page, ctx Data, parent *domu.Node)

type LiveDOM

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

LiveDOM wraps a dom which keeps track of Last render and reconciling new renderings with the old.

func NewLiveDOM

func NewLiveDOM(dom DOM) *LiveDOM

NewLiveDOM returns a DOM wrapped to return Nodes reconciled with their previous render.

func (*LiveDOM) LastCalled

func (c *LiveDOM) LastCalled() time.Time

LastCalled returns the time when the Last called was made.

func (*LiveDOM) Render

func (c *LiveDOM) Render(ctx Data) *domu.Node

Render implements the DOM interface which returns a new instance of the DOM's rendered reconciled with the Last.

type LiveDOMList

type LiveDOMList []LiveDOM

type Logger

type Logger interface {
	Log(json *njson.JSON)
}

type Mux

type Mux struct {
	NotFound Handler
	// contains filtered or unexported fields
}

Mux is Request multiplexer. It matches an event routePath or http url pattern to a specific TransportHandler which will be registered to the provided transport for handling specific events.

func NewMux

func NewMux(config MuxConfig) *Mux

func (*Mux) Match

func (m *Mux) Match(target string) bool

Match implements the Matcher interface.

Allow a mux to be used as a matcher and handler elsewhere.

func (*Mux) Serve

func (m *Mux) Serve(route string, handler Handler)

func (*Mux) ServeRoute

func (m *Mux) ServeRoute(d Data) *domu.Node

type MuxConfig

type MuxConfig struct {
	RootPath string
	NotFound Handler
}

type Node

type Node struct {

	// insert main data relative to http and a tag for things like route names.
	Handler Handler
	Tag     string

	// other insert data.
	Data interface{}
	// contains filtered or unexported fields
}

Node is the trie's node which path patterns with their data like an HTTP handler are saved to. See `Trie` too.

func NewNode

func NewNode() *Node

NewNode returns a new, empty, Node.

func (*Node) IsEnd

func (n *Node) IsEnd() bool

IsEnd returns true if this Node is a final path, has a key.

func (*Node) Keys

func (n *Node) Keys(sorter NodeKeysSorter) (list []string)

Keys returns this node's key (if it's a final path segment) and its children's node's key. The "sorter" can be optionally used to sort the result.

func (*Node) Parent

func (n *Node) Parent() *Node

Parent returns the parent of that node, can return nil if this is the rootPath node.

func (*Node) String

func (n *Node) String() string

String returns the key, which is the path pattern for the HTTP Mux.

type NodeKeysSorter

type NodeKeysSorter = func(list []string) func(i, j int) bool

NodeKeysSorter is the type definition for the sorting logic that caller can pass on `GetKeys` and `Autocomplete`.

type OnPage added in v0.0.6

type OnPage func(route string, p *Page)

type OnPages added in v0.1.1

type OnPages func(route string, p *Pages)

type Page

type Page struct {
	UseChanges     bool
	Name           string
	RoutePath      string
	AnyRouteInPage string
	Router         *Mux
	Layout         Layout

	Last             *domu.Node
	Registry         *DOMRegistry
	LiveRegistry     *DOMRegistry
	LiveRouteMapping *plain.StringMap
	// contains filtered or unexported fields
}

Page implements the Document interface and represents a single unique html page.

func NewPageUsingRouter

func NewPageUsingRouter(routePath string, layout Layout, router *Mux) *Page

func WithPage

func WithPage(name string, layout Layout, notFound Handler) *Page

func (*Page) AddLive

func (pg *Page) AddLive(route string, targetName string) DOM

AddLive returns a new live DOM by creating a route mapping between a target component or list of components in the page to be serviceable by the route ensuring it's always reconciled with it's last state.

func (*Page) AddStatic

func (pg *Page) AddStatic(name string, items ...DOM)

AddStatic adds a new routePath mapping for a provided list, ensuring these are referenced with giving component, if there is more than 1 item then they are treated as a whole and referenced by provided routePath.

Note: It clears all live routes for this giving routePath, if we are replacing an existing live key.

func (*Page) Cleanup

func (pg *Page) Cleanup(maxDuration time.Duration)

Cleanup goes through the live instance releasing any which may have surpassed the maxLastUsed period.

func (*Page) Close

func (pg *Page) Close()

func (*Page) GetRouting

func (pg *Page) GetRouting(name string) string

GetRouting returns the route for giving routePath.

func (*Page) HasLive

func (pg *Page) HasLive(route string) bool

HasLive returns true if giving route exists either as a live dom or dom list.

func (*Page) HasStatic

func (pg *Page) HasStatic(name string) bool

HasStatic returns true if giving route exists either as a static dom or dom list.

func (*Page) Live

func (pg *Page) Live(route string) DOM

GetLive returns the DOM for giving routePath, if not found, nil is returned.

func (*Page) OnPageAdd added in v0.0.6

func (pg *Page) OnPageAdd(fn OnPage)

func (*Page) Render

func (pg *Page) Render(d Data) *domu.Node

Render renders new domu.Node from existing page which is reconciled with it's previous render.

The markup node is pure, which means it's the complete picture generated from a Page object.

func (*Page) Retire

func (pg *Page) Retire(name string)

Retire retires the dom (live or static) associated with the routePath and every route associated with it.

func (*Page) Serve

func (pg *Page) Serve(d Data) *domu.Node

func (*Page) ServeLive

func (pg *Page) ServeLive(route string, handler Handler)

func (*Page) ServeStatic

func (pg *Page) ServeStatic(name string, handler Handler)

func (*Page) SetReconciliation

func (pg *Page) SetReconciliation(b bool)

func (*Page) Static

func (pg *Page) Static(name string) DOM

GetStatic returns the DOM for giving routePath, if not found, nil is returned.

type PageCreator

type PageCreator func(name string, pubsub sabuhp.Transport) *Page

type PageNotification added in v0.0.6

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

func NewPageNotification added in v0.0.6

func NewPageNotification() *PageNotification

func (*PageNotification) Add added in v0.0.6

func (h *PageNotification) Add(eventHandler OnPage)

func (*PageNotification) Emit added in v0.0.6

func (h *PageNotification) Emit(route string, page *Page)

type PageSession

type PageSession struct {
	Id   nxid.ID
	Page *Page
	// contains filtered or unexported fields
}

func (*PageSession) Close

func (ps *PageSession) Close()

type PageSessionManager

type PageSessionManager struct {
	Creator PageCreator
	// contains filtered or unexported fields
}

func NewPageSessionManager

func NewPageSessionManager(
	ctx context.Context,
	routePath string,
	maxIdle time.Duration,
	idleCheckInterval time.Duration,
	creator PageCreator,
	onAddRoute OnPage,
) *PageSessionManager

func (*PageSessionManager) GetName

func (psm *PageSessionManager) GetName() string

func (*PageSessionManager) NewSession

func (psm *PageSessionManager) NewSession(t sabuhp.Transport) (*Page, string, error)

NewSession returns a new session page and session id.

func (*PageSessionManager) Retire

func (psm *PageSessionManager) Retire(sessionId string) error

Retire returns closes a specific session using giving id.

func (*PageSessionManager) Session

func (psm *PageSessionManager) Session(sessionId string) (*Page, error)

Session returns a giving page for a giving sessionId.

func (*PageSessionManager) Start

func (psm *PageSessionManager) Start()

func (*PageSessionManager) Stat

func (psm *PageSessionManager) Stat() (SessionStat, error)

func (*PageSessionManager) Stop

func (psm *PageSessionManager) Stop()

func (*PageSessionManager) Wait

func (psm *PageSessionManager) Wait()

type Pages

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

Pages exists to provider an organization around sessions and pages.

It implements the http.Handler interface.

func NewPages

func NewPages(
	ctx context.Context,
	logger Logger,
	prefix string,
	maxIdle time.Duration,
	idleCheck time.Duration,
	transport sabuhp.Transport,
	notFound Handler,
) *Pages

func WithPages

func WithPages(
	ctx context.Context,
	logger Logger,
	prefix string,
	transport sabuhp.Transport,
	notFound Handler,
) *Pages

func (*Pages) Add

func (p *Pages) Add(pageName string, creatorFunc PageCreator) error

AddCreator adds a new PageCreator for a giving page routePath. It returns true/false based on whether the routePath and creator was registered or if there was routePath conflict.

func (*Pages) AddOnPageRoute added in v0.0.6

func (p *Pages) AddOnPageRoute(cb OnPages)

func (*Pages) Get

func (p *Pages) Get(pageName string) (*PageSessionManager, error)

GetManager returns page creator registered for a giving page page

func (*Pages) Handle

func (p *Pages) Handle(message *sabuhp.Message, tr sabuhp.Transport) sabuhp.MessageErr

func (*Pages) Has

func (p *Pages) Has(pageName string) bool

HasPage returns true/false if a giving page exists.

func (*Pages) Stats

func (p *Pages) Stats() PagesStat

Stats returns current states of existing pages, creators.

func (*Pages) Wait

func (p *Pages) Wait()

type PagesStat

type PagesStat struct {
	TotalPages   int
	PageSessions map[string]SessionStat
}

type Params

type Params map[string]string

func (Params) Delete

func (h Params) Delete(k string)

func (Params) EncodeObject

func (h Params) EncodeObject(encoder npkg.ObjectEncoder)

func (Params) Get

func (h Params) Get(k string) string

func (Params) Set

func (h Params) Set(k string, v string)

type ParamsSetter

type ParamsSetter interface {
	Set(string, string)
}

ParamsSetter is the interface which should be implemented by the params writer for `Search` in order to store the found named path parameters, if any.

type SessionStat

type SessionStat struct {
	PageName      string
	TotalSessions int
	Sessions      map[string]time.Time
}

type Trie

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

Trie contains the main logic for adding and searching nodes for path segments. It supports wildcard and named path parameters. Trie supports very coblex and useful path patterns for routes. The Trie checks for static paths(path without : or *) and named parameters before that in order to support everything that other implementations do not, and if nothing else found then it tries to find the closest wildcard path(super and unique).

func NewTrie

func NewTrie() *Trie

NewTrie returns a new, empty Trie. It is only useful for end-developers that want to design their own mux/router based on my trie implementation.

See `Trie`

func (*Trie) Autocomplete

func (t *Trie) Autocomplete(prefix string, sorter NodeKeysSorter) (list []string)

Autocomplete returns the keys that starts with "prefix", this is useful for custom search-engines built on top of my trie implementation.

func (*Trie) HasPrefix

func (t *Trie) HasPrefix(prefix string) bool

HasPrefix returns true if "prefix" is found inside the registered nodes.

func (*Trie) Insert

func (t *Trie) Insert(pattern string, options ...InsertOption)

Insert adds a node to the trie.

func (*Trie) Parents

func (t *Trie) Parents(prefix string) (parents []*Node)

Parents returns the list of nodes that a node with "prefix" key belongs to.

func (*Trie) Search

func (t *Trie) Search(q string, params ParamsSetter) *Node

Search is the most important part of the Trie. It will try to find the responsible node for a specific query (or a request path for HTTP endpoints).

Search supports searching for static paths(path without : or *) and paths that contain named parameters or wildcards. Priority as: 1. static paths 2. named parameters with ":" 3. wildcards 4. closest wildcard if not found, if any 5. rootPath wildcard

func (*Trie) SearchPrefix

func (t *Trie) SearchPrefix(prefix string) *Node

SearchPrefix returns the last node which holds the key which starts with "prefix".

Jump to

Keyboard shortcuts

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