Update for static file serving

This commit is contained in:
2025-06-08 22:48:56 +09:00
parent 265e604c00
commit bd11364b65
2 changed files with 142 additions and 9 deletions

View File

@ -129,15 +129,33 @@ app.get("/podcast.xml", async (c) => {
}
});
// Category-specific RSS feeds
// Category-specific RSS feeds - try static file first, then generate dynamically
app.get("/podcast/category/:category.xml", async (c) => {
try {
const category = decodeURIComponent(c.req.param("category") || "");
if (!category) {
return c.notFound();
}
const { generateCategoryRSS } = await import("./services/podcast.js");
// Try to serve static file first
const safeCategory = encodeURIComponent(category);
const staticFilePath = path.join(
config.paths.publicDir,
`podcast_category_${safeCategory}.xml`,
);
const staticFile = Bun.file(staticFilePath);
if (await staticFile.exists()) {
const blob = await staticFile.arrayBuffer();
return c.body(blob, 200, {
"Content-Type": "application/xml; charset=utf-8",
"Cache-Control": "public, max-age=3600", // Cache for 1 hour
});
}
// Fallback to dynamic generation
console.log(`📄 Static category RSS not found for "${category}", generating dynamically...`);
const { generateCategoryRSS } = await import("./services/podcast.js");
const rssXml = await generateCategoryRSS(category);
return c.body(rssXml, 200, {
@ -146,22 +164,39 @@ app.get("/podcast/category/:category.xml", async (c) => {
});
} catch (error) {
console.error(
`Error generating category RSS for "${c.req.param("category")}":`,
`Error serving category RSS for "${c.req.param("category")}":`,
error,
);
return c.notFound();
}
});
// Feed-specific RSS feeds
// Feed-specific RSS feeds - try static file first, then generate dynamically
app.get("/podcast/feed/:feedId.xml", async (c) => {
try {
const feedId = c.req.param("feedId");
if (!feedId) {
return c.notFound();
}
const { generateFeedRSS } = await import("./services/podcast.js");
// Try to serve static file first
const staticFilePath = path.join(
config.paths.publicDir,
`podcast_feed_${feedId}.xml`,
);
const staticFile = Bun.file(staticFilePath);
if (await staticFile.exists()) {
const blob = await staticFile.arrayBuffer();
return c.body(blob, 200, {
"Content-Type": "application/xml; charset=utf-8",
"Cache-Control": "public, max-age=3600", // Cache for 1 hour
});
}
// Fallback to dynamic generation
console.log(`📄 Static feed RSS not found for "${feedId}", generating dynamically...`);
const { generateFeedRSS } = await import("./services/podcast.js");
const rssXml = await generateFeedRSS(feedId);
return c.body(rssXml, 200, {
@ -170,7 +205,7 @@ app.get("/podcast/feed/:feedId.xml", async (c) => {
});
} catch (error) {
console.error(
`Error generating feed RSS for "${c.req.param("feedId")}":`,
`Error serving feed RSS for "${c.req.param("feedId")}":`,
error,
);
return c.notFound();