Quick Start

Spider is a web framework for Zig. This guide gets you from zero to a running HTTP server in under 5 minutes.

Prerequisites

You need Zig 0.16.0-dev.2676+ or later. If you don't have it yet, download it at ziglang.org/download.

Create a new project

sh
mkdir myapp && cd myapp
zig init

This generates build.zig, build.zig.zon, and src/main.zig.

Fetch Spider

sh
zig fetch --save git+https://github.com/llllOllOOll/spider

Spider is added to your build.zig.zon automatically:

zig
// build.zig.zon
.dependencies = .{
    .spider = .{
        .url = "git+https://github.com/llllOllOOll/spider#f79bfab69d1c99fa552583bd2cc627d34f894260",
        .hash = "spider-0.3.0-RIctlSneAQAo31OblO8nx0AZLGT0-d5qjb4aAjdzp4lP",
    },
},

Configure build.zig

Open build.zig and add Spider as a dependency of your executable:

zig
const spider_dep = b.dependency("spider", .{ .target = target });

const exe = b.addExecutable(.{
    .name = "myapp",
    .root_module = b.createModule(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "spider", .module = spider_dep.module("spider") },
        },
    }),
});

Write your first handler

Replace src/main.zig with:

zig
const std = @import("std");
const spider = @import("spider");

pub fn main(init: std.process.Init) !void {
    const arena: std.mem.Allocator = init.arena.allocator();
    const io = init.io;
    const server = try spider.Spider.init(arena, io, "127.0.0.1", 8080, .{
        // .layout = @embedFile("views/layout.html"), // optional
    });
    defer server.deinit();

    server.get("/ping", pingHandler).listen() catch |err| {
        std.log.err("server error: {}", .{err});
        return err;
    };
}

fn pingHandler(alc: std.mem.Allocator, _: *spider.Request) !spider.Response {
    return spider.Response.json(alc, .{ .msg = "pong" });
}

Run

sh
zig build run

Test it in another terminal:

sh
curl http://localhost:8080/ping
# {"msg":"pong"}
You should also see Spider logging each request in the terminal:
[[SPIDER]] | 200 | 0ns | GET "/ping"

Next Steps

Now that your server is running, explore what Spider can do:

  • Router — dynamic params, wildcards, chained routes
  • PostgreSQL — built-in PG client with struct mapping
  • WebSocket — real-time with hub broadcasting