Documentation
¶
Index ¶
- Variables
- func EnableDebugLog()
- func EnableProductionLog(logfilePath string)
- type MatchResult
- type Matcher
- type MatcherFunc
- type Middleware
- type MiddlewareHandler
- type Request
- type Response
- type Route
- func (r *Route) GetHandler() RouteHandler
- func (r *Route) GetMethods() []string
- func (r *Route) GetName() string
- func (r *Route) GetPattern() string
- func (r *Route) SetHandler(handler RouteHandler) *Route
- func (r *Route) SetMethods(methods ...string) *Route
- func (r *Route) SetName(name string) *Route
- func (r *Route) SetPattern(pattern string) *Route
- type RouteDecorator
- type RouteHandler
- type RouteHandlerFunc
- type Router
- func (r *Router) AddMiddleware(name string, mw MiddlewareHandler) *Middleware
- func (r *Router) AddRoute(route *Route, methods ...string)
- func (r *Router) AddRouteDecorator(decorator RouteDecorator)
- func (r *Router) CreateRoute(name, pattern string, handlerFunc RouteHandlerFunc, methods ...string) *Route
- func (r *Router) GetRouteByPath(path, method string) (*Route, *MatchResult)
- func (r *Router) NewRoute() *Route
- func (r *Router) Serve(addr string) error
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
Constants ¶
This section is empty.
Variables ¶
var Log = log15.New("module", "grout")
Log is a log15 instance which is the base for all loggers in grout
Functions ¶
func EnableDebugLog ¶
func EnableDebugLog()
EnableDebugLog enables logging starting from Debug level.
func EnableProductionLog ¶
func EnableProductionLog(logfilePath string)
EnableProductionLog enables logging starting from INFO level. Thus information about requests will be logged. If the logpath is empty, no fileHandler will be created.
Types ¶
type MatchResult ¶
type MatchResult struct { URLParams map[string][]string RouteParams map[string][]string URL *url.URL }
MatchResult is the result from matching an URL against a Matcher
func DefaultMatcher ¶
func DefaultMatcher(uri string, r *Route) *MatchResult
DefaultMatcher is up to change. It only detects url paramters and takes trailing slashes into account. A parameter is indicated by placing ":" in the route. A route which can be matched is for example /user/4 if there is a route /user/:id. Afterwards there will be a mapentry for id which maps to {4}. Multiple parameters are separated by ",". For example for /user/4,5,6 id maps to {4,5,6}
type Matcher ¶
type Matcher interface { // If the returned match is nil, there is no match to this route Match(uri string, r *Route) *MatchResult }
Matcher describes a strategy for matching
type MatcherFunc ¶
type MatcherFunc func(uri string, r *Route) *MatchResult
func (MatcherFunc) Match ¶
func (matcher MatcherFunc) Match(uri string, r *Route) *MatchResult
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware holds meta data useful for debugging as well as the handler which is ought to be called. All middlewares run before executing the
type MiddlewareHandler ¶
MiddlewareHandler is a function manipulating a request. If an error occures (err != nil) it is sent to the client. Useful for e.g. authentication It is guaranteed that r is never nil. This is the case that the route does not exist and thus an 404 is issued before.
type Request ¶
type Request struct { Body io.ReadCloser MatchResult *MatchResult HTTPRequest *http.Request }
Request is holding all information like parameter matching and body content
type Response ¶
type Response struct { // Http status code issued to the cliend Status int // Header fields Header map[string]string // contains filtered or unexported fields }
Response definition adding a convenience layer to the http.ResponseWriter. However, we do not implement the http.ResponseWriter interface on purpose as there are no convenient operations for setting the http Status for example. Not implementing the interface forces the use of the Response datastructure. Of course due do this, there are missing convenience operations. Please submit a pull request or an issue if there is something missing.
func NewResponse ¶
func NewResponse(rw http.ResponseWriter) Response
NewResponse creates a new Response object and sets the status to 200
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route definition
func (*Route) GetHandler ¶
func (r *Route) GetHandler() RouteHandler
GetHandler returns the Handler for the request
func (*Route) GetMethods ¶
GetMethods returns the allowed http methods
func (*Route) GetPattern ¶
GetPattern returns the raw string pattern as defined by the user and as it is used to match against an url
func (*Route) SetHandler ¶
func (r *Route) SetHandler(handler RouteHandler) *Route
SetHandler sets the Handler for the request
func (*Route) SetMethods ¶
SetMethods sets the list of http methods which can be used to call exactly this route
func (*Route) SetPattern ¶
SetPattern sets the url pattern that matches the route
type RouteDecorator ¶
type RouteDecorator func(handler RouteHandler, r *Route) RouteHandler
RouteDecorator wrapping the an initial handler. This way the handlers get decorated and we can provide more information
type RouteHandler ¶
RouteHandler for a Route
type RouteHandlerFunc ¶
RouteHandlerFunc as specified in the RouteHandler interface
func (RouteHandlerFunc) Run ¶
func (h RouteHandlerFunc) Run(req *Request, res *Response)
Run implements the RouteHandler interface
type Router ¶
type Router struct { RouteMatcher Matcher // contains filtered or unexported fields }
Router takes care of matching the routes as well as attaching and executing middlewares/decorators.
func (*Router) AddMiddleware ¶
func (r *Router) AddMiddleware(name string, mw MiddlewareHandler) *Middleware
AddMiddleware adds an middleware to the set of active middlewares intercepting an incoming request. The middleware is returned afterwards It is not possible to access the internally used middlewares. If there is a usecase for this, support for this can be implemented fastly.
func (*Router) AddRouteDecorator ¶
func (r *Router) AddRouteDecorator(decorator RouteDecorator)
AddRouteDecorator and apply it to existing routes
func (*Router) CreateRoute ¶
func (r *Router) CreateRoute(name, pattern string, handlerFunc RouteHandlerFunc, methods ...string) *Route
CreateRoute creates a new Route and adds it to the router
func (*Router) GetRouteByPath ¶
func (r *Router) GetRouteByPath(path, method string) (*Route, *MatchResult)
GetRouteByPath and match it against included patterns
func (*Router) NewRoute ¶
NewRoute returns a blank route. The route is not added to the set of active routes, yet.