grout

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

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

Go to latest
Published: Oct 24, 2016 License: MIT Imports: 7 Imported by: 0

README

GoDoc

grout

Grout is a simple router written in Go which supports middlewares and route decoration.

Example

It is pretty easy to setup and use - replace the handlers by your own ones.

func main() {
    router := grout.NewRouter()

    router.AddMiddleware("CheckAccess", accessCheckMiddleware)
    router.CreateRoute("UserCreate", "/user", view.CreateUser, "PUT")
    router.CreateRoute("GetUser", "/user/:id", view.GetUser, "GET")

    log.Printf("Listening on %s...", port)
    log.Fatal(router.Serve(port))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

type MiddlewareHandler func(req *Request, res *Response, route *Route) error

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

func (*Response) SetCookie

func (res *Response) SetCookie(c *http.Cookie)

SetCookie sets a cookie on the underlying ResponseWriter.

func (*Response) Write

func (res *Response) Write(data []byte) (int, error)

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

func (r *Route) GetMethods() []string

GetMethods returns the allowed http methods

func (*Route) GetName

func (r *Route) GetName() string

GetName returns the route name

func (*Route) GetPattern

func (r *Route) GetPattern() string

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

func (r *Route) SetMethods(methods ...string) *Route

SetMethods sets the list of http methods which can be used to call exactly this route

func (*Route) SetName

func (r *Route) SetName(name string) *Route

SetName sets the route name

func (*Route) SetPattern

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

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

type RouteHandler interface {
	Run(*Request, *Response)
}

RouteHandler for a Route

type RouteHandlerFunc

type RouteHandlerFunc func(*Request, *Response)

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 NewRouter

func NewRouter() *Router

NewRouter creates a new router handling routes.

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) AddRoute

func (r *Router) AddRoute(route *Route, methods ...string)

AddRoute r to the list of available routes. r is online afterwards.

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

func (r *Router) NewRoute() *Route

NewRoute returns a blank route. The route is not added to the set of active routes, yet.

func (*Router) Serve

func (r *Router) Serve(addr string) error

Serve launches an http server listening on addr. If addr is empty it will listen on :http Attention: This is up to change for ssl support and probably some more...

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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