scaffold

package module
v0.0.0-...-464f0ff Latest Latest
Warning

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

Go to latest
Published: May 8, 2016 License: MIT Imports: 7 Imported by: 6

README

Scaffold

Simple context aware router

GoDoc Coverage Status Build Status Go Report Card

Scaffold is a simple router with middleware support. It allows for alternate dispatchers.

The default dispatcher can parse basic patterns in the form /segment/:param/segment. Parameters can be accessed in the Handler/Middleware using GetParam.

Example:

dispatcher := scaffold.DefaultDispatcher()
router := scaffold.New(dispatcher)

// More specific routes have precedence
router.Host("example.com").Get("", somehandler)
router.Get("", somehandler)

http.ListenAndServe(":8080", dispatcher)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotFound

func NotFound(ctx context.Context, w http.ResponseWriter, r *http.Request)

NotFound wraps http.NotFound

func StoreParam

func StoreParam(ctx context.Context, name string, value string) context.Context

StoreParam stores a variable in a context

func URLPart

func URLPart(ctx context.Context, r *http.Request, i int) (context.Context, string, bool)

URLPart returns a part of the url and caches it in the context

func URLParts

func URLParts(ctx context.Context, r *http.Request) (context.Context, []string)

URLParts spliths a path into parts and caches it in the context

Types

type Dispatcher

type Dispatcher interface {
	http.Handler
	Handler

	Handle(route Route, handlers ...Handler)
	Middleware(route Route, middleware ...Middleware)
	NotFoundHandler(route Route, handler Handler)
}

Dispatcher dipatches requests to routes

func DefaultDispatcher

func DefaultDispatcher() Dispatcher

DefaultDispatcher implements dispatcher allowing for basic url params

type Handler

type Handler interface {
	CtxServeHTTP(context.Context, http.ResponseWriter, *http.Request)
}

Handler is a context aware handler

var NotFoundHandler Handler = HandlerFunc(NotFound)

NotFoundHandler is a not found handler

func Scaffold

func Scaffold(d Dispatcher, platform Platform) Handler

Scaffold creates a router and passes it to a platorm

type HandlerFunc

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)

HandlerFunc wraps a handler letting it implement Handler

func (HandlerFunc) CtxServeHTTP

func (h HandlerFunc) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)

CtxServeHTTP implements Handler.CtxServeHTTP

type HandlerList

type HandlerList []Handler

HandlerList is an array of handlers

func (HandlerList) CtxServeHTTP

func (h HandlerList) CtxServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request)

CtxServeHTTP implements Handler.CtxServeHTTP

type Middleware

type Middleware func(next Handler) Handler

Middleware is middleware for a router

func MiddlewareList

func MiddlewareList(middlware []Middleware) Middleware

MiddlewareList list creates a single middlware callback from an array

type Param

type Param string

Param is a parameter

func GetParam

func GetParam(ctx context.Context, name string) Param

GetParam retrieves a param from a context

func (Param) Float32

func (p Param) Float32() (float32, error)

Float32 returns param as float32

func (Param) Float64

func (p Param) Float64() (float64, error)

Float64 returns param as float64

func (Param) Int

func (p Param) Int() (int, error)

Int returns param as int

func (Param) Int32

func (p Param) Int32() (int32, error)

Int32 returns param as int32

func (Param) Int64

func (p Param) Int64() (int64, error)

Int64 returns param as int64

func (Param) String

func (p Param) String() (string, error)

String returns param as string

func (Param) UInt

func (p Param) UInt() (uint, error)

UInt returns param as uint

func (Param) UInt32

func (p Param) UInt32() (uint32, error)

UInt32 returns param as uint32

func (Param) UInt64

func (p Param) UInt64() (uint64, error)

UInt64 returns param as uint64

type Platform

type Platform interface {
	Routes(*Router)
}

Platform is a object that can specify its own routes

type Route

type Route struct {
	Hosts   []string
	Pattern string
	Method  string
}

Route is a route used in handling requests

type Router

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

Router is a HTTP router

func New

func New(d Dispatcher) *Router

New creates new router

func (*Router) AddHandlerBuilder

func (r *Router) AddHandlerBuilder(builder func(interface{}) (Handler, error))

AddHandlerBuilder adds a builder to construct handlers

func (*Router) Delete

func (r *Router) Delete(pattern string, handlers ...interface{}) *Router

Delete handles DELETE methods with a given pattern

func (*Router) Get

func (r *Router) Get(pattern string, handlers ...interface{}) *Router

Get handles GET methods with a given pattern

func (*Router) Group

func (r *Router) Group(pattern string, group func(*Router))

Group calls the specified function with the subrouter for the given pattern

func (*Router) Handle

func (r *Router) Handle(pattern string, handlers ...interface{}) *Router

Handle all methods with a given pattern

func (*Router) Head

func (r *Router) Head(pattern string, handlers ...interface{}) *Router

Head handles HEAD methods with a given pattern

func (*Router) Host

func (r *Router) Host(hosts ...string) *Router

Host lets you specify the host(s) the route is available for

func (*Router) NotFound

func (r *Router) NotFound(i interface{})

NotFound specifys a not found handler for a route

func (*Router) Options

func (r *Router) Options(pattern string, handlers ...interface{}) *Router

Options handles OPTIONS methods with a given pattern

func (*Router) Platform

func (r *Router) Platform(pattern string, platform Platform)

Platform routes the platform object to the given pattern

func (*Router) Post

func (r *Router) Post(pattern string, handlers ...interface{}) *Router

Post handles POST methods with a given pattern

func (*Router) Put

func (r *Router) Put(pattern string, handlers ...interface{}) *Router

Put handles PUT methods with a given pattern

func (*Router) Route

func (r *Router) Route(pattern string) *Router

Route returns the subrouter for a pettern

func (*Router) Use

func (r *Router) Use(middleware ...interface{})

Use attaches middleware to a route

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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