robin/maivi

a tiny cute lua web framework

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

robinconfig: use runtime env `MAIVI_RUNTIME`0520f68

push-srxvuswpkqxx
2.2 KiB62 linesraw
1const std = @import("std");
2
3const fern = @import("fern");
4const options = @import("options");
5
6const Self = @This();
7
8root: []const u8,
9// root for html files, defaults to `root + "build"`
10buildroot: []const u8,
11// root for lua modules, defaults to `root + "lua"`
12luaroot: []const u8,
13// root for static assets, defaults to `root + "static"`
14staticroot: []const u8,
15// maivi runtime path
16runtime: []const u8,
17
18log_level: fern.Level = .info,
19base_url: []const u8,
20port: u16,
21
22pub fn init(io: std.Io, arena: *std.heap.ArenaAllocator, environ_map: *std.process.Environ.Map) !Self {
23    const allocator = arena.allocator();
24
25    const cwd = try std.Io.Dir.cwd().realPathFileAlloc(io, ".", allocator);
26
27    const port_env = if (environ_map.get("MAIVI_PORT")) |p| try std.fmt.parseInt(u16, p, 10) else null;
28    const port = port_env orelse 5882;
29    const default_base_url = try std.fmt.allocPrint(allocator, "http://127.0.0.1:{d}", .{port});
30
31    const root = environ_map.get("MAIVI_ROOT") orelse cwd;
32    const default_buildroot = try std.mem.join(allocator, "/", &.{ root, "build" });
33    const default_luaroot = try std.mem.join(allocator, "/", &.{ root, "lua" });
34    const default_staticroot = try std.mem.join(allocator, "/", &.{ root, "static" });
35
36    return .{
37        .root = root,
38        .buildroot = environ_map.get("MAIVI_BUILD") orelse default_buildroot,
39        .luaroot = environ_map.get("MAIVI_LUA") orelse default_luaroot,
40        .staticroot = environ_map.get("MAIVI_STATIC") orelse default_staticroot,
41        .runtime = environ_map.get("MAIVI_RUNTIME") orelse options.runtimepath,
42        .log_level = if (environ_map.get("MAIVI_LOG")) |env|
43            if (std.mem.eql(u8, env, "fatal"))
44                .fatal
45            else if (std.mem.eql(u8, env, "error"))
46                .err
47            else if (std.mem.eql(u8, env, "warn"))
48                .warn
49            else if (std.mem.eql(u8, env, "info"))
50                .info
51            else if (std.mem.eql(u8, env, "debug"))
52                .debug
53            else if (std.mem.eql(u8, env, "off"))
54                .off
55            else
56                .info
57        else
58            .info,
59        .base_url = environ_map.get("MAIVI_BASE_URL") orelse default_base_url,
60        .port = port,
61    };
62}