7Days/day2/gee/router.go

28 lines
572 B
Go
Raw Normal View History

2023-01-26 23:26:32 +08:00
package gee
import "net/http"
type router struct {
handlers map[string]HandlerFunc
}
func newRouter() *router {
return &router{handlers: make(map[string]HandlerFunc)}
}
// addRoute 添加路由规则
func (r *router) addRoute(method string, pattern string, handler HandlerFunc) {
key := method + "-" + pattern
r.handlers[key] = handler
}
// handle 路由匹配
func (r *router) handle(c *Context) {
key := c.Method + "-" + c.Path
if handler, ok := r.handlers[key]; ok {
handler(c)
} else {
c.String(http.StatusNotFound, "404 NOT FOUND: %s\n", c.Path)
}
}