aboutsummaryrefslogtreecommitdiff
path: root/comments/src/main.zig
blob: 4044ae7b231f07aeb30c6031ff368af21d51d4cd (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const std = @import("std");
const zws = @import("zws");
const pq = @import("pq");
const mustache = @import("mustache");
const smtp = @import("smtp");

const Params = zws.Params;

const Err = error{
    AccessDenied,
    BrokenPipe,
    ColumnNotFound,
    ConnectionResetByPeer,
    ConnectionTimedOut,
    DeviceBusy,
    DiskQuota,
    FileTooBig,
    InputOutput,
    InvalidArgument,
    InvalidCharacter,
    InvalidLength,
    InvalidRequestMethod,
    IsDir,
    LockViolation,
    Malformatted,
    NetNameDeleted,
    NoSpaceLeft,
    NotOpenForReading,
    NotOpenForWriting,
    OperationAborted,
    OutOfMemory,
    PqError,
    SocketNotConnected,
    StreamTooLong,
    SystemResources,
    Unexpected,
    WouldBlock,
    Overflow,
    EndOfStream,
};
const Ctx = struct {
    db: pq.Db,
    pub fn clone(self: @This()) Ctx {
        return Ctx{
            .db = self.db,
        };
    }
    pub fn deinit(self: @This()) void {
        _ = self;
    }
};

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const Request = struct {
    method: std.http.Method,
    target: []const u8,
};
const ResponseTransfer = union(enum) {
    content_length: u64,
    chunked: void,
    none: void,
};
const Headers = struct {
    const Entry = struct { key: []const u8, val: []const u8 };
    _internal: std.ArrayList(Entry),

    fn init(allocator: std.mem.Allocator) Headers {
        return .{ ._internal = std.ArrayList(Entry).init(allocator) };
    }
    fn append(self: *@This(), key: []const u8, val: []const u8) !void {
        try self._internal.append(.{ .key = key, .val = val });
    }
};
const Response = struct {
    allocator: std.mem.Allocator,
    request: Request,
    // TODO other fields and writer function to write headers and body to stdout
    status: std.http.Status,
    transfer_encoding: ResponseTransfer,
    headers: Headers,
    fn reader(_: @This()) std.fs.File.Reader {
        return std.io.getStdIn().reader();
    }
    fn do(self: @This()) !void {
        const wtr = std.io.getStdOut().writer();
        if (self.status.phrase()) |phrase| {
            try std.fmt.format(wtr, "status: {} {s}\r\n", .{@intFromEnum(self.status), phrase});
        } else {
            try std.fmt.format(wtr, "status: {}\r\n", .{@intFromEnum(self.status)});
        }
        for (self.headers._internal.items) |tup| {
            try wtr.writeAll(tup.key);
            try wtr.writeAll(": ");
            try wtr.writeAll(tup.val);
            try wtr.writeAll("\r\n");
        }
        try wtr.writeAll("\r\n");
    }
    fn writer(_: @This()) std.fs.File.Writer {
        return std.io.getStdOut().writer();
    }
    fn finish(_: @This()) !void {
        // TODO Write empty lines? or just do nothing
    }
};

const Rtr = zws.Router(*Response, Ctx, Err);
const router = Rtr{
    .allocator = gpa.allocator(),
    .handlers = &[_]Rtr.Handler{
        .{
            .method = .GET,
            .pattern = "/api/comment",
            .handle_fn = get_comment,
        },
        .{
            .method = .POST,
            .pattern = "/api/comment",
            .handle_fn = post_comment,
        },
        .{
            .method = .GET,
            .pattern = "/api/form",
            .handle_fn = get_form,
        },
    },
    .notfound = notfound,
};

/// Run as a CGI program!
pub fn main() !void {
    const allocator = gpa.allocator();
    const db_url = std.os.getenv("DATABASE_URL") orelse "postgresql://comments@localhost/comments";
    var db = try pq.Db.init(db_url);
    // try db.exec(@embedFile("migrations/0_init.sql"));
    // try db.exec(@embedFile("migrations/1_capcha.sql"));
    defer db.deinit();
    const req = Request{
        .method = std.meta.stringToEnum(std.http.Method, std.os.getenv("REQUEST_METHOD") orelse "GET") orelse {
            return error.InvalidRequestMethod;
        },
        .target = std.os.getenv("REQUEST_URI") orelse "/",
    };
    var res = Response{
        .allocator = allocator,
        .request = req,
        .status = .ok,
        .transfer_encoding = .none,
        .headers = Headers.init(allocator),
    };
    const ctx = Ctx{ .db = db };
    try router.handle(&res, ctx);
}

fn notfound(res: *Response, _: Ctx) Err!void {
    const rr = @embedFile("templates/notfound.html");
    try constresponse(res, rr, .not_found);
}

fn badrequest(res: *Response, _: Ctx) Err!void {
    const rr = @embedFile("templates/badrequest.html");
    try constresponse(res, rr, .bad_request);
}

fn constresponse(res: *Response, rr: []const u8, status: std.http.Status) Err!void {
    res.status = status;
    res.transfer_encoding = .{ .content_length = rr.len };
    try res.headers.append("content-type", "text/html");
    try res.do();
    try res.writer().writeAll(rr);
    try res.finish();
}

fn get_comment(res: *Response, ctx: Ctx, _: Params) Err!void {
    var p = try zws.Path.parse(res.allocator, res.request.target);
    defer p.deinit();
    const url: []const u8 = try p.get_query_param("url") orelse {
        try badrequest(res, ctx);
        return;
    };

    const Comment = struct {
        author: []const u8,
        comment: []const u8,
        ts: []const u8,
    };
    var comments = std.ArrayList(Comment).init(res.allocator);
    var stmt = try ctx.db.prepare_statement(res.allocator,
        \\ select author,comment,ts from comments where url = $1 order by ts
    );
    defer stmt.deinit();
    try stmt.bind(0, url);
    while (try stmt.step()) {
        const cmt = try stmt.read_struct(Comment);
        try comments.append(cmt);
    }

    const rr = @embedFile("templates/comments.html");
    const tt = comptime mustache.parseComptime(rr, .{}, .{});
    res.transfer_encoding = .chunked;
    try res.headers.append("content-type", "text/html");
    try res.do();

    const data = struct {
        comments: []const Comment,
    };
    try mustache.render(tt, data{
        .comments = comments.items,
    }, res.writer());
    try res.finish();
}

fn post_comment(res: *Response, ctx: Ctx, _: Params) Err!void {
    const cl = if (std.os.getenv("CONTENT_LENGTH")) |clh| try std.fmt.parseInt(usize, clh, 10) else {
        return error.InvalidLength;
    };
    const body = try res.allocator.alloc(u8, cl);
    defer res.allocator.free(body);
    try res.reader().readNoEof(body);
    var form = try zws.Form.parse(res.allocator, body);
    const Form = struct {
        url: []const u8,
        capcha_id: []const u8,
        author: []const u8,
        comment: []const u8,
        capcha_answer: []const u8,
    };
    const form_val = form.form_to_struct(Form) catch {
        std.log.err("couldn't parse Form", .{});
        try badrequest(res, ctx);
        return;
    };

    // Validate the capcha
    {
        var stmt = try ctx.db.prepare_statement(res.allocator, "select answer from capchas where id = $1");
        defer stmt.deinit();
        try stmt.bind(0, form_val.capcha_id);
        if (!try stmt.step()) {
            std.log.err("missing capcha_id {s}", .{form_val.capcha_id});
            try badrequest(res, ctx);
            return;
        }
        const ans = try stmt.read_column(0, []const u8);
        if (!std.mem.eql(u8, ans, form_val.capcha_answer)) {
            std.log.err("bad capcha answer {s} expected {s}", .{form_val.capcha_answer, ans});
            try constresponse(res, @embedFile("templates/capchainvalid.html"), std.http.Status.unauthorized);
            return;
        }
    }

    // Add the comment...
    {
        var stmt = try ctx.db.prepare_statement(res.allocator, "insert into comments(url,author,comment) values($1, $2, $3)");
        defer stmt.deinit();
        try stmt.bind(0, form_val.url);
        try stmt.bind(1, form_val.author);
        try stmt.bind(2, form_val.comment);
        _ = try stmt.step();
    }

    // Send me an email
    const rr = @embedFile("templates/notification.txt");
    const tt = comptime mustache.parseComptime(rr, .{}, .{});
    const Data = struct { url: []const u8, author: []const u8, comment: []const u8 };
    const notification = try mustache.allocRender(res.allocator, tt, Data{
        .url = form_val.url,
        .author = form_val.author,
        .comment = form_val.comment,
    });
    const smtp_username = std.os.getenv("SMTP_USERNAME") orelse "comments@mfashby.net";
    const smtp_password = std.os.getenv("SMTP_PASSWORD") orelse "foobar";
    const notification_address = std.os.getenv("NOTIFICATION_ADDRESS") orelse "martin@mfashby.net";
    const smtp_server = std.os.getenv("SMTP_SERVER") orelse "mail.mfashby.net:587";
    smtp.send_mail(res.allocator, smtp_server, .{ .user = smtp_username, .pass = smtp_password }, smtp_username, &[_][]const u8{notification_address}, notification) catch |err| {
        std.log.err("failed to send notification email {}", .{err});
    };

    // And redirect!
    res.transfer_encoding = .none;
    res.status = .found;
    try res.headers.append("location", form_val.url);
    try res.do();
    try res.finish();
}

fn get_form(res: *Response, ctx: Ctx, _: Params) Err!void {
    var p = try zws.Path.parse(res.allocator, res.request.target);
    defer p.deinit();
    const url: []const u8 = try p.get_query_param("url") orelse {
        try badrequest(res, ctx);
        return;
    };

    var stmt = try ctx.db.prepare_statement(res.allocator, "select id, question from capchas order by random() limit 1");
    defer stmt.deinit();
    if (!try stmt.step()) {
        std.log.err("no capcha!", .{});
        try badrequest(res, ctx);
        return;
    }
    const Capcha = struct {
        id: []const u8,
        question: []const u8,
    };
    const capcha = try stmt.read_struct(Capcha);

    const rr = @embedFile("templates/form.html");
    const tt = comptime mustache.parseComptime(rr, .{}, .{});

    res.transfer_encoding = .chunked;
    try res.headers.append("content-type", "text/html");
    try res.do();
    // For some reason, mustache.render won't work with anonymous struct
    const data = struct {
        capcha_id: []const u8,
        capcha_question: []const u8,
        url: []const u8,
    };
    try mustache.render(tt, data{
        .capcha_id = capcha.id,
        .capcha_question = capcha.question,
        .url = url,
    }, res.writer());
    try res.finish();
}