|
| 1 | +const std = @import("std"); |
| 2 | +const httpz = @import("httpz"); |
| 3 | +const pg = @import("pg"); |
| 4 | +const datetimez = @import("datetimez"); |
| 5 | +const mustache = @import("mustache"); |
| 6 | + |
| 7 | +const Allocator = std.mem.Allocator; |
| 8 | +const Thread = std.Thread; |
| 9 | +const Mutex = Thread.Mutex; |
| 10 | +const template = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>{{#fortunes}}<tr><td>{{id}}</td><td>{{message}}</td></tr>{{/fortunes}}</table></body></html>"; |
| 11 | + |
| 12 | +pub const Global = struct { |
| 13 | + pool: *pg.Pool, |
| 14 | + prng: *std.rand.DefaultPrng, |
| 15 | + allocator: Allocator, |
| 16 | + mutex: std.Thread.Mutex = .{}, |
| 17 | +}; |
| 18 | + |
| 19 | +const Message = struct { |
| 20 | + message: []const u8, |
| 21 | +}; |
| 22 | + |
| 23 | +const World = struct { |
| 24 | + id: i32, |
| 25 | + randomNumber: i32, |
| 26 | +}; |
| 27 | + |
| 28 | +const Fortune = struct { |
| 29 | + id: i32, |
| 30 | + message: []const u8, |
| 31 | +}; |
| 32 | + |
| 33 | +pub fn plaintext(global: *Global, _: *httpz.Request, res: *httpz.Response) !void { |
| 34 | + try setHeaders(global.allocator, res); |
| 35 | + |
| 36 | + res.content_type = .TEXT; |
| 37 | + res.body = "Hello, World!"; |
| 38 | +} |
| 39 | + |
| 40 | +pub fn json(global: *Global, _: *httpz.Request, res: *httpz.Response) !void { |
| 41 | + try setHeaders(global.allocator, res); |
| 42 | + |
| 43 | + const message = Message{ .message = "Hello, World!" }; |
| 44 | + |
| 45 | + try res.json(message, .{}); |
| 46 | +} |
| 47 | + |
| 48 | +pub fn db(global: *Global, _: *httpz.Request, res: *httpz.Response) !void { |
| 49 | + try setHeaders(global.allocator, res); |
| 50 | + |
| 51 | + global.mutex.lock(); |
| 52 | + const random_number = 1 + (global.prng.random().uintAtMost(u32, 9999)); |
| 53 | + global.mutex.unlock(); |
| 54 | + |
| 55 | + const world = getWorld(global.pool, random_number) catch |err| { |
| 56 | + std.debug.print("Error querying database: {}\n", .{err}); |
| 57 | + return; |
| 58 | + }; |
| 59 | + |
| 60 | + try res.json(world, .{}); |
| 61 | +} |
| 62 | + |
| 63 | +pub fn fortune(global: *Global, _: *httpz.Request, res: *httpz.Response) !void { |
| 64 | + try setHeaders(global.allocator, res); |
| 65 | + |
| 66 | + const fortunes_html = try getFortunesHtml(global.allocator, global.pool); |
| 67 | + |
| 68 | + res.header("content-type", "text/html; charset=utf-8"); |
| 69 | + res.body = fortunes_html; |
| 70 | +} |
| 71 | + |
| 72 | +fn getWorld(pool: *pg.Pool, random_number: u32) !World{ |
| 73 | + var conn = try pool.acquire(); |
| 74 | + defer conn.release(); |
| 75 | + |
| 76 | + const row_result = try conn.row("SELECT id, randomNumber FROM World WHERE id = $1", .{random_number}); |
| 77 | + |
| 78 | + var row = row_result.?; |
| 79 | + defer row.deinit() catch {}; |
| 80 | + |
| 81 | + return World{ .id = row.get(i32, 0), .randomNumber = row.get(i32, 1) }; |
| 82 | +} |
| 83 | + |
| 84 | +fn setHeaders(allocator: Allocator, res: *httpz.Response) !void { |
| 85 | + res.header("Server", "Httpz"); |
| 86 | + |
| 87 | + const now = datetimez.datetime.Date.now(); |
| 88 | + const time = datetimez.datetime.Time.now(); |
| 89 | + |
| 90 | + // Wed, 17 Apr 2013 12:00:00 GMT |
| 91 | + // Return date in ISO format YYYY-MM-DD |
| 92 | + const TB_DATE_FMT = "{s:0>3}, {d:0>2} {s:0>3} {d:0>4} {d:0>2}:{d:0>2}:{d:0>2} GMT"; |
| 93 | + const now_str = try std.fmt.allocPrint(allocator, TB_DATE_FMT, .{ now.weekdayName()[0..3], now.day, now.monthName()[0..3], now.year, time.hour, time.minute, time.second }); |
| 94 | + |
| 95 | + //defer allocator.free(now_str); |
| 96 | + |
| 97 | + res.header("Date", now_str); |
| 98 | +} |
| 99 | + |
| 100 | +fn getFortunesHtml(allocator: Allocator, pool: *pg.Pool) ![]const u8 { |
| 101 | + const fortunes = try getFortunes(allocator, pool); |
| 102 | + |
| 103 | + const raw = try mustache.allocRenderText(allocator, template,.{ .fortunes = fortunes }); |
| 104 | + |
| 105 | + // std.debug.print("mustache output {s}\n", .{raw}); |
| 106 | + |
| 107 | + const html = try deescapeHtml(allocator, raw); |
| 108 | + |
| 109 | + // std.debug.print("html output {s}\n", .{html}); |
| 110 | + |
| 111 | + return html; |
| 112 | +} |
| 113 | + |
| 114 | +fn getFortunes(allocator: Allocator, pool: *pg.Pool) ![]const Fortune { |
| 115 | + var conn = try pool.acquire(); |
| 116 | + defer conn.release(); |
| 117 | + |
| 118 | + var rows = try conn.query("SELECT id, message FROM Fortune", .{}); |
| 119 | + defer rows.deinit(); |
| 120 | + |
| 121 | + var fortunes = std.ArrayList(Fortune).init(allocator); |
| 122 | + defer fortunes.deinit(); |
| 123 | + |
| 124 | + while (try rows.next()) |row| { |
| 125 | + const current_fortune = Fortune{ .id = row.get(i32, 0), .message = row.get([]const u8, 1) }; |
| 126 | + try fortunes.append(current_fortune); |
| 127 | + } |
| 128 | + |
| 129 | + const zero_fortune = Fortune{ .id = 0, .message = "Additional fortune added at request time." }; |
| 130 | + try fortunes.append(zero_fortune); |
| 131 | + |
| 132 | + const fortunes_slice = try fortunes.toOwnedSlice(); |
| 133 | + std.mem.sort(Fortune, fortunes_slice, {}, cmpFortuneByMessage); |
| 134 | + |
| 135 | + return fortunes_slice; |
| 136 | +} |
| 137 | + |
| 138 | +fn cmpFortuneByMessage(_: void, a: Fortune, b: Fortune) bool { |
| 139 | + return std.mem.order(u8, a.message, b.message).compare(std.math.CompareOperator.lt); |
| 140 | +} |
| 141 | + |
| 142 | +fn deescapeHtml(allocator: Allocator, input: []const u8) ![]const u8 { |
| 143 | + var output = std.ArrayList(u8).init(allocator); |
| 144 | + defer output.deinit(); |
| 145 | + |
| 146 | + var i: usize = 0; |
| 147 | + while (i < input.len) { |
| 148 | + if (std.mem.startsWith(u8, input[i..], " ")) { |
| 149 | + try output.append(' '); |
| 150 | + i += 5; |
| 151 | + } else if (std.mem.startsWith(u8, input[i..], """)) { |
| 152 | + try output.append('"'); |
| 153 | + i += 5; |
| 154 | + } else if (std.mem.startsWith(u8, input[i..], "&")) { |
| 155 | + try output.append('&'); |
| 156 | + i += 5; |
| 157 | + } else if (std.mem.startsWith(u8, input[i..], "'")) { |
| 158 | + try output.append('\''); |
| 159 | + i += 5; |
| 160 | + } else if (std.mem.startsWith(u8, input[i..], "(")) { |
| 161 | + try output.append('('); |
| 162 | + i += 5; |
| 163 | + } else if (std.mem.startsWith(u8, input[i..], ")")) { |
| 164 | + try output.append(')'); |
| 165 | + i += 5; |
| 166 | + } else if (std.mem.startsWith(u8, input[i..], "+")) { |
| 167 | + try output.append('+'); |
| 168 | + i += 5; |
| 169 | + } else if (std.mem.startsWith(u8, input[i..], ",")) { |
| 170 | + try output.append(','); |
| 171 | + i += 5; |
| 172 | + } else if (std.mem.startsWith(u8, input[i..], ".")) { |
| 173 | + try output.append('.'); |
| 174 | + i += 5; |
| 175 | + } else if (std.mem.startsWith(u8, input[i..], "/")) { |
| 176 | + try output.append('/'); |
| 177 | + i += 5; |
| 178 | + } else if (std.mem.startsWith(u8, input[i..], ":")) { |
| 179 | + try output.append(':'); |
| 180 | + i += 5; |
| 181 | + } else if (std.mem.startsWith(u8, input[i..], ";")) { |
| 182 | + try output.append(';'); |
| 183 | + i += 5; |
| 184 | + } else { |
| 185 | + try output.append(input[i]); |
| 186 | + i += 1; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return output.toOwnedSlice(); |
| 191 | +} |
| 192 | + |
0 commit comments