summaryrefslogtreecommitdiff
path: root/src/cgi.zig
blob: 849ef8ceee517d3dc3eab0fab98bc7df0406c71e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const std = @import("std");
const Io = std.Io;
const File = Io.File;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const ArrayList = std.ArrayList;
const StringArrayHashMap = std.StringArrayHashMapUnmanaged;

const HTMLua = @import("htmlua");

const Config = struct {
    component_path: []const u8,
    template_path: []const u8,
};

var component_dir: Io.Dir = undefined;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var arena = std.heap.ArenaAllocator.init(init.gpa);
    defer arena.deinit();
    const alloc = arena.allocator();

    var args = init.minimal.args.iterate();

    const cwd = std.Io.Dir.cwd();

    if (!args.skip()) unreachable;
    const config_file_path = args.next().?;
    const config_buf = try cwd.readFileAllocOptions(io, config_file_path, alloc, .unlimited, std.mem.Alignment.@"1", '\x00');
    const config = try std.zon.parse.fromSliceAlloc(
        Config,
        alloc,
        config_buf,
        null,
        .{},
    );

    component_dir = try cwd.openDir(io, config.component_path, .{});
    const template_dir = try cwd.openDir(io, config.template_path, .{});

    var state: HTMLua.Parser = .{
        .tags = &[_]HTMLua.ParserTag{
            HTMLua.tags.Include,
            HTMLua.tags.Export,
            HTMLua.tags.Use,
            HTMLua.tags.Syntax,
        },
        .tag_stack = try ArrayList([]const u8).initCapacity(alloc, 256),
        .exported_parts = try StringArrayHashMap([]const u8).init(alloc, &[0][]const u8{}, &[0][]const u8{}),
        .component_resolver = struct {
            pub fn resolve(arena_inner: *ArenaAllocator, io_inner: Io, path: []const u8) anyerror!*Io.Reader {
                const read_buff = try arena_inner.allocator().alloc(u8, 16384);
                const clean_path = try HTMLua.utils.sanitizePath(arena_inner.allocator(), path);
                var file = try component_dir.openFile(io_inner, clean_path, .{ .mode = .read_only });
                const reader = try arena_inner.allocator().create(File.Reader);
                reader.* = file.reader(io_inner, read_buff);
                return &reader.interface;
            }
        }.resolve,
    };

    const request_path = init.environ_map.get("PATH_INFO").?;
    const clean_request_path = try HTMLua.utils.sanitizePath(alloc, request_path);
    const file = try template_dir.openFile(io, clean_request_path, .{ .mode = .read_only });
    defer file.close(io);

    const in_buf = try alloc.alloc(u8, 16384);
    var in = file.reader(io, in_buf);

    const out_buf = try alloc.alloc(u8, 16384);
    var out = File.stdout().writer(io, out_buf);

    try out.interface.writeAll("Content-type: text/html\n");
    try HTMLua.parseTemplate(&arena, io, &state, &in.interface, &out.interface);

    try out.flush();
}