this post was submitted on 27 Feb 2024
0 points (50.0% liked)

Zig Programming Language

157 readers
1 users here now

A lemm.ee community for Zig!

founded 1 year ago
MODERATORS
 

From the README:

zig-router

Straightforward HTTP-like request routing.


Project is tested against zig 0.12.0-dev.2341+92211135f

Sample

const router = @import("zig-router");

const MyJsonBody = struct {
    float: f32,
    text: []const u8,
    number_with_default: u32 = 42,
};

fn putJson(body: MyJsonBody) !Response {
    log.info("float: {}", .{body.float});
    log.info("text: {s}", .{body.text});
    log.info("number_with_default: {}", .{body.number_with_default});
    return .{ .body = "ok" };
}

const MyPathParams = struct {
    id: []const u8,
    bundle: u32,
};

fn getDynamic(params: MyPathParams) !Response {
    log.info("id: {s}", .{params.id});
    log.info("bundle: {}", .{params.bundle});
    return .{};
}

const MyQuery = struct {
    id: []const u8 = "plz give me a good paying stable job",
    bundle: u32 = 42,
};

fn getQuery(query: MyQuery) !Response {
    log.info("id: {s}", .{query.id});
    log.info("bundle: {}", .{query.bundle});
    return .{};
}

fn getError() !void {
    return error.EPIC_FAIL;
}

fn onRequest(arena: *std.heap.ArenaAllocator, request: Request) !void {
    router.Router(.{
        router.Decoder(.json, router.JsonBodyDecoder(.{}, 4096).decode),
    }, .{
        router.Route(.PUT, "/json", putJson, .{}),
        router.Route(.GET, "/dynamic/:id/paths/:bundle", getDynamic, .{}),
        router.Route(.GET, "/query", getQuery, .{}),
        router.Route(.GET, "/error", getError, .{}),
    }).match(arena.allocator(), .{
        .method = request.method,
        .path = request.path,
        .query = request.query,
        .body = .{ .reader = request.body.reader() }
    }, .{ arena.allocator() }) catch |err| switch (err) {
        error.not_found => return .{ .status = .not_found },
        error.bad_request => return .{ .status = .bad_request },
        else => return err,
    };
}

Depend

build.zig.zon

.zig_router = .{
  .url = "https://github.com/Cloudef/zig-router/archive/{COMMIT}.tar.gz",
  .hash = "{HASH}",
},

build.zig

const zig_router = b.dependency("zig_router", .{}).module("zig-router");
exe.root_module.addImport("zig-router", zig_router);
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here