Router: add static routers
This commit is contained in:
parent
913e33a993
commit
5cb54ae560
@ -3,21 +3,71 @@ package framework
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Core is the core struct of the framework
|
// Core is the core struct of the framework
|
||||||
type Core struct {
|
type Core struct {
|
||||||
router map[string]ControllerHandler
|
router map[string]map[string]ControllerHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCore initialize the Core.
|
// NewCore initialize the Core.
|
||||||
func NewCore() *Core {
|
func NewCore() *Core {
|
||||||
return &Core{router: map[string]ControllerHandler{}}
|
getRouter := map[string]ControllerHandler{}
|
||||||
|
postRouter := map[string]ControllerHandler{}
|
||||||
|
putRouter := map[string]ControllerHandler{}
|
||||||
|
deleteRouter := map[string]ControllerHandler{}
|
||||||
|
|
||||||
|
router := map[string]map[string]ControllerHandler{}
|
||||||
|
router["GET"] = getRouter
|
||||||
|
router["POST"] = postRouter
|
||||||
|
router["PUT"] = putRouter
|
||||||
|
router["DELETE"] = deleteRouter
|
||||||
|
|
||||||
|
return &Core{router: router}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get is a simple get router
|
// Get is a simple get router
|
||||||
func (c *Core) Get(url string, handler ControllerHandler) {
|
func (c *Core) Get(url string, handler ControllerHandler) {
|
||||||
c.router[url] = handler
|
upperUrl := strings.ToUpper(url)
|
||||||
|
c.router["GET"][upperUrl] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post is a simple post router
|
||||||
|
func (c *Core) Post(url string, handler ControllerHandler) {
|
||||||
|
upperUrl := strings.ToUpper(url)
|
||||||
|
c.router["POST"][upperUrl] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put is a simple put router
|
||||||
|
func (c *Core) Put(url string, handler ControllerHandler) {
|
||||||
|
upperUrl := strings.ToUpper(url)
|
||||||
|
c.router["PUT"][upperUrl] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete is a simple delete router
|
||||||
|
func (c *Core) Delete(url string, handler ControllerHandler) {
|
||||||
|
upperUrl := strings.ToUpper(url)
|
||||||
|
c.router["DELETE"][upperUrl] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Core) FindRouteByRequest(r *http.Request) ControllerHandler {
|
||||||
|
upperUri := strings.ToUpper(r.URL.Path)
|
||||||
|
upperMethod := strings.ToUpper(r.Method)
|
||||||
|
|
||||||
|
mapper, ok := c.router[upperMethod]
|
||||||
|
if !ok {
|
||||||
|
log.Printf("Method %q is not recognized\n", upperMethod)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
controller, ok := mapper[upperUri]
|
||||||
|
if !ok {
|
||||||
|
log.Printf("URI %q is not recognized\n", r.URL.Path)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return controller
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements the Handler interface
|
// ServeHTTP implements the Handler interface
|
||||||
@ -26,11 +76,15 @@ func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
ctx := NewContext(w, r)
|
ctx := NewContext(w, r)
|
||||||
|
|
||||||
// XXX: hard code foo handler for test purpose
|
router := c.FindRouteByRequest(r)
|
||||||
router := c.router["foo"]
|
|
||||||
if router == nil {
|
if router == nil {
|
||||||
|
ctx.WriteJSON(http.StatusNotFound, "Request not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = router(ctx)
|
err := router(ctx)
|
||||||
|
if err != nil {
|
||||||
|
ctx.WriteJSON(http.StatusInternalServerError, "Internal error")
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,5 @@ package main
|
|||||||
import "git.vinchent.xyz/vinchent/go-web/framework"
|
import "git.vinchent.xyz/vinchent/go-web/framework"
|
||||||
|
|
||||||
func registerRouter(core *framework.Core) {
|
func registerRouter(core *framework.Core) {
|
||||||
core.Get("foo", FooControllerHandler)
|
core.Get("/foo", FooControllerHandler)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user