28 lines
572 B
Go
28 lines
572 B
Go
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)
|
|
}
|
|
}
|