Documentation
¶
Overview ¶
Package router provides a lightning fast HTTP router.
Example ¶
package main
import (
"fmt"
"github.com/gowww/router"
"net/http"
)
func main() {
rt := router.New()
// File server
rt.Get("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// Static route
rt.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
}))
// Path parameter
rt.Get("/users/:name", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Get user %s", router.Parameter(r, "name"))
}))
// Path parameter with regular expression
rt.Get(`users/:id:^\d+$`, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Page of user #%s", router.Parameter(r, "id"))
}))
// Path parameter + Trailing slash for wildcard
rt.Post("/users/:id/files/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Post file %s to user %s", router.Parameter(r, "*"), router.Parameter(r, "id"))
}))
// Custom "not found"
rt.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
})
http.ListenAndServe(":8080", rt)
}
Output:
Index ¶
- func Parameter(r *http.Request, key string) string
- type Router
- func (rt *Router) Delete(path string, handler http.Handler)
- func (rt *Router) Get(path string, handler http.Handler)
- func (rt *Router) Handle(method, path string, handler http.Handler)
- func (rt *Router) Patch(path string, handler http.Handler)
- func (rt *Router) Post(path string, handler http.Handler)
- func (rt *Router) Put(path string, handler http.Handler)
- func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (rt *Router) String() (s string)
- Bugs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Router ¶
The Router is the main structure of this package.
Notes ¶
Bugs ¶
If the client has no route for "/" and this split occurs on the first level because of the 2nd byte (just after the leading "/"), the isRoot flag of the parent node ("/") is false. It's not a problem because it has no handler and will never match a request, but it's not clean. A first solution would be to let makeChild know the current level in tree, but... All that for this?
Click to show internal directories.
Click to hide internal directories.