Documentation ¶
Index ¶
- Constants
- func CleanPath(path string) string
- func IsEmptyRoutingError(re RoutingError) bool
- func Log(v ...interface{})
- func ServeFile(ctx *HandlerContext, filename string, statusCode int)
- type Bundle
- type CacheEntry
- type Chain
- func (ch *Chain) Append(handlers ...ChainHandler) Chain
- func (ch Chain) Call() ContextHandlerFunc
- func (ch Chain) Copy() Chain
- func (ch *Chain) Error(ctx *HandlerContext, chainError error, statusCode int)
- func (ch *Chain) Halt(ctx *HandlerContext)
- func (ch *Chain) Next(ctx *HandlerContext)
- func (ch Chain) Then(handler ContextHandlerFunc) ContextHandlerFunc
- type ChainCompletedFunc
- type ChainHandler
- type ChainResult
- type ChainStatus
- type CheckedResponseWriter
- type ContextHandler
- type ContextHandlerFunc
- type DebugLevel
- type DomainMap
- type ErrorInfoMap
- type Filter
- type Group
- func (g *Group) Add(method string, routePath string) *Route
- func (g *Group) DELETE(routePath string) *Route
- func (g *Group) GET(routePath string) *Route
- func (g *Group) Group(prefix string) *Group
- func (g *Group) HEAD(routePath string) *Route
- func (g *Group) OPTIONS(routePath string) *Route
- func (g *Group) PATCH(routePath string) *Route
- func (g *Group) POST(routePath string) *Route
- func (g *Group) PUT(routePath string) *Route
- type HandlerContext
- func (hc *HandlerContext) ClearState(key string)
- func (hc *HandlerContext) ErrorForStatus(status int) RoutingError
- func (hc *HandlerContext) FirstError() RoutingError
- func (hc *HandlerContext) GetState(key string) interface{}
- func (hc *HandlerContext) GetStateInt(key string) int
- func (hc *HandlerContext) GetStateString(key string) string
- func (hc *HandlerContext) HasError() bool
- func (hc *HandlerContext) HasErrorForStatus(status int) bool
- func (hc *HandlerContext) SetState(key string, value interface{})
- type Match
- type MatchCandidate
- type Matcher
- type Node
- type Parameters
- type Route
- type RouteCache
- type RouteComponentType
- type Router
- func (r *Router) Add(method string, routePath string) *Route
- func (r *Router) AddBundle(bundle Bundle)
- func (r *Router) AddFilter(filter Filter)
- func (r *Router) AddStatic(staticRoot string)
- func (r *Router) AddStaticWithPrefix(staticRoot string, prefix string)
- func (r *Router) GET(routePath string) *Route
- func (r *Router) Group(prefix string) *Group
- func (r *Router) HC(handlers ...ChainHandler) Chain
- func (r *Router) NewChain(handlers ...ChainHandler) Chain
- func (r *Router) NewMatcher() *Matcher
- func (r *Router) POST(routePath string) *Route
- func (r *Router) PUT(routePath string) *Route
- func (r *Router) PrintRoutes()
- func (r *Router) PrintTreeInfo()
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) SetAlwaysUseFirstMatch(alwaysUseFirst bool)
- func (r *Router) SetDebugLevel(debugLevel DebugLevel)
- func (r *Router) SetErrorHandler(statusCode int, handler ContextHandler)
- func (r *Router) SetGlobalHandler(method string, handler ContextHandler)
- func (r *Router) SetMethodNotAllowedIsError(isError bool)
- func (r *Router) SetRouterErrorHandler(handler ContextHandler)
- func (r *Router) SetStringVariable(variable string, value string)
- func (r *Router) Use(routes ...*Route) []*Route
- type RouterErrorCode
- type RoutingError
- type StaticLocation
- type Tree
Constants ¶
const ( StateKeyHasExecutedPreFilters = "_goro.skey.hasExecutedPreFilters" StateKeyHasExecutedPostFilters = "_goro.skey.hasExecutedPostFilters" )
const ( // RouteInfoKeyIsRoot - does the route have wildcard parts RouteInfoKeyIsRoot string = "is_root" // RouteInfoKeyDescription - does the route have a catch all part RouteInfoKeyDescription string = "description" )
const DomainMapNakedSubdomainKey = ":naked"
const DomainMapWildcardSubdomainKey = ":wildcard"
const RootPath = "/"
RootPath - string representation of the root path
Variables ¶
This section is empty.
Functions ¶
func CleanPath ¶ added in v0.2.0
CleanPath - returns a path value with the following modifications:
- replaces any '\' with '/'
- replaces any '//' with '/'
- adds a leading '/' (if missing)
func IsEmptyRoutingError ¶ added in v0.2.0
func IsEmptyRoutingError(re RoutingError) bool
func ServeFile ¶ added in v0.2.0
func ServeFile(ctx *HandlerContext, filename string, statusCode int)
ServeFile replies to the request with the contents of the named file.
This implementation is different to the standard library implementation in that it doesn't concern itself with directories, and does not account for redirecting paths ending in /index.html. If you wish to retain that functionality, you should set up your routes accordingly.
ServeFile will only catch common errors, for example 404s or access related errors and wont catch lower-level errors. Due to the implementation details in the Go standard library (which we still rely on) some of those errors will fall through to the standard error reporting mechanisms.
Types ¶
type CacheEntry ¶
type CacheEntry struct { Params map[string]interface{} Route *Route // contains filtered or unexported fields }
CacheEntry - an entry in the route cache
func NotFoundCacheEntry ¶
func NotFoundCacheEntry() CacheEntry
NotFoundCacheEntry - represents the inability to find an entry in the cache
type Chain ¶
type Chain struct { // RouterCatchesErrors - if true and the chain is attached to a router then // errors will bubble up to the router error handler RouterCatchesErrors bool // EmitHTTPError - if true, the router will emit an http.Error when the chain // result is an error EmitHTTPError bool // ChainCompletedFunc - called when chain completes ChainCompletedFunc ChainCompletedFunc // contains filtered or unexported fields }
Chain allows for chaining of Handlers
func HC ¶ added in v0.2.0
func HC(router *Router, handlers ...ChainHandler) Chain
func NewChain ¶
func NewChain(router *Router, handlers ...ChainHandler) Chain
NewChain - creates a new Chain instance
func (*Chain) Append ¶ added in v0.2.0
func (ch *Chain) Append(handlers ...ChainHandler) Chain
Append - returns a new chain with the ChainHandler appended to the list of handlers
func (Chain) Call ¶ added in v0.2.0
func (ch Chain) Call() ContextHandlerFunc
Call - calls the chain
func (*Chain) Error ¶ added in v0.2.0
func (ch *Chain) Error(ctx *HandlerContext, chainError error, statusCode int)
Error - halt the chain and report an error
func (*Chain) Halt ¶ added in v0.2.0
func (ch *Chain) Halt(ctx *HandlerContext)
Halt - halt chain execution
func (*Chain) Next ¶ added in v0.2.0
func (ch *Chain) Next(ctx *HandlerContext)
Next - execute the next handler in the chain
func (Chain) Then ¶
func (ch Chain) Then(handler ContextHandlerFunc) ContextHandlerFunc
Then - calls the chain and then the designated Handler
type ChainCompletedFunc ¶ added in v0.2.0
type ChainCompletedFunc func(result ChainResult)
ChainCompletedFunc - callback function executed when chain execution has completed
type ChainHandler ¶ added in v0.2.0
type ChainHandler func(*Chain, *HandlerContext)
type ChainResult ¶ added in v0.2.0
type ChainResult struct { Status ChainStatus Error error StatusCode int }
ChainResult - the chain execution result
type ChainStatus ¶ added in v0.2.0
type ChainStatus int
ChainStatus - the status of the chain
const ( // ChainCompleted - the chain completed normally ChainCompleted ChainStatus = 1 << iota // ChainError - the chain was stopped because of an error ChainError // ChainHalted - the chain was halted before it could finish executing ChainHalted )
type CheckedResponseWriter ¶ added in v0.2.0
type CheckedResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
func NewCheckedResponseWriter ¶ added in v0.2.0
func NewCheckedResponseWriter(w http.ResponseWriter) *CheckedResponseWriter
func (*CheckedResponseWriter) Write ¶ added in v0.2.0
func (w *CheckedResponseWriter) Write(b []byte) (int, error)
func (*CheckedResponseWriter) WriteHeader ¶ added in v0.2.0
func (w *CheckedResponseWriter) WriteHeader(status int)
type ContextHandler ¶ added in v0.2.0
type ContextHandler interface {
Serve(ctx *HandlerContext)
}
ContextHandler - the standard Goro handler
type ContextHandlerFunc ¶ added in v0.3.0
type ContextHandlerFunc func(ctx *HandlerContext)
ContextHandlerFunc - the standard Goro handler
func (ContextHandlerFunc) Serve ¶ added in v0.3.0
func (chf ContextHandlerFunc) Serve(ctx *HandlerContext)
Serve - implement the ContextHandler interface
type DebugLevel ¶
type DebugLevel int
DebugLevel - Debug information output level
const ( // DebugLevelNone - debugging is off DebugLevelNone DebugLevel = 1 << iota // DebugLevelTimings - show timings only DebugLevelTimings // DebugLevelFull - show all debugging information DebugLevelFull )
type DomainMap ¶
type DomainMap struct { // NotFoundHandler - if the (sub)domain is not mapped, call this handler NotFoundHandler ContextHandlerFunc // contains filtered or unexported fields }
DomainMap - maps (sub)domains to routers
func NewDomainMap ¶
NewDomainMap - creates a new domain map for the provided domains
func (*DomainMap) NewRouter ¶
NewRouter - Creates a new Router, registers it in the domain map and returns it for use
func (*DomainMap) NewRouters ¶ added in v0.2.0
NewRouters - Creates a new Router for each of the defined subdomains and registers it with the DomainMap
type ErrorInfoMap ¶ added in v0.2.0
type ErrorInfoMap map[string]interface{}
type Filter ¶
type Filter interface { ExecuteBefore(ctx *HandlerContext) ExecuteAfter(ctx *HandlerContext) }
Filter is an interface that can be registered on the Router to apply custom logic to modify the Request or calling Context
type Group ¶ added in v0.2.0
type Group struct {
// contains filtered or unexported fields
}
func (*Group) Add ¶ added in v0.2.0
Add creates a new Route and registers the instance within the Router
func (*Group) DELETE ¶ added in v0.2.0
Add creates a new Route using the DELETE method and registers the instance within the Router
func (*Group) GET ¶ added in v0.2.0
Add creates a new Route using the GET method and registers the instance within the Router
func (*Group) HEAD ¶ added in v0.2.0
Add creates a new Route using the HEAD method and registers the instance within the Router
func (*Group) OPTIONS ¶ added in v0.2.0
Add creates a new Route using the OPTIONS method and registers the instance within the Router
func (*Group) PATCH ¶ added in v0.2.0
Add creates a new Route using the PATCH method and registers the instance within the Router
type HandlerContext ¶ added in v0.2.0
type HandlerContext struct { sync.RWMutex Request *http.Request ResponseWriter http.ResponseWriter Parameters *Parameters Meta map[string]interface{} Path string CatchAllValue string Errors []RoutingError // contains filtered or unexported fields }
func NewHandlerContext ¶ added in v0.2.0
func NewHandlerContext(request *http.Request, responseWriter http.ResponseWriter, router *Router) *HandlerContext
func (*HandlerContext) ClearState ¶ added in v0.2.0
func (hc *HandlerContext) ClearState(key string)
func (*HandlerContext) ErrorForStatus ¶ added in v0.2.0
func (hc *HandlerContext) ErrorForStatus(status int) RoutingError
func (*HandlerContext) FirstError ¶ added in v0.2.0
func (hc *HandlerContext) FirstError() RoutingError
func (*HandlerContext) GetState ¶ added in v0.2.0
func (hc *HandlerContext) GetState(key string) interface{}
func (*HandlerContext) GetStateInt ¶ added in v0.3.1
func (hc *HandlerContext) GetStateInt(key string) int
func (*HandlerContext) GetStateString ¶ added in v0.3.1
func (hc *HandlerContext) GetStateString(key string) string
func (*HandlerContext) HasError ¶ added in v0.2.0
func (hc *HandlerContext) HasError() bool
func (*HandlerContext) HasErrorForStatus ¶ added in v0.2.0
func (hc *HandlerContext) HasErrorForStatus(status int) bool
func (*HandlerContext) SetState ¶ added in v0.2.0
func (hc *HandlerContext) SetState(key string, value interface{})
type Match ¶
Match represents a matched node in the tree
func NewMatchWithParent ¶
NewMatchWithParent creates a new instance of a match that passes down the values from the parent match
type MatchCandidate ¶
type MatchCandidate struct {
// contains filtered or unexported fields
}
MatchCandidate is a helper class for matching path components
func NewMatchCandidate ¶
func NewMatchCandidate(path string) MatchCandidate
NewMatchCandidate creates a new match candidate instance and initializes if for the first part
func NoMatchCandidate ¶
func NoMatchCandidate() MatchCandidate
NoMatchCandidate represents an empty MatchCandidate
func (MatchCandidate) HasRemainingCandidates ¶
func (mc MatchCandidate) HasRemainingCandidates() bool
HasRemainingCandidates returns true if the MatchCandidate has more candidate parts
func (MatchCandidate) IsNoMatch ¶
func (mc MatchCandidate) IsNoMatch() bool
IsNoMatch returns true if the MatchCandidate equals the NoMatchCandidate value
func (MatchCandidate) NextCandidate ¶
func (mc MatchCandidate) NextCandidate() MatchCandidate
NextCandidate returns the next MatchCandidate in the full path
type Matcher ¶
type Matcher struct { LogMatchTime bool FallbackToCatchAll bool // contains filtered or unexported fields }
Matcher is the global matching engine
func NewMatcher ¶
NewMatcher creates a new instance of the Matcher
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node - tree node to store route information
func (*Node) HasChildren ¶
HasChildren - returns true if the Node has 1 or more sub-Nodes
func (*Node) RouteForMethod ¶
RouteForMethod - returns the route that was defined for the method or nil if no route is defined
type Parameters ¶ added in v0.2.0
type Parameters struct {
// contains filtered or unexported fields
}
func NewParametersWithMap ¶ added in v0.2.0
func NewParametersWithMap(paramsMap map[string][]string) *Parameters
func (*Parameters) Get ¶ added in v0.2.0
func (p *Parameters) Get(key string) []interface{}
func (*Parameters) GetFirstString ¶ added in v0.2.0
func (p *Parameters) GetFirstString(key string) string
func (*Parameters) GetInt ¶ added in v0.2.0
func (p *Parameters) GetInt(key string) int
func (*Parameters) GetInts ¶ added in v0.2.0
func (p *Parameters) GetInts(key string) []int
func (*Parameters) GetStrings ¶ added in v0.2.0
func (p *Parameters) GetStrings(key string) []string
type Route ¶
type Route struct { Method string Path string PathFormat string Handler ContextHandler Meta map[string]interface{} Info map[string]interface{} }
Route stores all the information about a route
func NewRouteWithMeta ¶
NewRouteWithMeta creates a new Route instance with meta values
func (*Route) Handle ¶
func (rte *Route) Handle(handlerFunc ContextHandler) *Route
Handle adds a ContextHandler to the Route
func (*Route) HandleFunc ¶
func (rte *Route) HandleFunc(handlerFunc ContextHandlerFunc) *Route
HandleFunc adds a ContextHandlerFunc to the Route
type RouteCache ¶
type RouteCache struct { // Entries - ordered list of cache entries Entries []CacheEntry // MaxEntries - maximum number of items permitted in the cache MaxEntries int // ReorderOnAccess - move the last accessed item to the top ReorderOnAccess bool // contains filtered or unexported fields }
RouteCache - temporary storage for routes
func NewRouteCache ¶
func NewRouteCache() *RouteCache
NewRouteCache - creates a new default RouteCache
func (*RouteCache) Get ¶
func (rc *RouteCache) Get(path string) CacheEntry
Get - fetch a cache entry (if exists)
func (*RouteCache) Put ¶
func (rc *RouteCache) Put(path string, entry CacheEntry)
Put - add an item to the route cache
func (*RouteCache) PutRoute ¶
func (rc *RouteCache) PutRoute(path string, route *Route)
PutRoute - add a route into the route cache
type RouteComponentType ¶
type RouteComponentType int
RouteComponentType - route component types NOTE: variables will be stripped out / replaced so we dont track them
const ( // ComponentTypeFixed - a fixed path component ComponentTypeFixed RouteComponentType = 1 << iota // ComponentTypeWildcard - a wildcard path component ComponentTypeWildcard // ComponentTypeCatchAll - catch all route component ComponentTypeCatchAll )
type Router ¶
type Router struct { // ShouldCacheMatchedRoutes - if true then any matched routes should be cached // according to the path they were matched to ShouldCacheMatchedRoutes bool // contains filtered or unexported fields }
Router is the main routing class
func NewRouter ¶
func NewRouter() *Router
NewRouter - creates a new default instance of the Router type
func (*Router) AddStaticWithPrefix ¶
AddStaticWithPrefix registers a directory to serve static files. prefix value will be added at matching
func (*Router) GET ¶ added in v0.2.0
Add creates a new Route using the GET method and registers the instance within the Router
func (*Router) Group ¶ added in v0.2.0
Group creates a logical grouping point for a collection of routes. All routes under the group will have the group prefix appended to them.
func (*Router) HC ¶ added in v0.2.2
func (r *Router) HC(handlers ...ChainHandler) Chain
HC is syntactic sugar for NewChain
func (*Router) NewChain ¶ added in v0.2.0
func (r *Router) NewChain(handlers ...ChainHandler) Chain
NewChain - returns a new chain with the current router attached
func (*Router) NewMatcher ¶
NewMatcher returns a new matcher for the given Router
func (*Router) POST ¶ added in v0.2.0
Add creates a new Route using the POST method and registers the instance within the Router
func (*Router) PUT ¶ added in v0.2.0
Add creates a new Route using the PUT method and registers the instance within the Router
func (*Router) PrintRoutes ¶
func (r *Router) PrintRoutes()
PrintRoutes prints route registration information
func (*Router) PrintTreeInfo ¶
func (r *Router) PrintTreeInfo()
PrintTreeInfo prints debugging information about all registered Routes
func (*Router) SetAlwaysUseFirstMatch ¶ added in v0.2.0
SetAlwaysUseFirstMatch - Will the router always return the first match regardless of whether it fully meets all the criteria?
func (*Router) SetDebugLevel ¶
func (r *Router) SetDebugLevel(debugLevel DebugLevel)
SetDebugLevel - enables or disables Debug mode
func (*Router) SetErrorHandler ¶
func (r *Router) SetErrorHandler(statusCode int, handler ContextHandler)
SetErrorHandler configures a ContextHandler to handle all errors for the supplied status code
func (*Router) SetGlobalHandler ¶
func (r *Router) SetGlobalHandler(method string, handler ContextHandler)
SetGlobalHandler configures a ContextHandler to handle all requests for a given method
func (*Router) SetMethodNotAllowedIsError ¶ added in v0.2.0
SetMethodNotAllowedIsError - Will the router fail when it encounters a defined route that matches, but does not have a definition for the requested http method?
func (*Router) SetRouterErrorHandler ¶ added in v0.2.0
func (r *Router) SetRouterErrorHandler(handler ContextHandler)
SetRouterErrorHandler configures a ContextHandler to handle all general router errors (i.e.: non-network related)
func (*Router) SetStringVariable ¶
SetStringVariable adds a string variable value for substitution
type RouterErrorCode ¶ added in v0.2.0
type RouterErrorCode int
error codes RouterErrorCode - type used to represent an error in the routing request
const ( // RouterError - a non-specific router error RouterGenericErrorCode RouterErrorCode = 1 << iota // RouterContentError - a router static content error RouterContentErrorCode // ChainHadError - a router chain dispatched an error ChainGenericErrorCode )
const ErrorCodePanic RouterErrorCode = 777
ErrorCodePanic - error code used when recovering from a panic
type RoutingError ¶ added in v0.2.0
type RoutingError struct { StatusCode int ErrorCode RouterErrorCode Error error Message string Info ErrorInfoMap }
func EmptyRoutingError ¶ added in v0.2.0
func EmptyRoutingError() RoutingError
type StaticLocation ¶
type StaticLocation struct {
// contains filtered or unexported fields
}
StaticLocation is a holder for static location information
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree - storage for routes
func (*Tree) AddRouteToTree ¶
AddRouteToTree - splits the route into Nodes and adds them to the tree