- 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
60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import { count, desc } from "drizzle-orm";
|
|
import { apiJson, pagination } from "@/lib/api";
|
|
import { db, WebsiteArticles } from "@/lib/db";
|
|
import { apiCacheKey, cacheSafe, redisCache } from "@/lib/redis-cache";
|
|
|
|
/**
|
|
* GET /api/articles — paginated list of website_articles, newest first.
|
|
* Mirrors the /news page query. Returns list cards (shortStory only, never
|
|
* fullStory) plus pagination metadata.
|
|
*/
|
|
export async function GET(req: Request) {
|
|
const sp = new URL(req.url).searchParams;
|
|
const { page, perPage, skip, take } = pagination(sp);
|
|
|
|
try {
|
|
const data = await redisCache(
|
|
apiCacheKey(`articles:${page}:${perPage}`),
|
|
60,
|
|
async () => {
|
|
const [totalRows, articles] = await Promise.all([
|
|
db.select({ total: count() }).from(WebsiteArticles),
|
|
db
|
|
.select({
|
|
id: WebsiteArticles.id,
|
|
title: WebsiteArticles.title,
|
|
slug: WebsiteArticles.slug,
|
|
shortStory: WebsiteArticles.shortStory,
|
|
image: WebsiteArticles.image,
|
|
createdAt: WebsiteArticles.createdAt,
|
|
})
|
|
.from(WebsiteArticles)
|
|
.orderBy(desc(WebsiteArticles.createdAt))
|
|
.limit(take)
|
|
.offset(skip),
|
|
]);
|
|
return cacheSafe({
|
|
data: articles,
|
|
meta: {
|
|
page,
|
|
perPage,
|
|
total: totalRows[0]?.total ?? 0,
|
|
lastPage: Math.max(
|
|
1,
|
|
Math.ceil((totalRows[0]?.total ?? 0) / perPage),
|
|
),
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
return apiJson(data);
|
|
} catch {
|
|
// DB unavailable — return an empty payload instead of a 500.
|
|
return apiJson(
|
|
{ data: [], meta: { page, perPage, total: 0, lastPage: 1 } },
|
|
{ status: 200 },
|
|
);
|
|
}
|
|
}
|