81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
|
package framework
|
|||
|
|
|||
|
import (
|
|||
|
"log"
|
|||
|
"net/http"
|
|||
|
"strings"
|
|||
|
)
|
|||
|
|
|||
|
type Serve struct {
|
|||
|
Addr string
|
|||
|
//Handler Hander
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
type Core struct {
|
|||
|
router map[string]*Tree
|
|||
|
}
|
|||
|
|
|||
|
func NewCore() *Core {
|
|||
|
router := map[string]*Tree{}
|
|||
|
router["GET"] = NewTree()
|
|||
|
router["POST"] = NewTree()
|
|||
|
router["PUT"] = NewTree()
|
|||
|
router["DELETE"] = NewTree()
|
|||
|
|
|||
|
return &Core{router: router}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) Get(url string, handler ControllerHandler) {
|
|||
|
if err := c.router["GET"].AddRouter(url, handler); err != nil {
|
|||
|
log.Fatal("add router error: ", err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) POST(url string, handler ControllerHandler) {
|
|||
|
if err := c.router["POST"].AddRouter(url, handler); err != nil {
|
|||
|
log.Fatal("add router error: ", err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) PUT(url string, handler ControllerHandler) {
|
|||
|
if err := c.router["PUT"].AddRouter(url, handler); err != nil {
|
|||
|
log.Fatal("add router error: ", err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) DELETE(url string, handler ControllerHandler) {
|
|||
|
if err := c.router["DELETE"].AddRouter(url, handler); err != nil {
|
|||
|
log.Fatal("add router error: ", err)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
log.Println("core.serveHTTP")
|
|||
|
ctx := NewContext(r, w)
|
|||
|
// 寻找路由
|
|||
|
router := c.FindRouteByRequest(r)
|
|||
|
if router == nil { // 如果没有找到,这里打印日志
|
|||
|
ctx.Json(404, "not found")
|
|||
|
return
|
|||
|
}
|
|||
|
// 调用路由函数,如果返回err 代表存在内部错误,返回500状态码
|
|||
|
if err := router(ctx); err != nil {
|
|||
|
ctx.Json(500, "inner error")
|
|||
|
return
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
func (c *Core) FindRouteByRequest(request *http.Request) ControllerHandler {
|
|||
|
uri := request.URL.Path
|
|||
|
method := request.Method
|
|||
|
upperMethod := strings.ToUpper(method)
|
|||
|
upperUri := strings.ToUpper(uri)
|
|||
|
|
|||
|
if methodHandlers, ok := c.router[upperMethod]; ok {
|
|||
|
return methodHandlers.FindHandler(upperUri)
|
|||
|
}
|
|||
|
|
|||
|
return nil
|
|||
|
}
|