robin/maivi

a tiny cute lua web framework

git clone https://git.robinwobin.dev/robin/maivi

robinmaivi: proper io usage6e07b35

push-srxvuswpkqxx
2.7 KiB94 linesraw
1const std = @import("std");
2const process = std.process;
3
4const Io = std.Io;
5const Allocator = std.mem.Allocator;
6
7const fern = @import("fern");
8const maivi = @import("maivi");
9const Handler = maivi.Handler;
10const helpers = maivi.helpers;
11const Status = helpers.Status;
12
13const mlua = @import("mlua.zig");
14
15pub fn main(init: process.Init) !void {
16    var args = init.minimal.args.iterate();
17    _ = args.skip();
18
19    if (args.next()) |arg| {
20        if (std.mem.eql(u8, arg, "build"))
21            maivi.build = true
22        else if (std.mem.eql(u8, arg, "dev"))
23            maivi.dev = true
24        else if (std.mem.eql(u8, arg, "-l"))
25            return mlua.main(init);
26    }
27
28    maivi.dev = maivi.dev or (!maivi.build and (if (init.environ_map.get("MAIVI_DEV")) |_| true else false));
29
30    const config: maivi.Config = try .init(init.io, init.arena, init.environ_map);
31
32    try fern.init(init.io, init.gpa, .{
33        .level = config.log_level,
34    });
35    defer fern.deinit();
36
37    var rev_buf: [0x80]u8 = undefined;
38    if (!maivi.dev)
39        maivi.rev = init.environ_map.get("MAIVI_REV") orelse rev_str(init.io, &rev_buf, config.root) catch |e|
40            blk: {
41                fern.warn().msg("version info cannot be retrieved from git. version must be provided using MAIVI_REV").err(e).log();
42                break :blk null;
43            };
44
45    try maivi.init(init.io, init.gpa, &config);
46    defer maivi.deinit(init.io);
47
48    try maivi.lua.setup(init.io);
49
50    if (maivi.build)
51        return maivi.gen.generate(init.io);
52
53    try maivi.server(init.io);
54    try maivi.listen(init.io);
55}
56
57fn rev_str(io: Io, buf: []u8, root: []const u8) ![]const u8 {
58    if (!process.can_spawn) {
59        return error.NoChildProcess;
60    }
61
62    const rev = try spawn(io, buf, &[_][]const u8{
63        "git",
64        "-C", root, // affects the --git-dir argument
65        "--git-dir", ".git", // affected by the -C argument
66        "rev-parse", "HEAD",
67    });
68
69    return std.mem.trim(u8, rev, std.ascii.whitespace[0..]);
70}
71
72fn spawn(io: Io, buf: []u8, argv: []const []const u8) ![]u8 {
73    var child = try process.spawn(io, .{
74        .argv = argv,
75        .stdin = .ignore,
76        .stdout = .pipe,
77        .stderr = .ignore,
78    });
79    errdefer child.kill(io);
80
81    var rbuf: [0x80]u8 = undefined;
82    var stdout_reader = child.stdout.?.readerStreaming(io, &rbuf);
83    const n = try stdout_reader.interface.readSliceShort(buf);
84
85    const term = try child.wait(io);
86    return switch (term) {
87        .exited => |code| if (code != 0)
88            error.ExitCodeFailure
89        else
90            return buf[0..n],
91        .signal, .stopped => error.ProcessTerminated,
92        .unknown => error.ProcessTerminated,
93    };
94}