nf/router.go

96 lines
1.8 KiB
Go
Raw Normal View History

2024-01-10 20:26:19 +08:00
package nf
2024-01-11 18:16:41 +08:00
import "strings"
2024-01-10 20:26:19 +08:00
type router struct {
2024-01-11 18:16:41 +08:00
roots map[string]*_node
2024-01-10 20:26:19 +08:00
handlers map[string][]HandlerFunc
}
func newRouter() *router {
return &router{
2024-01-11 18:16:41 +08:00
roots: make(map[string]*_node),
2024-01-10 20:26:19 +08:00
handlers: make(map[string][]HandlerFunc),
}
}
// Only one * is allowed
func parsePattern(pattern string) []string {
vs := strings.Split(pattern, "/")
parts := make([]string, 0)
for _, item := range vs {
if item != "" {
parts = append(parts, item)
if item[0] == '*' {
break
}
}
}
return parts
}
func (r *router) addRoute(method string, pattern string, handlers ...HandlerFunc) {
parts := parsePattern(pattern)
key := method + "-" + pattern
_, ok := r.roots[method]
if !ok {
2024-01-11 18:16:41 +08:00
r.roots[method] = &_node{}
2024-01-10 20:26:19 +08:00
}
r.roots[method].insert(pattern, parts, 0)
r.handlers[key] = handlers
}
2024-01-11 18:16:41 +08:00
func (r *router) getRoute(method string, path string) (*_node, map[string]string) {
2024-01-10 20:26:19 +08:00
searchParts := parsePattern(path)
params := make(map[string]string)
root, ok := r.roots[method]
if !ok {
return nil, nil
}
n := root.search(searchParts, 0)
if n != nil {
parts := parsePattern(n.pattern)
for index, part := range parts {
if part[0] == ':' {
params[part[1:]] = searchParts[index]
}
if part[0] == '*' && len(part) > 1 {
params[part[1:]] = strings.Join(searchParts[index:], "/")
break
}
}
return n, params
}
return nil, nil
}
2024-01-11 18:16:41 +08:00
func (r *router) getRoutes(method string) []*_node {
root, ok := r.roots[method]
if !ok {
return nil
}
nodes := make([]*_node, 0)
root.travel(&nodes)
return nodes
}
2024-01-10 20:26:19 +08:00
func (r *router) handle(c *Ctx) error {
2024-01-11 18:16:41 +08:00
node, params := r.getRoute(c.Method, c.Path)
if node != nil {
2024-01-10 20:26:19 +08:00
c.Params = params
2024-01-11 18:16:41 +08:00
key := c.Method + "-" + node.pattern
c.handlers = append(c.handlers, r.handlers[key]...)
2024-01-10 20:26:19 +08:00
} else {
2024-01-11 18:16:41 +08:00
_, err := c.Writef("404 NOT FOUND: %s\n", c.Path)
return err
2024-01-10 20:26:19 +08:00
}
2024-01-11 18:16:41 +08:00
return c.Next()
2024-01-10 20:26:19 +08:00
}