methodr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2014 License: MIT Imports: 1 Imported by: 2

README

MethodR for golang Build Status GoDoc Coverage Status

MethodR provides routing based on the request method written in golang. Fully http.Handler compliant.

Usage

$ go get github.com/blang/methodr
import github.com/blang/methodr

http.Handle("/foo", methodr.GET(getHandler).POST(postHandler).DEFAULT(notFoundHandler))

Also check the GoDocs.

Why should I use this lib?

  • Simple Interface
  • Fully http.Handler compliant (supports every good routing environment)
  • No reflection/reflection/nasty stuff
  • No allocs as handler
  • Fully tested (Coverage >99%)
  • Fast (See Benchmarks)
  • Only Stdlib

Features

  • Helper functions for all methods
  • Full control if your setup is more complex
  • Global or routebased default/catchall handler

Example

Have a look at full and runnable examples in examples/main.go. Also check the GoDocs.

import github.com/blang/methodr

// Setup 1: Restrict route to single method
// POST/PUT/... will result in StatusMethodNotAllowed
// Note: HEAD will use GET unless HEAD handler was set.
http.Handle("/foo1", methodr.GET(getHandler))

// Setup 2: Restrict route with custom default handler
// POST/PUT/... will result in StatusNotFound
http.Handle("/foo2", methodr.GET(getHandler).DEFAULT(notFoundHandler))

//NOTE: If you're not happy with the global default handler, you can set it:
// methodr.DefaultHandler = myBadRequestHandler

// Setup 3: Route depending on method
// Use chains to specify different routes, order is nonrelevant.
// PATCH/PUT/... will result in StatusNotFound
http.Handle("/foo3", methodr.GET(getHandler).POST(postHandler))
// Equivalent to: http.Handle("/foo3", methodr.POST(postHandler).GET(getHandler))

// Setup 4: Setup more complicated routes
mux := &methodr.Mux{
    Get:     getHandler,
    Post:    postHandler,
    Patch:   postHandler,
    Default: notFoundHandler,
}
http.Handle("/foo4", mux)
// Equivalent to:
// http.Handle("/foo4", methodr.GET(getHandler).POST(postHandler).PATCH(postHandler).DEFAULT(notFoundHandler))

To get a list of all available methods see the GoDocs.

Benchmarks

BenchmarkNoRoutingReference    500000000             7.46 ns/op         0 B/op          0 allocs/op
BenchmarkRoutingHit             50000000            31.2  ns/op         0 B/op          0 allocs/op
BenchmarkRoutingMissToDefault   50000000            35.0  ns/op         0 B/op          0 allocs/op
BenchmarkRoutingMissToCustom    50000000            36.2  ns/op         0 B/op          0 allocs/op

See benchmark cases at methodr_test.go

Contribution

Feel free to make a pull request. For bigger changes create a issue first to discuss about it.

License

See LICENSE file.

Documentation

Overview

Package methodr provides http.Handler compliant routing based on the request method.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Changeable global default handler in case of miss on routing table. Default: DefaultHandlerMethodNotAllowed
	DefaultHandler = DefaultHandlerMethodNotAllowed

	// Default handler returning http.StatusMethodNotAllowed
	DefaultHandlerMethodNotAllowed = http.HandlerFunc(defaultHandleMethodNotAllowed)
)

Functions

This section is empty.

Types

type Mux

type Mux struct {
	Get     http.Handler
	Head    http.Handler
	Post    http.Handler
	Put     http.Handler
	Delete  http.Handler
	Trace   http.Handler
	Options http.Handler
	Connect http.Handler
	Patch   http.Handler
	Default http.Handler // Default handler in case of miss
}

Routing table with handler for all methods

func CONNECT

func CONNECT(h http.Handler) *Mux

CONNECT sets the handler used for CONNECT method requests.

func DEFAULT

func DEFAULT(h http.Handler) *Mux

DEFAULT sets default handler used in case of miss on routing table.

func DELETE

func DELETE(h http.Handler) *Mux

DELETE sets the handler used for DELETE method requests.

func GET

func GET(h http.Handler) *Mux

GET sets the handler used for GET method requests. HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.

func HEAD(h http.Handler) *Mux

HEAD sets the handler used for HEAD method requests. HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.

func OPTIONS

func OPTIONS(h http.Handler) *Mux

OPTIONS sets the handler used for OPTIONS method requests.

func PATCH

func PATCH(h http.Handler) *Mux

PATCH sets the handler used for PATCH method requests.

func POST

func POST(h http.Handler) *Mux

POST sets the handler used for POST method requests.

func PUT

func PUT(h http.Handler) *Mux

PUT sets the handler used for PUT method requests.

func TRACE

func TRACE(h http.Handler) *Mux

TRACE sets the handler used for TRACE method requests.

func (*Mux) CONNECT

func (m *Mux) CONNECT(h http.Handler) *Mux

CONNECT sets the handler used for CONNECT method requests.

func (*Mux) DEFAULT

func (m *Mux) DEFAULT(h http.Handler) *Mux

DEFAULT sets default handler in case of miss on routing table

func (*Mux) DELETE

func (m *Mux) DELETE(h http.Handler) *Mux

DELETE sets the handler used for DELETE method requests.

func (*Mux) GET

func (m *Mux) GET(h http.Handler) *Mux

GET sets the handler used for GET method requests. HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.

func (*Mux) HEAD

func (m *Mux) HEAD(h http.Handler) *Mux

HEAD sets the handler used for HEAD method requests. HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.

func (*Mux) OPTIONS

func (m *Mux) OPTIONS(h http.Handler) *Mux

OPTIONS sets the handler used for OPTIONS method requests.

func (*Mux) PATCH

func (m *Mux) PATCH(h http.Handler) *Mux

PATCH sets the handler used for PATCH method requests.

func (*Mux) POST

func (m *Mux) POST(h http.Handler) *Mux

POST sets the handler used for POST method requests.

func (*Mux) PUT

func (m *Mux) PUT(h http.Handler) *Mux

PUT sets the handler used for PUT method requests.

func (*Mux) ServeHTTP

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

ServeHTTP routes requests depending on method routing table. In case of a miss the custom DEFAULT handler is used, otherwise the global DefaultHandler. HEAD requests are delegated to GET if there's a GET handler available and no HEAD handler set.

func (*Mux) TRACE

func (m *Mux) TRACE(h http.Handler) *Mux

TRACE sets the handler used for TRACE method requests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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