Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
- uses: actions/checkout@v5
- uses: mlugg/setup-zig@v2
with:
version: 0.14.0
- name: Build all examples
version: 0.15.2
- name: Build all examples
run: zig build

run-tests:
Expand All @@ -35,9 +35,9 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2
- uses: goto-bus-stop/setup-zig@v2
- uses: actions/checkout@v5
- uses: mlugg/setup-zig@v2
with:
version: 0.14.0
- name: Build all examples
version: 0.15.2
- name: Build all examples
run: zig build test --summary all
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


## Installing
Compatible Zig Version: `0.14.0`
Compatible Zig Version: `0.15.2`

Compatible [tardy](https://github.com/tardy-org/tardy) Version: `v0.3.0`

Expand Down Expand Up @@ -62,8 +62,8 @@ zzz can be configured to utilize minimal memory while remaining performant. The
- `poll` for Linux, Mac and Windows.
- Layered Router, including Middleware
- Single and Multithreaded Support
- TLS using [secsock](https://github.com/tardy-org/secsock)
- Memory Pooling for minimal allocations
- TLS using [secsock](https://github.com/tardy-org/secsock)
- Memory Pooling for minimal allocations

## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in zzz by you, shall be licensed as MPL2.0, without any additional terms or conditions.
26 changes: 16 additions & 10 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ pub fn build(b: *std.Build) void {

const tests = b.addTest(.{
.name = "tests",
.root_source_file = b.path("./src/tests.zig"),
.root_module = b.addModule("tests", .{
.root_source_file = b.path("./src/tests.zig"),
.target = target,
.optimize = optimize,
}),
});
tests.root_module.addImport("tardy", tardy);
tests.root_module.addImport("secsock", secsock);
Expand All @@ -55,22 +59,24 @@ fn add_example(
name: []const u8,
link_libc: bool,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Mode,
optimize: std.builtin.OptimizeMode,
zzz_module: *std.Build.Module,
) void {
const example = b.addExecutable(.{
.name = name,
const mod = b.createModule(.{
.root_source_file = b.path(b.fmt("./examples/{s}/main.zig", .{name})),
.target = target,
.optimize = optimize,
.target = target,
.strip = false,
.link_libc = link_libc,
});
mod.addImport("zzz", zzz_module);

if (link_libc) {
example.linkLibC();
}

example.root_module.addImport("zzz", zzz_module);
const example = b.addExecutable(.{
.name = name,
.root_module = mod,
// without llvm leads to error: undefined symbol: tardy_swap_frame
.use_llvm = true,
});

const install_artifact = b.addInstallArtifact(example, .{});
b.getInstallStep().dependOn(&install_artifact.step);
Expand Down
11 changes: 5 additions & 6 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
.name = .zzz,
.fingerprint = 0xc3273dca261a7ae0,
.version = "0.3.0",
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.2",
.dependencies = .{
.tardy = .{
.url = "git+https://github.com/tardy-org/tardy?ref=v0.3.0#cd454060f3b6006368d53c05ab96cd16c73c34de",
.hash = "tardy-0.3.0-69wrgi7PAwDFhO7m0aXae6N15s2b28VIOrnRrSHHake6",
.url = "git+https://github.com/bernardassan/tardy?ref=zig-0.15.2#edd54c2dbdb745760848b083b7f844b05d531148",
.hash = "tardy-0.3.0-69wrgn77AwDLFqiryrDCuNl-q7xF9VEUdONc7ytrNvsM",
},
.secsock = .{
.url = "git+https://github.com/tardy-org/secsock?ref=v0.1.0#263dcd630e32c7a5c7a0522a8d1fd04e39b75c24",
.hash = "secsock-0.0.0-p0qurf09AQD95s1NQF2MGpBqMmFz7cKZWibsgv_SQBAr",
.url = "git+https://github.com/bernardassan/secsock?ref=zig-0.15.1#25cec3e1b68dac92c17f3071caacff6b71c00b68",
.hash = "secsock-0.0.0-p0qurQhGAQBqG1dPh8s4htvzl1w8qiGBCxUV9uTOrt9h",
},
},

.paths = .{
"README.md",
"LICENSE",
Expand Down
15 changes: 9 additions & 6 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,22 @@ pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;

var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
var gpa: std.heap.DebugAllocator(.{ .thread_safe = true }) = .init;
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var t = try Tardy.init(allocator, .{ .threading = .auto });
var t: Tardy = try .init(allocator, .{ .threading = .auto });
defer t.deinit();

var router = try Router.init(allocator, &.{
var router: Router = try .init(allocator, &.{
Route.init("/").get({}, base_handler).layer(),
}, .{});
defer router.deinit(allocator);

var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
// create socket for tardy
var socket: Socket = try .init(.{
.tcp = .{ .host = host, .port = port },
});
defer socket.close_blocking();
try socket.bind();
try socket.listen(4096);
Expand All @@ -65,7 +68,7 @@ pub fn main() !void {
EntryParams{ .router = &router, .socket = socket },
struct {
fn entry(rt: *Runtime, p: EntryParams) !void {
var server = Server.init(.{
var server: Server = .init(.{
.stack_size = 1024 * 1024 * 4,
.socket_buffer_bytes = 1024 * 2,
.keepalive_count_max = null,
Expand All @@ -78,4 +81,4 @@ pub fn main() !void {
}
```

The snippet above handles all of the basic tasks involved with serving a plaintext route using zzz's HTTP implementation.
The snippet above handles all of the basic tasks involved with serving a plaintext route using zzz's HTTP implementation.
20 changes: 14 additions & 6 deletions docs/https.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Respond = http.Respond;

const secsock = zzz.secsock;
const SecureSocket = secsock.SecureSocket;
const Compression = http.Middlewares.Compression;

fn root_handler(ctx: *const Context, _: void) !Respond {
const body =
Expand All @@ -50,25 +51,32 @@ pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa: std.heap.DebugAllocator(.{ .thread_safe = true }) = .{
.backing_allocator = std.heap.smp_allocator,
};
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var t = try Tardy.init(allocator, .{ .threading = .auto });
var t: Tardy = try .init(allocator, .{ .threading = .auto });
defer t.deinit();

var router = try Router.init(allocator, &.{
var router: Router = try .init(allocator, &.{
Route.init("/").get({}, root_handler).layer(),
Compression(.{ .gzip = .{} }),
Route.init("/embed/pico.min.css").embed_file(
.{ .mime = .CSS },
@embedFile("embed/pico.min.css"),
).layer(),
}, .{});
defer router.deinit(allocator);

// create socket for tardy
var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
var socket: Socket = try .init(.{ .tcp = .{ .host = host, .port = port } });
defer socket.close_blocking();
try socket.bind();
try socket.listen(1024);

var bearssl = secsock.BearSSL.init(allocator);
var bearssl: secsock.BearSSL = .init(allocator);
defer bearssl.deinit();
try bearssl.add_cert_chain(
"CERTIFICATE",
Expand All @@ -87,7 +95,7 @@ pub fn main() !void {
EntryParams{ .router = &router, .socket = secure },
struct {
fn entry(rt: *Runtime, p: EntryParams) !void {
var server = Server.init(.{ .stack_size = 1024 * 1024 * 8 });
var server: Server = .init(.{ .stack_size = 1024 * 1024 * 8 });
try server.serve(rt, p.router, .{ .secure = p.socket });
}
}.entry,
Expand Down
20 changes: 11 additions & 9 deletions examples/basic/main.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const std = @import("std");
const log = std.log.scoped(.@"examples/basic");

const zzz = @import("zzz");
const http = zzz.HTTP;

const tardy = zzz.tardy;
const Tardy = tardy.Tardy(.auto);
const Runtime = tardy.Runtime;
const Socket = tardy.Socket;

const Server = http.Server;
const Router = http.Router;
const Context = http.Context;
const Route = http.Route;
const Respond = http.Respond;

const log = std.log.scoped(.@"examples/basic");

const Tardy = tardy.Tardy(.auto);

fn base_handler(ctx: *const Context, _: void) !Respond {
return ctx.response.apply(.{
.status = .OK,
Expand All @@ -27,20 +27,22 @@ pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;

var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
var gpa: std.heap.DebugAllocator(.{ .thread_safe = true }) = .init;
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var t = try Tardy.init(allocator, .{ .threading = .auto });
var t: Tardy = try .init(allocator, .{ .threading = .auto });
defer t.deinit();

var router = try Router.init(allocator, &.{
var router: Router = try .init(allocator, &.{
Route.init("/").get({}, base_handler).layer(),
}, .{});
defer router.deinit(allocator);

// create socket for tardy
var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
var socket: Socket = try .init(.{
.tcp = .{ .host = host, .port = port },
});
defer socket.close_blocking();
try socket.bind();
try socket.listen(4096);
Expand All @@ -54,7 +56,7 @@ pub fn main() !void {
EntryParams{ .router = &router, .socket = socket },
struct {
fn entry(rt: *Runtime, p: EntryParams) !void {
var server = Server.init(.{
var server: Server = .init(.{
.stack_size = 1024 * 1024 * 4,
.socket_buffer_bytes = 1024 * 2,
.keepalive_count_max = null,
Expand Down
22 changes: 11 additions & 11 deletions examples/cookies/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
const std = @import("std");
const log = std.log.scoped(.@"examples/cookies");

const zzz = @import("zzz");
const http = zzz.HTTP;

const tardy = zzz.tardy;
const Tardy = tardy.Tardy(.auto);
const Runtime = tardy.Runtime;
const Socket = tardy.Socket;

const Server = http.Server;
const Router = http.Router;
const Context = http.Context;
Expand All @@ -17,14 +13,18 @@ const Middleware = http.Middleware;
const Respond = http.Respond;
const Cookie = http.Cookie;

const log = std.log.scoped(.@"examples/cookies");

const Tardy = tardy.Tardy(.auto);

fn base_handler(ctx: *const Context, _: void) !Respond {
var iter = ctx.request.cookies.iterator();
while (iter.next()) |kv| log.debug("cookie: k={s} v={s}", .{ kv.key_ptr.*, kv.value_ptr.* });

const cookie = Cookie.init("example_cookie", "abcdef123");
const cookie: Cookie = .init("example_cookie", "abcdef123");
return ctx.response.apply(.{
.status = .OK,
.mime = http.Mime.HTML,
.mime = .HTML,
.body = "Hello, world!",
.headers = &.{
.{ "Set-Cookie", try cookie.to_string_alloc(ctx.allocator) },
Expand All @@ -36,20 +36,20 @@ pub fn main() !void {
const host: []const u8 = "0.0.0.0";
const port: u16 = 9862;

var gpa = std.heap.GeneralPurposeAllocator(.{ .thread_safe = true }){};
var gpa: std.heap.DebugAllocator(.{ .thread_safe = true }) = .init;
const allocator = gpa.allocator();
defer _ = gpa.deinit();

var t = try Tardy.init(allocator, .{ .threading = .single });
var t: Tardy = try .init(allocator, .{ .threading = .single });
defer t.deinit();

var router = try Router.init(allocator, &.{
var router: Router = try .init(allocator, &.{
Route.init("/").get({}, base_handler).layer(),
}, .{});
defer router.deinit(allocator);

// create socket for tardy
var socket = try Socket.init(.{ .tcp = .{ .host = host, .port = port } });
var socket: Socket = try .init(.{ .tcp = .{ .host = host, .port = port } });
defer socket.close_blocking();
try socket.bind();
try socket.listen(4096);
Expand All @@ -63,7 +63,7 @@ pub fn main() !void {
EntryParams{ .router = &router, .socket = socket },
struct {
fn entry(rt: *Runtime, p: EntryParams) !void {
var server = Server.init(.{
var server: Server = .init(.{
.stack_size = 1024 * 1024 * 4,
.socket_buffer_bytes = 1024 * 2,
.keepalive_count_max = null,
Expand Down
Loading