fediglam/src/main/controllers/web.zig

90 lines
2.5 KiB
Zig
Raw Normal View History

2022-11-18 04:12:49 +00:00
const std = @import("std");
2022-11-24 11:50:25 +00:00
const controllers = @import("../controllers.zig");
2022-11-18 04:12:49 +00:00
pub const routes = .{
2022-11-26 01:43:30 +00:00
//controllers.apiEndpoint(index),
//controllers.apiEndpoint(about),
//controllers.apiEndpoint(login),
//controllers.apiEndpoint(global_timeline),
//controllers.apiEndpoint(cluster.overview),
2022-11-18 04:12:49 +00:00
};
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.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);
}
};
2022-11-18 12:07:58 +00:00
const global_timeline = struct {
pub const path = "/timelines/global";
pub const method = .GET;
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
_ = req;
const timeline = try srv.globalTimeline(.{});
try res.template(.ok, @embedFile("./web/timelines/global.tmpl.html"), .{
.notes = timeline.items,
.community = srv.community,
});
}
};
2022-11-19 11:13:05 +00:00
const cluster = struct {
const overview = struct {
pub const path = "/cluster/overview";
pub const method = .GET;
pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
const meta = try srv.getClusterMeta();
try res.template(.ok, @embedFile("./web/cluster/overview.tmpl.html"), .{
.community = srv.community,
.meta = meta,
});
}
};
};