1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

added docs & docserver

This commit is contained in:
Rene Schallner 2023-12-30 00:51:05 +01:00
parent 82323f835e
commit 521b0deaf6
2 changed files with 81 additions and 0 deletions

View file

@ -21,6 +21,21 @@ pub fn build(b: *std.build.Builder) !void {
const all_step = b.step("all", "build all examples");
// -- Docs
const docs_obj = b.addObject(.{
.name = "docs",
.root_source_file = .{ .path = "src/zap.zig" },
.target = target,
.optimize = .Debug,
});
const install_docs = b.addInstallDirectory(.{
.install_dir = .prefix,
.install_subdir = "docs",
.source_dir = docs_obj.getEmittedDocs(),
});
b.step("docs", "Build docs").dependOn(&install_docs.step);
// --
inline for ([_]struct {
name: []const u8,
src: []const u8,
@ -198,6 +213,28 @@ pub fn build(b: *std.build.Builder) !void {
pkghash_step.dependOn(&pkghash_build_step.step);
all_step.dependOn(&pkghash_build_step.step);
//
// docserver
//
const docserver_exe = b.addExecutable(.{
.name = "docserver",
.root_source_file = .{ .path = "./tools/docserver.zig" },
.target = target,
.optimize = optimize,
});
docserver_exe.linkLibrary(facilio);
docserver_exe.addModule("zap", zap_module);
var docserver_step = b.step("docserver", "Build docserver");
const docserver_build_step = b.addInstallArtifact(docserver_exe, .{});
docserver_step.dependOn(&docserver_build_step.step);
docserver_step.dependOn(&install_docs.step);
const docserver_run_step = b.step("run-docserver", "run the docserver");
const docserver_run = b.addRunArtifact(docserver_exe);
docserver_run_step.dependOn(&docserver_run.step);
all_step.dependOn(&docserver_build_step.step);
//
// announceybot
//

44
tools/docserver.zig Normal file
View file

@ -0,0 +1,44 @@
const std = @import("std");
const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void {
r.setStatus(.not_found);
r.sendBody("<html><body><h1>404 - File not found</h1></body></html>") catch return;
}
pub fn main() !void {
var args_it = std.process.args();
var port: usize = 8080;
var docs_dir: []const u8 = "zig-out/docs";
while (args_it.next()) |arg| {
if (std.mem.startsWith(u8, arg, "--port=")) {
// try to parse port
if (std.fmt.parseUnsigned(usize, arg[7..], 0)) |the_port| {
port = the_port;
} else |_| {
std.debug.print("Invalid port number. Using default port {}\n", .{port});
}
}
if (std.mem.startsWith(u8, arg, "--docs=")) {
docs_dir = arg[7..];
}
}
var listener = zap.SimpleHttpListener.init(.{
.port = port,
.on_request = on_request,
.public_folder = docs_dir,
.log = true,
});
try listener.listen();
std.debug.print("\nServing docs from {s} at 0.0.0.0:{}\n", .{ docs_dir, port });
// start worker threads
zap.start(.{
.threads = 2,
.workers = 1,
});
}