mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 15:43:21 +00:00
new router implementation
This commit is contained in:
parent
c19c63325c
commit
d4717695cb
2 changed files with 191 additions and 80 deletions
83
router.go
Normal file
83
router.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue