fjord/router.odin
2025-11-03 13:50:38 +01:00

42 lines
1.1 KiB
Odin

package fjord
import "core:path/slashpath"
import http "http"
import rxt "radix_tree"
EndpointHandler :: struct($Error_Type: typeid) {
method_handlers: map[http.Method]#type proc(
request: ^http.Request,
) -> (
http.Response,
Error_Type,
),
path_variables: []string,
}
Router :: struct {
radix_tree: rxt.RadixTree(EndpointHandler),
}
router_init :: proc(router: ^Router, allocator := context.allocator) {
rxt.init(&router.radix_tree, allocator)
}
router_destroy :: proc(router : ^Router) {
rxt.destroy(&router.radix_tree)
}
router_lookup :: proc(router: ^Router, key: string) -> (endpoint_handler: EndpointHandler, ok: bool) {
return rxt.lookup(&router.radix_tree, key)
}
router_add_route :: proc(router: ^Router, key: []string, value: EndpointHandler) {
joined_path := slashpath.join(key, context.allocator)
defer delete(joined_path)
rxt.insert(&router.radix_tree, joined_path[:], value)
}
router_remove_route :: proc(router: ^Router, key: string) -> (ok: bool){
return rxt.remove(&router.radix_tree, key)
}