Use templating system

This commit is contained in:
jaina heartles 2022-11-16 19:55:38 -08:00
parent 305c5f8c92
commit 432a11dbc9
4 changed files with 16 additions and 10 deletions

View file

@ -23,10 +23,15 @@ const api_pkg = std.build.Pkg{
.dependencies = &.{ util_pkg, sql_pkg },
};
const template_pkg = std.build.Pkg{
.name = "template",
.source = std.build.FileSource.relative("src/template/lib.zig"),
};
const main_pkg = std.build.Pkg{
.name = "main",
.source = std.build.FileSource.relative("src/main/main.zig"),
.dependencies = &.{ util_pkg, http_pkg, sql_pkg, api_pkg },
.dependencies = &.{ util_pkg, http_pkg, sql_pkg, api_pkg, template_pkg },
};
pub fn build(b: *std.build.Builder) void {
@ -48,6 +53,7 @@ pub fn build(b: *std.build.Builder) void {
exe.addPackage(util_pkg);
exe.addPackage(http_pkg);
exe.addPackage(api_pkg);
exe.addPackage(template_pkg);
exe.linkSystemLibrary("sqlite3");
exe.linkSystemLibrary("pq");

View file

@ -44,6 +44,8 @@ pub fn serveConn(conn: std.net.StreamServer.Connection, ctx: anytype, handler: a
.stream = conn.stream,
};
std.log.debug("{any}", .{req});
std.log.debug("Opening handler", .{});
handler(ctx, &req, &res);
std.log.debug("done handling", .{});

View file

@ -2,18 +2,17 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{[community_name]s}</title>
<title>{ .community.name }</title>
</head>
<body>
<header>
<h1>{[community_name]s}</h1>
Cluster Admin pseudocommunity
<h1>{ .community.name }</h1>
</header>
<form action="/login" method="post">
<h2>Login</h2>
<label>
Username
<div>@<input type="text" name="username" placeholder="xion" />@{[community_host]s}</div>
<div>@<input type="text" name="username" placeholder="xion" />@{.community.host}</div>
</label>
<label>
Password

View file

@ -1,4 +1,4 @@
const std = @import("std");
const template = @import("template");
pub const path = "/";
pub const method = .GET;
@ -9,12 +9,11 @@ pub fn handler(_: anytype, res: anytype, srv: anytype) !void {
var stream = try res.open(.ok);
defer stream.close();
try std.fmt.format(stream.writer(), template, .{
.community_name = srv.community.name,
.community_host = srv.community.host,
try template.execute(stream.writer(), tmpl, .{
.community = srv.community,
});
try stream.finish();
}
const template = @embedFile("./index.fmt.html");
const tmpl = @embedFile("./index.tmpl.html");