handler

package
v0.0.0-...-fe173bb Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package handler contains metadata struct for http handlers; urls, titles, privileges and navigation menu info. It cannot be part of package handlers, since this would cause cyclical dependencies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetInfos

func SetInfos(arg InfosT)

SetInfos saves arg as package variable infos

func ThisFunc

func ThisFunc() *runtime.Func

ThisFunc returns a runtime func info

Types

type Info

type Info struct {
	//
	Keys    []string `json:"keys,omitempty"` // identifier - does not change as URL or title - dynamically derived from the handlerfunc pointer - if not set, then autogenerated from title
	Urls    []string `json:"urls,omitempty"` // first is idiomatic
	OnClick string   `json:"on_click,omitempty"`
	Methods []string `json:"methods,omitempty"` // GET POST ...

	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"` // Meta stuff

	Handler http.HandlerFunc `json:"-"`

	ShortCut string `json:"short_cut,omitempty"` // accesskey attribute in main navi

	// also determines showing up in navigation
	Allow map[Privilege]bool `json:"-"`

	Active bool `json:"-"` // per menu rendering per request/thread
}

Info and Infos keep all the meta information about routes, such as Title, Description, Url(s), [GET,POST,PUT]. They also contain the related handlerFunc. Last not least, they are injected into the template engine, so that URLs are dynamically fetched via {{ index ( byKey "val-delete" ).Urls 0 }}

func (*Info) HasKey

func (l *Info) HasKey(argKey string) bool

HasKey checks an info for some key. Usage in templates, where 'myKey' is known, to determine from a list of HIs, which one "isActive".

func (*Info) RequiresAdmin

func (l *Info) RequiresAdmin() bool

RequiresAdmin checks if a handler requires admin

type InfosT

type InfosT []Info

InfosT models a collection of all app specific handlers

func Infos

func Infos() *InfosT

Infos returns all handlers

func (*InfosT) ByHandlerFunc

func (l *InfosT) ByHandlerFunc(argFunc http.Handler) (Info, error)

ByHandlerFunc returns the handlerinfo for a handler func from outside the request.

handlerFnc, canonPath := mux.Handler(req)
hi, err := handler.HIs.ByHandlerFunc(handlerFnc)

Returns an error, if no handlerinfo exists.

func (*InfosT) ByKey

func (l *InfosT) ByKey(argKey string) Info

ByKey retrieves a handlerinfo by key

func (*InfosT) ByKeyTranslated

func (l *InfosT) ByKeyTranslated(argKey, langCode string) Info

ByKeyTranslated retrieves a handlerinfo by key and translates the title

func (*InfosT) ByRuntimeFunc

func (l *InfosT) ByRuntimeFunc(argFunc *runtime.Func) Info

ByRuntimeFunc retrieves a handlerinfo from *inside* a request handlerFunc:

handler.Infos().ByRuntimeFunc(util.ThisFunc())

Handler funcs - similar to *this* in javaScript can now be retrieved with

myLink := links.Links.ByRuntimeFunc( util.ThisFunc() )

We use

reflect.ValueOf().Pointer() -

and

runtime.Caller() => program counter address => runtime.FuncForPc(pca).Entry()

to compare the two program counter addresses.

I am amazed that this works. Gives every handler access to *its* info object.

func (*InfosT) MakeKeys

func (l *InfosT) MakeKeys()

MakeKeys generates keys, if none exists yet

func (*InfosT) URLByKey

func (l *InfosT) URLByKey(argKey string) string

URLByKey directly returns canonical URL by key

type Privilege

type Privilege int

Privilege encodes some

const (
	// ReadOnly means no saving operation
	ReadOnly Privilege = iota
	// LoggedOut -> only show if no user is logged in
	LoggedOut
	// LoggedIn requires login
	LoggedIn
	// LoggedInViaJSON - must be logged in - and provider must be JSON
	LoggedInViaJSON
	// Admin has all rights
	Admin
	// Editor can save values
	Editor
	// ForwardCopyAllTabs can perform mass copy operations
	ForwardCopyAllTabs
)

type TreeT

type TreeT struct {
	Node     Info // Info may contain just a title string => not clickable
	Children []TreeT
}

TreeT stores nested handler.Info instances

func Tree

func Tree(lc string) *TreeT

Tree returns an application specific nav tree of handler.Info based on which a navigation can be rendered; we dont use a package variable, since the nav tree may be modified by requests

func (*TreeT) AppendAfterByKey

func (tr *TreeT) AppendAfterByKey(key string, summand *Info, asChild ...bool) bool

AppendAfterByKey recursively appends behind an existing node asChild == false => append as sibling - same level asChild == true => append as child - one level deeper

func (*TreeT) ByKey

func (tr *TreeT) ByKey(key string) *TreeT

ByKey recursively retrieves a node; use ByKey() and SetByKey() to modify the nav tree

func (*TreeT) NavHTML

func (tr *TreeT) NavHTML(w io.Writer, r *http.Request, isLogin bool, l *lgn.LoginT, lvl int)

NavHTML - renders a navigation tree into the HTML base snippet below; it is called from templates via template func {{nav .Req}} being mapped to tpl.fcNav

<nav>

<!-- logo text or image are part of the design; they are set via CSS -->
<div class="logo">
</div>

<!-- burger checkbox - out of sight - still can be tabbed and get focus -->
<input type="checkbox" id="mnu-1st-lvl-toggler" class="mnu-1st-lvl-toggler">

<!-- burger label -->
<label for="mnu-1st-lvl-toggler" class="burger">
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
</label>

<!-- menu second level-->
<ul class="mnu-2nd-lvl">

    {{ .Content}}

    <li><a href="#">Home</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">Contact</a></li>

    <li class='nde-2nd-lvl'>
        <a href='#' onclick='return false;'>Sys Admin</a>

        <ul class='mnu-3rd-lvl'>
            <li><a href='/login-primitive' class=''>Login app</a></li>
            <li><a href='/login-appengine' class=''>Login appengine</a></li>
            <li><a href='/google-token-signin' class=''>Google token sign-in</a></li>
        </ul>
    </li>
    <li class='nde-2nd-lvl'>
        <a href="#">About</a>

        <ul class='mnu-3rd-lvl'>
            <li><a href='/login-primitive' class=''>More</a></li>
            <li><a href='/login-primitive' class=''>Subitems</a></li>
        </ul>

    </li>

</ul>

</nav>

func (*TreeT) SetByKey

func (tr *TreeT) SetByKey(key string, repl *Info) bool

SetByKey recursively replaces an existing node; use ByKey() and SetByKey() to modify the nav tree

Jump to

Keyboard shortcuts

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