router

package
v0.0.0-...-3f1871c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: MIT, MIT Imports: 6 Imported by: 6

README

router Build Status GoDoc Coverage Status Go Report Card API

Featured and fast router used by vinxi. Provides a hierarchical middleware layer.

Originally based in bmizerany/pat.

Installation

go get -u gopkg.in/vinxi/router.v0

API

See godoc reference for detailed API documentation.

Examples

Router
package main

import (
  "fmt"
  "gopkg.in/vinxi/router.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  r := router.New()
  r.Get("/get").Forward("http://httpbin.org")
  r.Get("/headers").Forward("http://httpbin.org")
  r.Get("/image/:format").Forward("http://httpbin.org")
  r.Get("/say").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello, foo"))
  }))

  vs.Use(r)
  vs.Forward("http://example.com")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}
Virtual host like muxer router
package main

import (
  "fmt"
  "gopkg.in/vinxi/mux.v0"
  "gopkg.in/vinxi/router.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  fmt.Printf("Server listening on port: %d\n", 3100)
  vs := vinxi.NewServer(vinxi.ServerOptions{Host: "localhost", Port: 3100})

  r := router.New()
  r.Get("/get").Forward("http://httpbin.org")
  r.Get("/headers").Forward("http://httpbin.org")
  r.Get("/image/:format").Forward("http://httpbin.org")
  r.Get("/say").Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello, foo"))
  }))

  // Create a host header multiplexer
  muxer := mux.Host("localhost:3100")
  muxer.Use(r)

  vs.Use(muxer)
  vs.Forward("http://example.com")

  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

License

MIT.

Documentation

Overview

Package router implements a simple URL pattern muxer router with hierarchical middleware layer.

Index

Constants

This section is empty.

Variables

View Source
var DefaultForwarder = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	fwd, _ := forward.New(forward.PassHostHeader(true))
	fwd.ServeHTTP(w, r)
})

DefaultForwarder stores the default http.Handler to be used to forward the traffic. By default the proxy will reply with 502 Bad Gateway if no custom forwarder is defined.

View Source
var (
	// ErrNoRouteMatch is returned as typed error when no route can be matched.
	ErrNoRouteMatch = errors.New("router: cannot match any route")
)

Functions

func Match

func Match(pat, path string) (url.Values, bool)

Match matches the given path string againts the register pattern.

func Tail

func Tail(pat, path string) string

Tail returns the trailing string in path after the final slash for a pat ending with a slash.

Examples:

Tail("/hello/:title/", "/hello/mr/mizerany") == "mizerany"
Tail("/:a/", "/x/y/z")                       == "y/z"

Types

type Route

type Route struct {
	// Pattern stores the path pattern
	Pattern string

	// Layer provides the middleware layer at route level.
	Layer *layer.Layer

	// Handler stores the final route handler function.
	Handler http.Handler
}

Route represents an HTTP route based on the given

func NewRoute

func NewRoute(pattern string) *Route

NewRoute creates a new Route for the given URL path pattern.

func (*Route) Flush

func (r *Route) Flush()

Flush flushes the route level middleware stack.

func (*Route) Forward

func (r *Route) Forward(uri string)

Forward defines the default URL to forward incoming traffic.

func (*Route) Handle

func (r *Route) Handle(handler http.HandlerFunc)

Handle defines a custom route final handler function. Use this method only if you really want to handle the route in a very specific way.

func (*Route) Match

func (r *Route) Match(path string) (url.Values, bool)

Match matches an incoming request againts the registered matchers returning true if all matchers passes.

func (*Route) ServeHTTP

func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP handlers the incoming request and implemented the vinxi specific handler interface.

func (*Route) SetParent

func (r *Route) SetParent(parent layer.Middleware)

SetParent sets a middleware parent layer. This method is tipically called via inversion of control from parent router.

func (*Route) Use

func (r *Route) Use(handler interface{}) *Route

Use attaches a new middleware handler for incoming HTTP traffic.

func (*Route) UseFinalHandler

func (r *Route) UseFinalHandler(handler http.Handler) *Route

UseFinalHandler attaches a new http.Handler as final middleware layer handler.

func (*Route) UsePhase

func (r *Route) UsePhase(phase string, handler interface{}) *Route

UsePhase attaches a new middleware handler to a specific phase.

type Router

type Router struct {
	// ForceMethodNotAllowed, if true, is used to return a 405 Method Not Allowed response
	// in case that the router has no configured routes for the incoming request method.
	// Defaults to false.
	ForceMethodNotAllowed bool

	// NotFound, if set, is used whenever the request doesn't match any
	// pattern for its method. NotFound should be set before serving any
	// requests.
	NotFound http.Handler

	// Layer provides middleware layer capabilities to the router.
	Layer *layer.Layer

	// Routes associates HTTP methods with path patterns handlers.
	Routes map[string][]*Route
}

