route

package module
v0.0.0-...-f9f09b5 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2016 License: MIT Imports: 3 Imported by: 2

README

route

Package route is a helper for the core.
It provides syntactic sugar that wraps a handler with request method and path filtering.

GoDoc

Documentation

Overview

Package route is a helper for the core (https://godoc.org/github.com/volatile/core). It provides syntactic sugar that wraps a handler with request method and path filtering.

Method filtering

Functions exists for the most common and standard HTTP methods.

Example for a GET route:

route.Get("^/$", func(c *core.Context) {
	fmt.Fprint(c.ResponseWriter, "Hello, World!")
})

If you need to handle custom methods, Use receives the methods in a strings slice:

route.Use([]string{"GET", "POST"}, "^/$", func(c *core.Context) {
	fmt.Fprint(c.ResponseWriter, "Hello, World from GET or POST!")
})

Remember that HTTP methods are case-sensitive. See RFC 7231 4.1 (https://tools.ietf.org/html/rfc7231#section-4.1).

Path filtering

A regular expression is used to match the request path. We think it offers the best balance between performance and power for this kind of job.

If you need to use named parameters from the URL, just use a regexp named group and a func(*core.Context, map[string]string) handler type:

route.Get("^/(?P<name>[A-Za-z]+)$", func(c *core.Context, params map[string]string) {
	fmt.Fprintf(c.ResponseWriter, "Hello, %s!", params["name"])
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(pattern string, handler interface{})

Delete adds a handler to the default handlers stack. It executes the handler when request matches DELETE method and pattern.

func Get

func Get(pattern string, handler interface{})

Get adds a handler to the default handlers stack. It executes the handler when request matches GET method and pattern.

func Head(pattern string, handler interface{})

Head adds a handler to the default handlers stack. It executes the handler when request matches HEAD method and pattern.

func Options

func Options(pattern string, handler interface{})

Options adds a handler to the default handlers stack. It executes the handler when request matches OPTIONS method and pattern.

func Patch

func Patch(pattern string, handler interface{})

Patch adds a handler to the default handlers stack. It executes the handler when request matches PATCH method and pattern.

func Post

func Post(pattern string, handler interface{})

Post adds a handler to the default handlers stack. It executes the handler when request matches POST method and pattern.

func Put

func Put(pattern string, handler interface{})

Put adds a handler to the default handlers stack. It executes the handler when request matches PUT method and pattern.

func Use

func Use(methods []string, pattern string, handler interface{})

Use adds a handler to the default handlers stack. It executes the handler when request matches methods and pattern.

Types

This section is empty.

Jump to

Keyboard shortcuts

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