medeina: github.com/imdario/medeina Index | Files

package medeina

import "github.com/imdario/medeina"

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

Package Files

doc.go medeina.go switch.go

Constants

const (
    GET    = "GET"
    POST   = "POST"
    PUT    = "PUT"
    PATCH  = "PATCH"
    DELETE = "DELETE"
)

Variables

var Methods = []Method{GET, POST, PUT, PATCH, DELETE}

func HandlerPathPrefix Uses

func HandlerPathPrefix(prefix string, handle http.Handler) http.Handler

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.

type Handle Uses

type Handle func()

Medeina closures definition.

type HostSwitch Uses

type HostSwitch map[string]http.Handler

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 Uses

func (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request)

Implement the ServerHTTP method on our new type

type Medeina Uses

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 Uses

func NewMedeina() *Medeina

Returns a new initialized Medeina tree routing with default httprouter's one.

func (*Medeina) DELETE Uses

func (m *Medeina) DELETE(handles Handle)

Switches context to use DELETE method as default in the closure.

func (*Medeina) GET Uses

func (m *Medeina) GET(handles Handle)

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 Uses

func (m *Medeina) Handler(path string, handle http.Handler, methods ...Method)

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 Uses

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 Uses

func (m *Medeina) On(path string, handle Handle)

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 Uses

func (m *Medeina) OnFunc(path string, handle func(*Medeina))

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 Uses

func (m *Medeina) OnHandler(path string, handle http.Handler)

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 Uses

func (m *Medeina) OnMux(path string, handle http.Handler)

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)

func (*Medeina) PATCH Uses

func (m *Medeina) PATCH(handles Handle)

Switches context to use PATCH method as default in the closure.

func (*Medeina) POST Uses

func (m *Medeina) POST(handles Handle)

Switches context to use POST method as default in the closure.

func (*Medeina) PUT Uses

func (m *Medeina) PUT(handles Handle)

Switches context to use PUT method as default in the closure.

func (*Medeina) ServeHTTP Uses

func (m *Medeina) ServeHTTP(w http.ResponseWriter, r *http.Request)

Makes the routing tree implement the http.Handler interface.

type Method Uses

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 Uses

type Params httprouter.Params

Package medeina imports 7 packages (graph) and is imported by 1 packages. Updated 2018-01-17. Refresh now. Tools for package owners.