Skip to content

Instantly share code, notes, and snippets.

@thaolt
Last active March 16, 2024 08:32
Show Gist options
  • Save thaolt/cc9774ce7898f0f8aacea5daa51eda36 to your computer and use it in GitHub Desktop.
Save thaolt/cc9774ce7898f0f8aacea5daa51eda36 to your computer and use it in GitHub Desktop.
Zig mongoose ws simple http server example
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "http_server",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
exe.addCSourceFile(.{ .file = .{ .path = "src/mongoose.c" }, .flags = &[_][]const u8{
"-Wall",
"-Wextra",
"-Werror",
} });
exe.linkLibC();
exe.addIncludePath(.{ .path = "src" });
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
const std = @import("std");
const mg = @import("mongoose.zig");
const print = std.debug.print;
// Connection event handler function
fn handle(c: ?*mg.connection, ev: c_int, ev_data: ?*anyopaque) callconv(.C) void {
if (ev == mg.EV_HTTP_MSG) { // New HTTP request received
const hm: *mg.http_message = @ptrCast(@alignCast(ev_data)); // Parsed HTTP request
if (mg.match(hm.*.uri, mg.str_c("/api/hello"), null)) { // REST API call?
mg.http_reply(c, 200, null, "{%m:%d}\n", mg.print_esc, @as(c_int, 0), "status", @as(c_int, 1));
} else {
const opts = mg.http_serve_opts{
.root_dir = ".",
.ssi_pattern = null,
.extra_headers = null,
.mime_types = null,
.page404 = null,
.fs = null,
}; // For all other URLs,
mg.http_serve_dir(c, hm, &opts); // Serve static files
}
}
}
pub fn main() void {
var mgr: mg.mgr = undefined; // Mongoose event manager. Holds all connections
mg.mgr_init(&mgr); // Initialise event manager
_ = mg.http_listen(&mgr, "http://0.0.0.0:8000", handle, null); // Setup listener
mg.log("Listening on port 8000");
while (true) {
mg.mgr_poll(&mgr, 1000); // Infinite event loop
}
}
pub const mgc = @cImport({
@cInclude("mongoose.h");
});
pub const EV_HTTP_MSG = mgc.MG_EV_HTTP_MSG;
pub const mgr = mgc.struct_mg_mgr;
pub const connection = mgc.struct_mg_connection;
pub const http_message = mgc.struct_mg_http_message;
pub const http_serve_opts = mgc.struct_mg_http_serve_opts;
pub const str = mgc.struct_mg_str;
pub const log = mgc.mg_log;
pub const mprintf = mgc.mg_mprintf;
pub const mgr_init = mgc.mg_mgr_init;
pub const ws_connect = mgc.mg_ws_connect;
pub const listen = mgc.mg_listen;
pub const timer_init = mgc.mg_timer_init;
pub const mgr_poll = mgc.mg_mgr_poll;
pub const timer_poll = mgc.mg_timer_poll;
pub const mgr_free = mgc.mg_mgr_free;
pub const send = mgc.mg_send;
pub const path_is_sane = mgc.mg_path_is_sane;
pub const http_listen = mgc.mg_http_listen;
pub const match = mgc.mg_match;
pub const http_reply = mgc.mg_http_reply;
pub const http_serve_dir = mgc.mg_http_serve_dir;
pub const str_c = mgc.mg_str;
pub const print_esc = mgc.mg_print_esc;
pub const millis = mgc.mg_millis;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment