wok

package
v2.0.12+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2017 License: MIT Imports: 6 Imported by: 0

README

Wok

GoDoc

A simple and minimalistic (51 LOC in wok.go) web application router based on httprouter. Supports route groups, global, per-group and per-route noodle middleware. Compatible with http.HandlerFunc. For a quick start see the sample application.

Root router object

Wok router is created by wok.Default() and wok.New() constructors that accept arbitrary list of noodle middlewares. Note that the Default constructor preloads standard logger, recovery and local storage middlewares that come with noodle middleware package. Resulting middleware chain will be shared among all routes.

w := wok.Default()

Handling routes

Convenience methods GET, POST, PUT, PATCH, DELETE and OPTIONS create routing entries with specific paths and middleware chains. Following is an example of attaching hander to site root. Note that all of the methods return a closure that accept a single noodle.Handler parameter.


import "gopkg.in/andviro/noodle.v2/render"

func index(w http.ResponseWriter, r *http.Request) {
	// nothing to do here, everything is in the template
	return nil
}

...

idxTpl := template.Must(template.New("index").Parse("<h1>Hello</h1>"))
// Middlewares are passed as variadic parameter list to GET method.
// Resulting closure accepts a route handler parameter.
w.GET("/", render.Template(idxTpl))(index)

Named parameters such as /:name and catch-all parameters i.e. /*pathList are supported in route path assignment. See the httprouter documentation for the parameter syntax reference. To get the value of a route parameter use wok.Var function:

func userDetail(w http.ResponseWriter, r *http.Request) {
    id := wok.Var(r, "id")
    // ... do something with the id
}

Route grouping

Group method creates a route group with the specific prefix. A middleware variadic list can be supplied to Group function, then the resulting middleware chain for a group will contain a router global middlewares and group-specific middlewares.


// apiAuth is some group specific middleware
// apiIndex and apiDetail are route handers for /api and /api/:id paths
api := w.Group("/api", apiAuth, render.JSON)
api.GET("/")(apiIndex)
api.GET("/:id")(apiDetail)

Note that you also can pass route-specific middleware lists to GET methods!

Serving HTTP

The Wok router object implements http.Handler interface and can be directly passed to http.ListenAndServe function.

w := wok.Default()
// setup routes and middlewares
// ...

// start server
http.ListenAndServe(":8080", w)

License

This code is released under MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func URLJoin

func URLJoin(paths ...string) (res string)

URLJoin joins components of a network path

func Var

func Var(r *http.Request, name string) string

Var returns route variable for context or empty string

Types

type RouteClosure

type RouteClosure func(http.HandlerFunc)

RouteClosure is a convenience type that allows setting route handlers by calling the function

func (RouteClosure) Handle

func (rc RouteClosure) Handle(h http.Handler)

Handle allows route closure to attach a http.Handler

type Wok

type Wok struct {
	*httprouter.Router
	// contains filtered or unexported fields
}

Wok is a simple wrapper for httprouter with route groups and native support for http.HandlerFunc

func Default

func Default(mws ...noodle.Middleware) *Wok

Default creates new Wok with the default Noodle middleware chain

func New

func New(mws ...noodle.Middleware) *Wok

New creates new Wok initialized with middlewares. The resulting middleware chain will be called for all routes in Wok

func (*Wok) DELETE

func (wok *Wok) DELETE(path string, mws ...noodle.Middleware) RouteClosure

DELETE is a convenience wrapper over Wok.Handle

func (*Wok) GET

func (wok *Wok) GET(path string, mws ...noodle.Middleware) RouteClosure

GET is a convenience wrapper over Wok.Handle

func (*Wok) Group

func (wok *Wok) Group(prefix string, mws ...noodle.Middleware) *Wok

Group starts new route group with common prefix. Middleware passed to Group will be used for all routes in it.

func (*Wok) Handle

func (wok *Wok) Handle(method, path string, mws ...noodle.Middleware) RouteClosure

Handle allows to attach some noodle Middlewares and a Handle to a route

func (*Wok) OPTIONS

func (wok *Wok) OPTIONS(path string, mws ...noodle.Middleware) RouteClosure

OPTIONS is a convenience wrapper over Wok.Handle

func (*Wok) PATCH

func (wok *Wok) PATCH(path string, mws ...noodle.Middleware) RouteClosure

PATCH is a convenience wrapper over Wok.Handle

func (*Wok) POST

func (wok *Wok) POST(path string, mws ...noodle.Middleware) RouteClosure

POST is a convenience wrapper over Wok.Handle

func (*Wok) PUT

func (wok *Wok) PUT(path string, mws ...noodle.Middleware) RouteClosure

PUT is a convenience wrapper over Wok.Handle

Jump to

Keyboard shortcuts

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