Documentation ¶
Overview ¶
Medeina is a routing tree based on httprouter inspired by Ruby's Roda and Cuba. It allows to define your routes as a tree, operating on the current one at any point of the routing tree.
Usage
// From Roda's site r := medeina.NewMedeina() r.GET(func() { r.Is("", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { http.Redirect(w, r, "/hello", http.StatusFound) }) r.On("hello", func() { r.Is("world", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "Hello world!") }) r.Is("", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprintf(w, "Hello!") }) }) })
If you want to check a more convoluted example, feel free to check github_test.go. It uses several possible styles of creating routes using Medeina in order to avoid pitfalls.
Copyright (c) 2014 Dario Castañé. Licensed under the MIT License.
Copyright (c) 2015 Dario Castañé. Licensed under the MIT License. From httprouter's example.
Index ¶
- Constants
- Variables
- func HandlerPathPrefix(prefix string, handle http.Handler) http.Handler
- type Handle
- type HostSwitch
- type Medeina
- func (m *Medeina) DELETE(handles Handle)
- func (m *Medeina) GET(handles Handle)
- func (m *Medeina) Handler(path string, handle http.Handler, methods ...Method)
- func (m *Medeina) Is(path string, handle httprouter.Handle, methods ...Method)
- func (m *Medeina) On(path string, handle Handle)
- func (m *Medeina) OnFunc(path string, handle func(*Medeina))
- func (m *Medeina) OnHandler(path string, handle http.Handler)
- func (m *Medeina) OnMux(path string, handle http.Handler)
- func (m *Medeina) PATCH(handles Handle)
- func (m *Medeina) POST(handles Handle)
- func (m *Medeina) PUT(handles Handle)
- func (m *Medeina) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Method
- type Params
Constants ¶
const ( GET = "GET" POST = "POST" PUT = "PUT" PATCH = "PATCH" DELETE = "DELETE" )
Variables ¶
Functions ¶
func HandlerPathPrefix ¶
Utility function to use with http.Handler compatible routers. Modifies the request's URL in order to make subrouters relative to the prefix. If you use a router as subrouter without this they need to match the full path.
Types ¶
type HostSwitch ¶
We need an object that implements the http.Handler interface. Therefore we need a type for which we implement the ServeHTTP method. We just use a map here, in which we map host names (with port) to http.Handlers
func (HostSwitch) ServeHTTP ¶
func (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request)
Implement the ServerHTTP method on our new type
type Medeina ¶
type Medeina struct {
// contains filtered or unexported fields
}
Medeina is a goddess willing to help you with your trees... of routes. Allow it to be part of your chain of HTTP Handlers and she will handle all those messy branches that your once-used-to-be-simple router got.
func NewMedeina ¶
func NewMedeina() *Medeina
Returns a new initialized Medeina tree routing with default httprouter's one.
func (*Medeina) GET ¶
Switches context to use GET method as default in the closure. You can override for a route while using Is, setting which methods you want.
func (*Medeina) Handler ¶
As Is but delegateing on a standard http.Handler. There is no equivalent functions for specific HTTP methods, so you must use this in order to add standard http.Handlers.
func (*Medeina) Is ¶
func (m *Medeina) Is(path string, handle httprouter.Handle, methods ...Method)
Sets a canonical path. A canonical path means no further entries are in the path.
func (*Medeina) On ¶
Adds a new subpath to the current context. Everything under the closure will use all the previously set path as root for their URLs.
func (*Medeina) OnFunc ¶
As On but using a function which accepts a routing tree as parameter. This will be useful to split routes definition in several functions.
func (*Medeina) OnHandler ¶
As On but using a function which accepts a standard http.Handler, delegating further route handling to the handler. It adds a HttpRouter catch-all matcher called 'medeina_subpath'. This will be useful to split routes definition in several functions.
func (*Medeina) OnMux ¶
As OnHandler for subrouters. It's a convenience function. These two calls are equivalent:
m.OnHandler("api/v1/events", HandlerPathPrefix("/api/v1/events", mux)) m.OnMux("api/v1/events", mux)
type Method ¶
type Method string
HTTP Methods available as constants. We could use strings but it was cleaner to force specefic values in an enum-like fashion.
type Params ¶
type Params httprouter.Params