milo

package module
v0.0.0-...-6070897 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2017 License: MIT Imports: 19 Imported by: 3

README

milo

A go based web framework.

There are a few goals to the milo project.

  1. Use as much of the default net/http package as possible
    • Register static routes for assets
  2. Work with a specific go http design pattern
  3. Routing Helpers
    • Provide a wrapper for routing to plug pre & post middleware into
    • Helpers for handling 404
    • Helpers for handling errors
  4. Response Rendering
    • Rendering multiple templates
    • Rendering JSON output
    • Rendering error output

Documentation

Index

Constants

View Source
const (
	SessFlash    = "session_flash"
	FlashError   = "flasherror"
	FlashSuccess = "flashsuccess"
)
View Source
const (
	BIND_ERR = "bind: address already in use"
)

Variables

This section is empty.

Functions

func Gravatar

func Gravatar(email string, s int) string

Gravatar builds links to gravatar.

func Host

func Host(r *http.Request) string

Get the host for the given http request. Can be used like {{ host .request }}

func IdFromContext

func IdFromContext(ctx context.Context) (string, bool)

func Marshal

func Marshal(v interface{}) template.JS

Get a json encoding of an object from the backend. Can be used like {{ marshal .user }}

func SetBind

func SetBind(bind string) func(*Milo) error

Configuration option to change the bind address.

func SetPort

func SetPort(port int) func(*Milo) error

Configuration option to change the bind port.

func SetPortInc

func SetPortInc(inc bool) func(*Milo) error

Configuration option to change the port increment.

func Title

func Title(word string) string

Title gets the title for the word.

func TokenFromContext

func TokenFromContext(ctx context.Context) (string, bool)

Types

type AuthBase

type AuthBase struct {
	*FlashBase
	// contains filtered or unexported fields
}

func NewAuthBase

func NewAuthBase(fb *FlashBase, ac AuthCheck, loginURL string) *AuthBase

func NewAuthBaseCustom

func NewAuthBaseCustom(fb *FlashBase, ac AuthCheck, loginURL string, authKey string, xToken string) *AuthBase

func (*AuthBase) AuthMiddleware

func (ab *AuthBase) AuthMiddleware(fn http.HandlerFunc, overrideAuthCheck ...AuthCheck) http.HandlerFunc

func (*AuthBase) AuthMiddlewareCookie

func (ab *AuthBase) AuthMiddlewareCookie(fn http.HandlerFunc, overrideAuthCheck ...AuthCheck) http.HandlerFunc

func (*AuthBase) AuthMiddlewareToken

func (ab *AuthBase) AuthMiddlewareToken(fn http.HandlerFunc, overrideAuthCheck ...AuthCheck) http.HandlerFunc

func (*AuthBase) DoLogin

func (ab *AuthBase) DoLogin(w http.ResponseWriter, r *http.Request, id string) error

func (*AuthBase) DoLogout

func (ab *AuthBase) DoLogout(w http.ResponseWriter, r *http.Request) error

type AuthCheck

type AuthCheck interface {
	IsValid(id string) (bool, error)
	IsTokenValid(token string) (bool, error)
}

type Configer

type Configer interface {
	GetConfig(key string) interface{}
}

An interface to define a way to get config items always into template rendering

type FlashBase

type FlashBase struct {
	*Renderer
	// contains filtered or unexported fields
}

func NewFlashBase

func NewFlashBase(r *Renderer, s sessions.Store) *FlashBase

func (*FlashBase) GetFlashes

func (fb *FlashBase) GetFlashes(w http.ResponseWriter, r *http.Request) ([]interface{}, []interface{})

func (*FlashBase) RenderTemplates

func (fb *FlashBase) RenderTemplates(w http.ResponseWriter, r *http.Request, data map[string]interface{}, tpls ...string)

func (*FlashBase) SetErrorFlash

func (fb *FlashBase) SetErrorFlash(w http.ResponseWriter, r *http.Request, message string)

func (*FlashBase) SetSuccessFlash

func (fb *FlashBase) SetSuccessFlash(w http.ResponseWriter, r *http.Request, message string)

type Milo

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

This is the default application.

func NewMiloApp

func NewMiloApp(opts ...func(*Milo) error) *Milo

Create a new milo app. Uses the config object.

func (*Milo) PathPrefix

func (m *Milo) PathPrefix(path string, methods []string, hf http.HandlerFunc)

