Documentation ¶
Overview ¶
Package routes a simple http routing API for the Go programming language, compatible with the standard http.ListenAndServe function.
Create a new route multiplexer:
r := routes.NewRouter()
Define a simple route with a given method (ie Get, Put, Post ...), path and http.HandleFunc.
r.Get("/foo", fooHandler)
Define a route with restful parameters in the path:
r.Get("/:foo/:bar", func(rw http.ResponseWriter, req *http.Request) { c := routes.NewContext(req) foo := c.Params.Get("foo") bar := c.Params.Get("bar") fmt.Fprintf(rw, "%s %s", foo, bar) })
The parameters are parsed from the URL, and stored in the Request Context.
More control over the route's parameter matching is possible by providing a custom regular expression:
r.Get("/files/:file(.+)", handler)
To start the web server, use the standard http.ListenAndServe function, and provide the route multiplexer:
http.Handle("/", r) http.ListenAndServe(":8000", nil)
Index ¶
- Constants
- func Error(w http.ResponseWriter, code int)
- func ReadJson(r *http.Request, v interface{}) error
- func ReadXml(r *http.Request, v interface{}) error
- func ServeJson(w http.ResponseWriter, v interface{})
- func ServeTemplate(w http.ResponseWriter, name string, data map[string]interface{})
- func ServeXml(w http.ResponseWriter, v interface{})
- type Context
- type Params
- type Router
- func (r *Router) AddRoute(method string, pattern string, handler http.HandlerFunc)
- func (r *Router) Del(pattern string, handler http.HandlerFunc)
- func (r *Router) Filter(filter http.HandlerFunc)
- func (r *Router) FilterParam(param string, filter http.HandlerFunc)
- func (r *Router) Get(pattern string, handler http.HandlerFunc)
- func (r *Router) Patch(pattern string, handler http.HandlerFunc)
- func (r *Router) Post(pattern string, handler http.HandlerFunc)
- func (r *Router) Put(pattern string, handler http.HandlerFunc)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (r *Router) Set(name string, value interface{})
- func (r *Router) SetEnv(name, value string)
- func (r *Router) Static(pattern string, dir string)
- func (r *Router) Template(t *template.Template)
- func (r *Router) TemplateFiles(filenames ...string)
- func (r *Router) TemplateGlob(pattern string)
- type Values
Constants ¶
const ( DELETE = "DELETE" GET = "GET" HEAD = "HEAD" OPTIONS = "OPTIONS" PATCH = "PATCH" POST = "POST" PUT = "PUT" )
Variables ¶
This section is empty.
Functions ¶
func Error ¶
func Error(w http.ResponseWriter, code int)
Error will terminate the http Request with the specified error code.
func ReadJson ¶
ReadJson parses the JSON-encoded data in the http.Request object and stores the result in the value pointed to by v.
func ReadXml ¶
ReadXml parses the XML-encoded data in the http.Request object and stores the result in the value pointed to by v.
func ServeJson ¶
func ServeJson(w http.ResponseWriter, v interface{})
ServeJson writes the JSON representation of resource v to the http.ResponseWriter.
func ServeTemplate ¶
func ServeTemplate(w http.ResponseWriter, name string, data map[string]interface{})
ServeTemplate applies the named template to the specified data map and writes the output to the http.ResponseWriter.
func ServeXml ¶
func ServeXml(w http.ResponseWriter, v interface{})
ServeXml writes the XML representation of resource v to the http.ResponseWriter.
Types ¶
type Context ¶
type Context struct { // named parameters that are passed in via RESTful URL Parameters Params Params // named attributes that persist for the lifetime of the request Values Values // contains filtered or unexported fields }
Context stores data for the duration of the http.Request
func NewContext ¶
Retruns the Context associated with the http.Request.
type Params ¶
Params maps a string key to a list of values.
type Router ¶
func (*Router) AddRoute ¶
func (r *Router) AddRoute(method string, pattern string, handler http.HandlerFunc)
Adds a new Route to the Handler
func (*Router) Del ¶
func (r *Router) Del(pattern string, handler http.HandlerFunc)
Del adds a new Route for DELETE requests.
func (*Router) Filter ¶
func (r *Router) Filter(filter http.HandlerFunc)
Filter adds the middleware filter.
func (*Router) FilterParam ¶
func (r *Router) FilterParam(param string, filter http.HandlerFunc)
FilterParam adds the middleware filter iff the URL parameter exists.
func (*Router) Get ¶
func (r *Router) Get(pattern string, handler http.HandlerFunc)
Get adds a new Route for GET requests.
func (*Router) Patch ¶
func (r *Router) Patch(pattern string, handler http.HandlerFunc)
Patch adds a new Route for PATCH requests.
func (*Router) Post ¶
func (r *Router) Post(pattern string, handler http.HandlerFunc)
Post adds a new Route for POST requests.
func (*Router) Put ¶
func (r *Router) Put(pattern string, handler http.HandlerFunc)
Put adds a new Route for PUT requests.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
Required by http.Handler interface. This method is invoked by the http server and will handle all page routing
func (*Router) SetEnv ¶
SetEnv stores the specified environment variable as a key / value pair. If the environment variable is not set the default value will be used
func (*Router) Static ¶
Adds a new Route for Static http requests. Serves static files from the specified directory
func (*Router) TemplateFiles ¶
TemplateFiles parses the template definitions from the named files.
func (*Router) TemplateGlob ¶
TemplateGlob parses the template definitions from the files identified by the pattern, which must match at least one file.
type Values ¶
type Values map[interface{}]interface{}
Values maps a string key to a list of values.
func (Values) Del ¶
func (v Values) Del(key interface{})
Del deletes the values associated with key.
func (Values) Get ¶
func (v Values) Get(key interface{}) interface{}
Get gets the value associated with the given key. If there are no values associated with the key, Get returns nil.