Router/router.go

84 lines
2.0 KiB
Go
Raw Normal View History

2024-08-22 21:46:21 -05:00
package router
2024-09-06 08:16:32 -05:00
type Router[T any] struct {
get Tree[T]
post Tree[T]
delete Tree[T]
put Tree[T]
patch Tree[T]
head Tree[T]
connect Tree[T]
trace Tree[T]
options Tree[T]
2024-08-22 21:46:21 -05:00
}
2024-09-05 13:02:29 -05:00
// Create a new Router containing trees for every HTTP method.
2024-09-06 08:16:32 -05:00
func New[T any]() *Router[T] {
return &Router[T]{}
2024-08-22 21:46:21 -05:00
}
// Registers a new handler for the given method and path.
2024-09-06 08:16:32 -05:00
func (router *Router[T]) Add(method string, path string, handler T) {
2024-08-22 21:46:21 -05:00
tree := router.selectTree(method)
tree.Add(path, handler)
}
// Finds the handler and parameters for the given route.
2024-09-06 08:16:32 -05:00
func (router *Router[T]) Lookup(method string, path string) (T, []Parameter) {
2024-08-22 21:46:21 -05:00
if method[0] == 'G' {
return router.get.Lookup(path)
}
tree := router.selectTree(method)
return tree.Lookup(path)
}
// Finds the handler and parameters for the given route without using any memory allocations.
2024-09-06 08:16:32 -05:00
func (router *Router[T]) LookupNoAlloc(method string, path string, addParameter func(string, string)) T {
2024-08-22 21:46:21 -05:00
if method[0] == 'G' {
return router.get.LookupNoAlloc(path, addParameter)
}
tree := router.selectTree(method)
return tree.LookupNoAlloc(path, addParameter)
}
// Traverses all trees and calls the given function on every node.
2024-09-06 08:16:32 -05:00
func (router *Router[T]) Map(transform func(T) T) {
2024-08-22 21:46:21 -05:00
router.get.Map(transform)
router.post.Map(transform)
router.delete.Map(transform)
router.put.Map(transform)
router.patch.Map(transform)
router.head.Map(transform)
router.connect.Map(transform)
router.trace.Map(transform)
router.options.Map(transform)
}
2024-09-05 13:02:29 -05:00
// Returns the tree of the given HTTP method.
2024-09-06 08:16:32 -05:00
func (router *Router[T]) selectTree(method string) *Tree[T] {
2024-08-22 21:46:21 -05:00
switch method {
case "GET":
return &router.get
case "POST":
return &router.post
case "DELETE":
return &router.delete
case "PUT":
return &router.put
case "PATCH":
return &router.patch
case "HEAD":
return &router.head
case "CONNECT":
return &router.connect
case "TRACE":
return &router.trace
case "OPTIONS":
return &router.options
default:
return nil
}
}