Skip to content
Branch: master
Go to file
Code

Latest commit

joachimschmidt557 authored and Vexu committed d3faeaa Jul 2, 2020
Update to latest zig master
- @asynccall function signature changed
- std.mem.dupe deprecated

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Routez

HTTP server for Zig. See it in action

Example

basic

Run with zig build basic

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));
}
You can’t perform that action at this time.