Skip to content

Instantly share code, notes, and snippets.

@syrm
Last active August 29, 2024 14:00
Show Gist options
  • Select an option

  • Save syrm/a300c367f66591d42e51e1d8652b21f3 to your computer and use it in GitHub Desktop.

Select an option

Save syrm/a300c367f66591d42e51e1d8652b21f3 to your computer and use it in GitHub Desktop.
zig http ok
zig build-exe main.zig -O ReleaseSmall
ls -lh
total 56K
-rw-r--r-- 1 srm srm 221 Aug 29 13:21 Dockerfile
-rwxr-xr-x 1 srm srm 17K Aug 29 15:58 main
-rw-r--r-- 1 srm srm 28K Aug 29 15:58 main.o
-rw-r--r-- 1 srm srm 665 Aug 29 15:39 main.zig
ldd main
not a dynamic executable
FROM chainguard/zig AS builder
WORKDIR /opt
COPY main.zig .
RUN zig build-exe main.zig -O ReleaseSmall
FROM scratch
COPY --from=builder /opt/main /usr/bin/main
ENTRYPOINT ["main"]
const std = @import("std");
const net = std.net;
const posix = std.posix;
pub fn main() anyerror!void {
_ = std.posix.Sigaction{
.handler = .{ .handler = sigint_handler },
.mask = std.posix.empty_sigset,
.flags = 0,
};
const localhost = net.Address.parseIp("0.0.0.0", 8080) catch unreachable;
var server = try localhost.listen(.{});
while (server.accept()) |client| {
try client.stream.writer().print("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK", .{});
client.stream.close();
} else |err| {
return err;
}
}
fn sigint_handler(_: i32) callconv(.C) void {
std.process.exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment