https://github.com/hanaasagi/url-router
URL router
https://github.com/hanaasagi/url-router
http radix-tree router url zig ziglang
Last synced: 12 months ago
JSON representation
URL router
- Host: GitHub
- URL: https://github.com/hanaasagi/url-router
- Owner: Hanaasagi
- License: apache-2.0
- Created: 2023-12-16T12:22:02.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-29T03:49:15.000Z (over 2 years ago)
- Last Synced: 2025-07-02T05:38:44.985Z (12 months ago)
- Topics: http, radix-tree, router, url, zig, ziglang
- Language: Zig
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# URL-Router
```zig
const Request = struct {
params: Params,
method: enum { GET, POST },
// ...
};
const Response = struct {
// ...
};
const App = struct {
router: Router(*const (fn (*Request, *Response) void)),
const Self = @This();
fn init(allocator: std.mem.Allocator) Self {
return .{
.router = Router(*const (fn (*Request, *Response) void)).init(allocator),
};
}
fn deinit(self: *Self) void {
self.router.deinit();
}
fn get(self: *Self, path: []const u8, handler: *const (fn (*Request, *Response) void)) void {
self.router.insert(path, handler) catch @panic("TODO");
}
fn handle_request(self: *Self, full_url: []const u8) void {
// parse the url
const path = full_url;
const match = self.router.match(path) catch @panic("TODO");
var request = Request{
.method = .GET,
.params = match.params,
};
var response = Response{};
_ = match.value(&request, &response);
}
// ...
};
fn search_handler(request: *Request, response: *Response) void {
_ = request;
_ = response;
std.log.err("search handler is called", .{});
// ...
}
fn static_handler(request: *Request, response: *Response) void {
_ = response;
std.log.err("static handler is called, name is {s}", .{request.params.get("name").?});
// ...
}
test "test" {
var app = App.init(testing.allocator);
defer app.deinit();
app.get("/search", search_handler);
app.get("/static/:name", static_handler);
app.handle_request("/search");
app.handle_request("/static/robots.txt");
}
```