Router is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns with their associated methods and calls the handler for the pattern that most closely matches the URL.

Pattern matching attempts each pattern in the order in which they were registered.

Patterns may contain literals or captures. Capture names start with a colon and consist of letters A-Z, a-z, _, and 0-9. The rest of the pattern matches literally. The portion of the URL matching each name ends with an occurrence of the character in the pattern immediately following the name, or a /, whichever comes first. It is possible for a name to match the empty string.

Example pattern with one capture:

/hello/:name

Will match:

/hello/blake
/hello/keith

Will not match:

/hello/blake/
/hello/blake/foo
/foo
/foo/bar

Example 2:

/hello/:name/

Will match:

/hello/blake/
/hello/keith/foo
/hello/blake
/hello/keith

Will not match:

/foo
/foo/bar

A pattern ending with a slash will add an implicit redirect for its non-slash version. For example: Get("/foo/", handler) also registers Get("/foo", handler) as a redirect. You may override it by registering Get("/foo", anotherhandler) before the slash version.

Retrieve the capture from the r.URL.Query().Get(":name") in a handler (note the colon). If a capture name appears more than once, the additional values are appended to the previous values (see http://golang.org/pkg/net/url/#Values)

A trivial example server is:

package main

import (
	"io"
	"net/http"
	"github.com/bmizerany/pat"
	"log"
)

// hello world, the web server
func HelloServer(w http.ResponseWriter, req *http.Request) {
	io.WriteString(w, "hello, "+req.URL.Query().Get(":name")+"!\n")
}

func main() {
	m := pat.New()
	m.Get("/hello/:name", http.HandlerFunc(HelloServer))

	// Register this pat with the default serve mux so that other packages
	// may also be exported. (i.e. /debug/pprof/*)
	http.Handle("/", m)
	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

When "Method Not Allowed":

Router knows what methods are allowed given a pattern and a URI. For convenience, Router will add the Allow header for requests that match a pattern for a method other than the method requested and set the Status to "405 Method Not Allowed".

If the NotFound handler is set, then it is used whenever the pattern doesn't match the request path for the current method (and the Allow header is not altered).

func New

func New() *Router

New creates a new Router.

func (*Router) All

func (r *Router) All(path string) *Route

All will register a pattern for any HTTP method.

func (*Router) Delete

func (r *Router) Delete(path string) *Route

Delete will register a pattern for DELETE requests.

func (*Router) FindRoute

func (r *Router) FindRoute(method, path string) (*Route, error)

FindRoute tries to find a registered route who matches with the given method and path.

func (*Router) Flush

func (r *Router) Flush()

Flush flushes the router middleware stack.

func (*Router) Forward

func (r *Router) Forward(uri string) *Router

Forward defines the default URL to forward incoming traffic.

func (*Router) Get

func (r *Router) Get(path string) *Route

Get will register a pattern for GET requests. It also registers pat for HEAD requests. If this needs to be overridden, use Head before Get with pat.

func (*Router) HandleHTTP

func (r *Router) HandleHTTP(w http.ResponseWriter, req *http.Request, h http.Handler)

HandleHTTP matches r.URL.Path against its routing table using the rules described above.

func (*Router) Head

func (r *Router) Head(path string) *Route

Head will register a pattern for HEAD requests.

func (*Router) Options

func (r *Router) Options(path string) *Route

Options will register a pattern for OPTIONS requests.

func (*Router) Patch

func (r *Router) Patch(path string) *Route

Patch will register a pattern for PATCH requests.

func (*Router) Post

func (r *Router) Post(path string) *Route

Post will register a pattern for POST requests.

func (*Router) Put

func (r *Router) Put(path string) *Route

Put will register a pattern for PUT requests.

func (*Router) Remove

func (r *Router) Remove(method, path string) bool

Remove matches and removes the matched route from the router stack.

func (*Router) Route

func (r *Router) Route(method, path string) *Route

Route will register a new route for the given pattern and HTTP method.

func (*Router) SetParent

func (r *Router) SetParent(parent layer.Middleware)

SetParent sets a middleware parent layer. This method is tipically called via inversion of control from vinxi layer.

func (*Router) Use

func (r *Router) Use(handler ...interface{}) *Router

Use attaches a new middleware handler for incoming HTTP traffic.

func (*Router) UseFinalHandler

func (r *Router) UseFinalHandler(fn http.Handler) *Router

UseFinalHandler uses a new middleware handler function as final handler.

func (*Router) UsePhase

func (r *Router) UsePhase(phase string, handler ...interface{}) *Router

UsePhase attaches a new middleware handler to a specific phase.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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