Initial commit
This commit is contained in:
commit
1368b7b69b
10 changed files with 588 additions and 0 deletions
42
router.odin
Normal file
42
router.odin
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue