rest-server/router.go

84 lines
1.8 KiB
Go
Raw Normal View History

2015-09-17 13:46:29 +02:00
package main
import (
"log"
"net/http"
"strings"
)
type Route struct {
path []string
handler http.Handler
}
type Router struct {
routes map[string][]Route
}
func NewRouter() *Router {
return &Router{make(map[string][]Route)}
}
func (router *Router) Options(path string, handler http.HandlerFunc) {
router.Handle("OPTIONS", path, handler)
}
func (router *Router) Get(path string, handler http.HandlerFunc) {
router.Handle("GET", path, handler)
}
func (router *Router) Head(path string, handler http.HandlerFunc) {
router.Handle("HEAD", path, handler)
}
func (router *Router) Post(path string, handler http.HandlerFunc) {
router.Handle("POST", path, handler)
}
func (router *Router) Put(path string, handler http.HandlerFunc) {
router.Handle("PUT", path, handler)
}
func (router *Router) Delete(path string, handler http.HandlerFunc) {
router.Handle("DELETE", path, handler)
}
func (router *Router) Trace(path string, handler http.HandlerFunc) {
router.Handle("TRACE", path, handler)
}
func (router *Router) Connect(path string, handler http.HandlerFunc) {
router.Handle("Connect", path, handler)
}
func (router *Router) Handle(method string, uri string, handler http.HandlerFunc) {
routes := router.routes[method]
path := strings.Split(uri, "/")
routes = append(routes, Route{path, handler})
router.routes[method] = routes
}
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
method := r.Method
uri := r.RequestURI
path := strings.Split(uri, "/")
log.Printf("%s %s", method, uri)
ROUTE:
for _, route := range router.routes[method] {
if len(route.path) != len(path) {
continue
}
for i := 0; i < len(route.path); i++ {
if !strings.HasPrefix(route.path[i], ":") && route.path[i] != path[i] {
continue ROUTE
}
}
route.handler.ServeHTTP(w, r)
return
}
http.Error(w, "404 not found", 404)
}