Added mounting subrouters
This commit is contained in:
@@ -9,15 +9,26 @@ import (
|
|||||||
"git.aiterp.net/gisle/wrouter/auth"
|
"git.aiterp.net/gisle/wrouter/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Route is an interface for a request handler.
|
||||||
type Route interface {
|
type Route interface {
|
||||||
Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool
|
Handle(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Router is the main structure of the wrouter package. It routes requests to the appropriate
|
||||||
|
// Route
|
||||||
type Router struct {
|
type Router struct {
|
||||||
paths map[Route]string
|
paths map[Route]string
|
||||||
routes []Route
|
routes []Route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mount a router to the router, prefixing all the paths of it
|
||||||
|
func (router *Router) Mount(path string, subRouter *Router) {
|
||||||
|
for _, route := range subRouter.routes {
|
||||||
|
router.Route(strings.Replace(path+subRouter.paths[route], "//", "/", 1), route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Route mounts a route interface to a path
|
||||||
func (router *Router) Route(path string, route Route) {
|
func (router *Router) Route(path string, route Route) {
|
||||||
if router.paths == nil {
|
if router.paths == nil {
|
||||||
router.paths = make(map[Route]string, 16)
|
router.paths = make(map[Route]string, 16)
|
||||||
@@ -27,6 +38,7 @@ func (router *Router) Route(path string, route Route) {
|
|||||||
router.routes = append(router.routes, route)
|
router.routes = append(router.routes, route)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeHTTP serves a HTTP request using the router's routes
|
||||||
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
req.ParseForm()
|
req.ParseForm()
|
||||||
defer req.Body.Close()
|
defer req.Body.Close()
|
||||||
@@ -75,18 +87,24 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
w.Write([]byte("Not Found: " + req.URL.Path))
|
w.Write([]byte("Not Found: " + req.URL.Path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resource is a shorthand for creating and adding a new REST resource to the router
|
||||||
func (router *Router) Resource(mount string, list, create ResourceFunc, get, update, delete ResourceIDFunc) {
|
func (router *Router) Resource(mount string, list, create ResourceFunc, get, update, delete ResourceIDFunc) {
|
||||||
router.Route(mount, NewResource(list, create, get, update, delete))
|
router.Route(mount, NewResource(list, create, get, update, delete))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Static is a shorthand for creating a static file server on the following path
|
||||||
func (router *Router) Static(mount string, filePath string) {
|
func (router *Router) Static(mount string, filePath string) {
|
||||||
router.Route(mount, NewStatic(filePath))
|
router.Route(mount, NewStatic(filePath))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function creates a simple bare-bones handler that just runs a function, prevents
|
||||||
|
// the need for dummy interfaces
|
||||||
func (router *Router) Function(mount string, function FunctionHandlerFunc) {
|
func (router *Router) Function(mount string, function FunctionHandlerFunc) {
|
||||||
router.Route(mount, &functionHandler{function})
|
router.Route(mount, &functionHandler{function})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Listen creates a http.Server with some sane defaults and pointing to this structure
|
||||||
|
// for request handling.
|
||||||
func (router *Router) Listen(host string, port int) (*http.Server, error) {
|
func (router *Router) Listen(host string, port int) (*http.Server, error) {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: fmt.Sprintf("%s:%d", host, port),
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||||
|
|||||||
Reference in New Issue
Block a user