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 ¶
- func SetInfos(arg InfosT)
- func ThisFunc() *runtime.Func
- type Info
- type InfosT
- func (l *InfosT) ByHandlerFunc(argFunc http.Handler) (Info, error)
- func (l *InfosT) ByKey(argKey string) Info
- func (l *InfosT) ByKeyTranslated(argKey, langCode string) Info
- func (l *InfosT) ByRuntimeFunc(argFunc *runtime.Func) Info
- func (l *InfosT) MakeKeys()
- func (l *InfosT) URLByKey(argKey string) string
- type Privilege
- type TreeT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
RequiresAdmin checks if a handler requires admin
type InfosT ¶
type InfosT []Info
InfosT models a collection of all app specific handlers
func (*InfosT) ByHandlerFunc ¶
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) ByKeyTranslated ¶
ByKeyTranslated retrieves a handlerinfo by key and translates the title
func (*InfosT) ByRuntimeFunc ¶
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.
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 ¶
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 ¶
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 ¶
ByKey recursively retrieves a node; use ByKey() and SetByKey() to modify the nav tree
func (*TreeT) NavHTML ¶
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>