Files
podsteadr/server/src/routes/feeds.ts
T

29 lines
1.2 KiB
TypeScript
Raw Normal View History

import { createHash } from 'node:crypto';
import type { FastifyInstance } from 'fastify';
import { buildFeedXml } from '../services/rss.js';
import type { Episode, Podcast } from '../types.js';
export default async function feedRoutes(app: FastifyInstance) {
const { db, settings } = app.ctx;
const getPodcast = db.prepare('SELECT * FROM podcasts WHERE id = ?');
const listEpisodes = db.prepare('SELECT * FROM episodes WHERE podcast_id = ? ORDER BY pub_date DESC');
app.get('/feeds/:id/feed.xml', async (req, reply) => {
const { id } = req.params as { id: string };
const podcast = getPodcast.get(id) as Podcast | undefined;
if (!podcast) return reply.code(404).send({ error: 'feed not found' });
const episodes = listEpisodes.all(id) as Episode[];
const xml = buildFeedXml(podcast, episodes, { publicUrl: settings.all().public_url });
const etag = `"${createHash('sha256').update(xml).digest('hex').slice(0, 16)}"`;
if (req.headers['if-none-match'] === etag) return reply.code(304).send();
return reply
.header('content-type', 'application/rss+xml; charset=utf-8')
.header('etag', etag)
.header('cache-control', 'public, max-age=60')
.send(xml);
});
}