Add new pages
This commit is contained in:
parent
0e034a2064
commit
b68dcc8fbf
|
@ -0,0 +1,58 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub const routes = .{
|
||||
index,
|
||||
about,
|
||||
login,
|
||||
};
|
||||
|
||||
const index = struct {
|
||||
pub const path = "/";
|
||||
pub const method = .GET;
|
||||
|
||||
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
|
||||
if (srv.user_id == null) {
|
||||
try res.headers.put("Location", about.path);
|
||||
return res.status(.see_other);
|
||||
}
|
||||
|
||||
try res.template(.ok, "Hello", .{});
|
||||
}
|
||||
};
|
||||
|
||||
const about = struct {
|
||||
pub const path = "/about";
|
||||
pub const method = .GET;
|
||||
|
||||
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
|
||||
try res.headers.put("Content-Type", "text/html");
|
||||
|
||||
try res.template(.ok, tmpl, .{
|
||||
.community = srv.community,
|
||||
});
|
||||
}
|
||||
|
||||
const tmpl = @embedFile("./web/index.tmpl.html");
|
||||
};
|
||||
|
||||
const login = struct {
|
||||
pub const path = "/login";
|
||||
pub const method = .POST;
|
||||
|
||||
pub const Body = struct {
|
||||
username: []const u8,
|
||||
password: []const u8,
|
||||
};
|
||||
|
||||
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
||||
const token = try srv.login(req.body.username, req.body.password);
|
||||
|
||||
try res.headers.put("Location", index.path);
|
||||
var buf: [64]u8 = undefined;
|
||||
const cookie_name = try std.fmt.bufPrint(&buf, "token.{s}", .{req.body.username});
|
||||
try res.headers.setCookie(cookie_name, token.token, .{});
|
||||
try res.headers.setCookie("active_account", req.body.username, .{ .HttpOnly = false });
|
||||
|
||||
try res.status(.see_other);
|
||||
}
|
||||
};
|
|
@ -1,19 +0,0 @@
|
|||
const template = @import("template");
|
||||
|
||||
pub const path = "/";
|
||||
pub const method = .GET;
|
||||
|
||||
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
|
||||
try res.headers.put("Content-Type", "text/html");
|
||||
|
||||
var stream = try res.open(.ok);
|
||||
defer stream.close();
|
||||
|
||||
try template.execute(stream.writer(), tmpl, .{
|
||||
.community = srv.community,
|
||||
});
|
||||
|
||||
try stream.finish();
|
||||
}
|
||||
|
||||
const tmpl = @embedFile("./index.tmpl.html");
|
Loading…
Reference in New Issue