- Cache read-heavy public API routes via redisCache (leaderboard, values, shop, articles, photos, guilds, teams, staff, users, home, radio, badges) - Add single-flight and bounded-memory cache layer with unit tests - Parallelize independent DB queries on search, rares, shop, staff, polls and profile pages - Push radio points leaderboard aggregation to SQL with a LIMIT - Split studio-client and import-furni-client into focused modules - Clean up next.config.ts
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { apiJson } from "@/lib/api";
|
|
import { db, WebsiteArticles } from "@/lib/db";
|
|
import { apiCacheKey, cacheSafe, redisCache } from "@/lib/redis-cache";
|
|
|
|
/**
|
|
* GET /api/articles/:slug — single website_article by slug, including the
|
|
* fullStory body. Returns { error } (404) when the slug is unknown.
|
|
*/
|
|
export async function GET(
|
|
_req: Request,
|
|
{ params }: { params: Promise<{ slug: string }> },
|
|
) {
|
|
const { slug } = await params;
|
|
|
|
try {
|
|
const data = await redisCache(
|
|
apiCacheKey(`articles:${slug}`),
|
|
60,
|
|
async () => {
|
|
const [article] = await db
|
|
.select({
|
|
id: WebsiteArticles.id,
|
|
title: WebsiteArticles.title,
|
|
slug: WebsiteArticles.slug,
|
|
shortStory: WebsiteArticles.shortStory,
|
|
fullStory: WebsiteArticles.fullStory,
|
|
image: WebsiteArticles.image,
|
|
createdAt: WebsiteArticles.createdAt,
|
|
updatedAt: WebsiteArticles.updatedAt,
|
|
})
|
|
.from(WebsiteArticles)
|
|
.where(eq(WebsiteArticles.slug, slug))
|
|
.limit(1);
|
|
|
|
if (!article) {
|
|
return null;
|
|
}
|
|
|
|
return cacheSafe({ data: article });
|
|
},
|
|
);
|
|
|
|
if (data === null) {
|
|
return apiJson({ error: "Article not found" }, { status: 404 });
|
|
}
|
|
|
|
return apiJson(data);
|
|
} catch {
|
|
// DB unavailable — treat as not found rather than a 500.
|
|
return apiJson({ error: "Article not found" }, { status: 200 });
|
|
}
|
|
}
|