feat: add GET /api/v1/tools/popular endpoint

This commit is contained in:
SnapOtter
2026-06-14 18:36:32 +08:00
parent 6c61ae47f4
commit 110700520c
2 changed files with 37 additions and 0 deletions
+4
View File
@@ -94,6 +94,7 @@ import { registerOcrPdf } from "./ocr-pdf.js";
import { registerOptimizeForWeb } from "./optimize-for-web.js";
import { registerOrganizePdf } from "./organize-pdf.js";
import { registerPassportPhoto } from "./passport-photo.js";
import { registerPopularTools } from "./popular.js";
import { registerPdfMetadata } from "./pdf-metadata.js";
import { registerPdfPageNumbers } from "./pdf-page-numbers.js";
import { registerPdfToImage } from "./pdf-to-image.js";
@@ -168,6 +169,9 @@ import { registerYamlJson } from "./yaml-json.js";
* (when `enableExperimentalTools` is off) are skipped at startup.
*/
export async function registerToolRoutes(app: FastifyInstance): Promise<void> {
// Register non-tool utility endpoints (not subject to disable/experimental skip logic)
await registerPopularTools(app);
// Read disabled tools from settings
const [disabledRow] = await db
.select()
+33
View File
@@ -0,0 +1,33 @@
import type { FastifyInstance } from "fastify";
import { db } from "../../db/index.js";
import { jobs } from "../../db/schema.js";
import { sql } from "drizzle-orm";
const DEFAULT_POPULAR = [
"resize", "crop", "compress", "convert", "remove-background",
"upscale", "merge-pdf", "watermark-text", "compress-video",
"trim-video", "convert-audio", "compress-pdf",
];
export async function registerPopularTools(app: FastifyInstance) {
app.get("/api/v1/tools/popular", async () => {
try {
const rows = await db
.select({
toolId: jobs.toolId,
count: sql<number>`count(*)`.as("count"),
})
.from(jobs)
.groupBy(jobs.toolId)
.orderBy(sql`count(*) desc`)
.limit(12);
if (rows.length < 4) {
return { tools: DEFAULT_POPULAR };
}
return { tools: rows.map((r) => r.toolId) };
} catch {
return { tools: DEFAULT_POPULAR };
}
});
}