73 lines
2.0 KiB
Zig
73 lines
2.0 KiB
Zig
const std = @import("std");
|
|
const jetzig = @import("jetzig");
|
|
|
|
const RssItem = struct {
|
|
id: i32,
|
|
title: []const u8,
|
|
blob: []const u8,
|
|
content: []const u8,
|
|
created_at: jetzig.jetquery.DateTime,
|
|
updated_at: jetzig.jetquery.DateTime,
|
|
};
|
|
|
|
fn generateRss(items: []const RssItem, allocator: std.mem.Allocator) ![]u8 {
|
|
var list = std.ArrayList(u8).init(allocator);
|
|
const writer = list.writer();
|
|
|
|
try writer.print(
|
|
\\<?xml version="1.0" encoding="UTF-8"?>
|
|
\\<rss version="2.0">
|
|
\\<channel>
|
|
\\<title>yuzucchii.xyz</title>
|
|
\\<link>yuzucchii.xyz</link>
|
|
\\<description>Personal blog of Yuzu with all kinds of different articles</description>
|
|
, .{});
|
|
|
|
for (items) |item| {
|
|
try writer.print(
|
|
\\<item>
|
|
\\<title>{s}</title>
|
|
\\<link>yuzucchii.xyz/blogs/{d}</link>
|
|
\\<description>{s}</description>
|
|
, .{ item.title, item.id, item.blob });
|
|
|
|
try item.created_at.strftime(writer, "<pubDate>Day, DD Mon YYYY HH:MM:SS GMT</pubDate>");
|
|
|
|
try writer.print(
|
|
\\</item>
|
|
, .{});
|
|
}
|
|
|
|
try writer.writeAll("</channel></rss>");
|
|
return list.toOwnedSlice();
|
|
}
|
|
|
|
|
|
// we'll send xml instead
|
|
pub fn index(request: *jetzig.Request) !void {
|
|
try request.headers.append("Content-Type", "application/rss+xml");
|
|
|
|
const query = jetzig.database.Query(.Blog).orderBy(.{.created_at = .desc});
|
|
|
|
const blogs = try request.repo.all(query);
|
|
|
|
var items: std.ArrayList(RssItem) = .init(request.allocator);
|
|
|
|
for (blogs) |blog| {
|
|
try items.append(RssItem{
|
|
.id = blog.id,
|
|
.title = blog.title,
|
|
.blob = blog.blob,
|
|
.content = blog.content orelse "<empty>",
|
|
.created_at = blog.created_at,
|
|
.updated_at = blog.updated_at,
|
|
});
|
|
}
|
|
|
|
// TODO: wait until jetzig adds xml support
|
|
_ = try generateRss(try items.toOwnedSlice(), request.allocator);
|
|
|
|
return request.render(.ok);
|
|
}
|
|
|