Setup a route to be executed when the specific path prefix is matched, uses the gorilla mux router.

func (*Milo) RegisterAfter

func (m *Milo) RegisterAfter(mw MiloMiddlware)

Add after request middleware to the global middleware stack.

func (*Milo) RegisterBefore

func (m *Milo) RegisterBefore(mw MiloMiddlware)

Add before request middleware to the global middlware stack.

func (*Milo) RegisterDefaultErrorHandler

func (m *Milo) RegisterDefaultErrorHandler(h http.HandlerFunc)

Register an error handler for when things go crazy.

func (*Milo) RegisterLogger

func (m *Milo) RegisterLogger(l MiloLogger)

Register your own implementation of the milo logger.

func (*Milo) RegisterNotFound

func (m *Milo) RegisterNotFound(h http.HandlerFunc)

Register a not found handler so you can capture 404 errors.

func (*Milo) Route

func (m *Milo) Route(path string, methods []string, hf http.HandlerFunc)

Setup a route to be executed when the path is matched, uses the gorilla mux router.

func (*Milo) RouteAsset

func (m *Milo) RouteAsset(prefix, dir string)

Handle assets rooted in different directories.

func (*Milo) RouteAssetStripPrefix

func (m *Milo) RouteAssetStripPrefix(prefix, dir string)

Handle assets rooted in different directories, strips prefix.

func (*Milo) RouteWebsocket

func (m *Milo) RouteWebsocket(path string, hf func(ws *websocket.Conn))

Handling websocket connection.

func (*Milo) Run

func (m *Milo) Run()

Binds and runs the application on the given config port.

func (*Milo) ServeHTTP

func (m *Milo) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP as passed into the notfoundhandler.

func (*Milo) String

func (m *Milo) String() string

Stringer implementation

func (*Milo) SubRoute

func (m *Milo) SubRoute(prefix, path string, methods []string, hf http.HandlerFunc)

Setup sub routes for more efficient routing of requests inside of gorilla mux.

type MiloLogger

type MiloLogger interface {
	Log(message string)
	LogError(err error)
	LogInterfaces(items ...interface{})
	LogFatal(items ...interface{})
	LogStackTrace()
}

Methods to specify a logger for the milo app.

type MiloMiddlware

type MiloMiddlware func(w http.ResponseWriter, r *http.Request) bool

type MsgRender

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

func NewMsgRender

func NewMsgRender(tplDir string) *MsgRender

func (*MsgRender) RegisterTemplateFunc

func (m *MsgRender) RegisterTemplateFunc(key string, fn interface{})

func (*MsgRender) Render

func (m *MsgRender) Render(data interface{}, tpls ...string) (string, error)

func (*MsgRender) RenderHtml

func (m *MsgRender) RenderHtml(data interface{}, tpls ...string) (string, error)

type Renderer

type Renderer struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Default milo renderer that can cache templates, sets a base template directory.

func NewRenderer

func NewRenderer(tplDir string, cache bool, configer Configer) *Renderer

Create a new default milo renderer.

func (*Renderer) Partial

func (mr *Renderer) Partial(name string, payload interface{}) (template.HTML, error)

A template function which can include a partial template.

func (*Renderer) Redirect

func (mr *Renderer) Redirect(w http.ResponseWriter, r *http.Request, url string, code int)

Setup an http redirect on the request.

func (*Renderer) RegisterTemplateFunc

func (mr *Renderer) RegisterTemplateFunc(key string, fn interface{})

Register a template function with the MiloRenderer

func (*Renderer) RenderError

func (mr *Renderer) RenderError(w http.ResponseWriter, r *http.Request, code int, message string)

func (*Renderer) RenderJson

func (mr *Renderer) RenderJson(w http.ResponseWriter, r *http.Request, data interface{})

Render json output

func (*Renderer) RenderMessage

func (mr *Renderer) RenderMessage(w http.ResponseWriter, r *http.Request, message string)

func (*Renderer) RenderTemplates

func (mr *Renderer) RenderTemplates(w http.ResponseWriter, r *http.Request, data map[string]interface{}, tpls ...string)

Takes care of rendering templates from file, passes a status 200.

func (*Renderer) RenderTemplatesCode

func (mr *Renderer) RenderTemplatesCode(w http.ResponseWriter, r *http.Request, code int, data map[string]interface{}, tpls ...string)

Takes the care of rendering templates, with an explicit status code.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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