router

package
v0.0.0-...-0bc05a2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2016 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

Types

type Handler

type Handler func(http.ResponseWriter, *Request) error

type Method

type Method string

The router is built on: https://github.com/julienschmidt/httprouter Copyright 2013 Julien Schmidt. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

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

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) AsID

func (ps Params) AsID(name string) int64

AsID returns the value of the requested param as an int64

func (Params) ByName

func (ps Params) ByName(name string) string

ByName returns the value of the first Param which key matches the given name If no matching Param is found, an empty string is returned.

func (Params) EqualsAny

func (ps Params) EqualsAny(name string, values ...string) bool

EqualsAny is true if the give parameter name equals any of the given values. An empty string is both a valid name and value.

type Request

type Request struct {
	*http.Request
	Params Params
	User   auth.User
	Values url.Values
}

func NewRequest

func NewRequest(r *http.Request, auth *auth.Auth) (request *Request)

NewRequest wraps the http.Request and adds an auth.User if valid

func (*Request) Get

func (r *Request) Get(key string) string

Get gets a GET parameter and ONLY a get parameter - never POST form data

func (*Request) QueryValues

func (r *Request) QueryValues() url.Values

TODO This function is messy and doesn't make much sense - prefer mocks over tricks with context for testing

func (*Request) SetPath

func (r *Request) SetPath(path string)

SetPath allows the URL Path to be easily set - useful for testing.

type Router

type Router struct {
	RedirectTrailingSlash bool
	RedirectFixedPath     bool
	// contains filtered or unexported fields
}

Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes

func New

func New(auth *auth.Auth) *Router

New creates a new router instance using the given context

func (*Router) DELETE

func (r *Router) DELETE(path string, h Handler)

DELETE attaches the given handler on the path for the DELETE method only.

func (*Router) GET

func (r *Router) GET(path string, h Handler)

GET attaches the given handler on the path for the GET method only.

func (*Router) HandleFunc

func (r *Router) HandleFunc(path string, h http.HandlerFunc, ms ...Method)

HandleFunc adapts the given http.HandleFunc so it can be served for every given method by the router.

func (*Router) Handler

func (r *Router) Handler(path string, h http.Handler, ms ...Method)

Handler adapts the given http.Handler so it can be served for every given method by the router.

func (*Router) Lookup

func (r *Router) Lookup(method Method, path string) (Handler, Params, bool)

Lookup allows the manual lookup of a method + path combo.

func (*Router) PATCH

func (r *Router) PATCH(path string, h Handler)

PATCH attaches the given handler on the path for the PATCH method only.

func (*Router) POST

func (r *Router) POST(path string, h Handler)

POST attaches the given handler on the path for the POST method only.

func (*Router) PUT

func (r *Router) PUT(path string, h Handler)

PUT attaches the given handler on the path for the PUT method only.

func (*Router) Route

func (r *Router) Route(path string, h Handler, methods ...Method)

Route attaches the given handler on the path for every given method.

func (*Router) ServeFiles

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system path.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the request to the handler whose pattern and method matches the request. It will also get an auth.User if the session is valid, pass context to the matched handler, and generate any parameters requested by the route.

Jump to

Keyboard shortcuts

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