2026-01-24 23:53:19 +05:30
|
|
|
//! Pugz Demo - ViewEngine Template Rendering
|
2026-01-22 11:10:47 +05:30
|
|
|
//!
|
2026-01-24 23:53:19 +05:30
|
|
|
//! This demo shows how to use ViewEngine for server-side rendering.
|
2026-01-22 11:10:47 +05:30
|
|
|
//!
|
|
|
|
|
//! Routes:
|
2026-01-24 23:53:19 +05:30
|
|
|
//! GET / - Home page
|
|
|
|
|
//! GET /users - Users list
|
|
|
|
|
//! GET /page-a - Page with data
|
2026-01-22 11:10:47 +05:30
|
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
const httpz = @import("httpz");
|
|
|
|
|
const pugz = @import("pugz");
|
|
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
|
|
|
|
|
|
/// Application state shared across all requests
|
|
|
|
|
const App = struct {
|
|
|
|
|
allocator: Allocator,
|
|
|
|
|
view: pugz.ViewEngine,
|
|
|
|
|
|
|
|
|
|
pub fn init(allocator: Allocator) App {
|
|
|
|
|
return .{
|
|
|
|
|
.allocator = allocator,
|
|
|
|
|
.view = pugz.ViewEngine.init(.{
|
|
|
|
|
.views_dir = "views",
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
|
defer if (gpa.deinit() == .leak) @panic("leak");
|
|
|
|
|
|
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
|
|
|
|
|
|
var app = App.init(allocator);
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
const port = 8081;
|
2026-01-22 11:10:47 +05:30
|
|
|
var server = try httpz.Server(*App).init(allocator, .{ .port = port }, &app);
|
|
|
|
|
defer server.deinit();
|
|
|
|
|
|
|
|
|
|
var router = try server.router(.{});
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
router.get("/", index, .{});
|
|
|
|
|
router.get("/users", users, .{});
|
2026-01-22 11:10:47 +05:30
|
|
|
router.get("/page-a", pageA, .{});
|
2026-01-24 23:53:19 +05:30
|
|
|
router.get("/mixin-test", mixinTest, .{});
|
2026-01-22 11:10:47 +05:30
|
|
|
|
|
|
|
|
std.debug.print(
|
|
|
|
|
\\
|
2026-01-24 23:53:19 +05:30
|
|
|
\\Pugz Demo - ViewEngine Template Rendering
|
|
|
|
|
\\==========================================
|
2026-01-22 11:10:47 +05:30
|
|
|
\\Server running at http://localhost:{d}
|
|
|
|
|
\\
|
2026-01-24 23:53:19 +05:30
|
|
|
\\Routes:
|
|
|
|
|
\\ GET / - Home page
|
|
|
|
|
\\ GET /users - Users list
|
|
|
|
|
\\ GET /page-a - Page with data
|
|
|
|
|
\\ GET /mixin-test - Mixin test page
|
2026-01-22 11:10:47 +05:30
|
|
|
\\
|
|
|
|
|
\\Press Ctrl+C to stop.
|
|
|
|
|
\\
|
|
|
|
|
, .{port});
|
|
|
|
|
|
|
|
|
|
try server.listen();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
/// GET / - Home page
|
|
|
|
|
fn index(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
|
|
|
|
const html = app.view.render(res.arena, "index", .{
|
|
|
|
|
.title = "Welcome",
|
2026-01-22 11:10:47 +05:30
|
|
|
.authenticated = true,
|
|
|
|
|
}) catch |err| {
|
|
|
|
|
res.status = 500;
|
|
|
|
|
res.body = @errorName(err);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.content_type = .HTML;
|
|
|
|
|
res.body = html;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
/// GET /users - Users list
|
|
|
|
|
fn users(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
|
|
|
|
const html = app.view.render(res.arena, "users", .{
|
|
|
|
|
.title = "Users",
|
2026-01-22 11:10:47 +05:30
|
|
|
}) catch |err| {
|
|
|
|
|
res.status = 500;
|
|
|
|
|
res.body = @errorName(err);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.content_type = .HTML;
|
|
|
|
|
res.body = html;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
/// GET /page-a - Page with data
|
|
|
|
|
fn pageA(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
|
|
|
|
const html = app.view.render(res.arena, "page-a", .{
|
|
|
|
|
.title = "Page A - Pets",
|
|
|
|
|
.items = &[_][]const u8{ "A", "B", "C" },
|
|
|
|
|
.n = 0,
|
2026-01-22 11:10:47 +05:30
|
|
|
}) catch |err| {
|
|
|
|
|
res.status = 500;
|
|
|
|
|
res.body = @errorName(err);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.content_type = .HTML;
|
|
|
|
|
res.body = html;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 23:53:19 +05:30
|
|
|
/// GET /mixin-test - Mixin test page
|
|
|
|
|
fn mixinTest(app: *App, _: *httpz.Request, res: *httpz.Response) !void {
|
|
|
|
|
const html = app.view.render(res.arena, "mixin-test", .{}) catch |err| {
|
2026-01-22 11:10:47 +05:30
|
|
|
res.status = 500;
|
|
|
|
|
res.body = @errorName(err);
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.content_type = .HTML;
|
|
|
|
|
res.body = html;
|
|
|
|
|
}
|