Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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));
}
```