Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vexu/routez
Http server for Zig
https://github.com/vexu/routez
http server zig
Last synced: about 1 month ago
JSON representation
Http server for Zig
- Host: GitHub
- URL: https://github.com/vexu/routez
- Owner: Vexu
- License: mit
- Archived: true
- Created: 2019-06-08T11:33:41.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-06T11:23:03.000Z (over 1 year ago)
- Last Synced: 2024-10-01T07:04:13.870Z (about 1 month ago)
- Topics: http, server, zig
- Language: Zig
- Homepage: https://routez.vexu.eu
- Size: 171 KB
- Stars: 242
- Watchers: 7
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Routez
HTTP server for Zig.## Example
### [basic](examples/basic.zig)
Run with `zig build basic`
```Zig
const std = @import("std");
const Address = std.net.Address;
usingnamespace @import("routez");
const allocator = std.heap.page_allocator;pub const io_mode = .evented;
pub fn main() !void {
var server = Server.init(
allocator,
.{},
.{
all("/", indexHandler),
get("/about", aboutHandler),
get("/about/more", aboutHandler2),
get("/post/{post_num}/?", postHandler),
static("./", "/static"),
all("/counter", counterHandler),
},
);
var addr = try Address.parseIp("127.0.0.1", 8080);
try server.listen(addr);
}fn indexHandler(req: Request, res: Response) !void {
try res.sendFile("examples/index.html");
}fn aboutHandler(req: Request, res: Response) !void {
try res.write("Hello from about\n");
}fn aboutHandler2(req: Request, res: Response) !void {
try res.write("Hello from about2\n");
}fn postHandler(req: Request, res: Response, args: *const struct {
post_num: []const u8,
}) !void {
try res.print("Hello from post, post_num is {}\n", args.post_num);
}var counter = std.atomic.Int(usize).init(0);
fn counterHandler(req: Request, res: Response) !void {
try res.print("Page loaded {} times\n", counter.fetchAdd(1));
}
```