-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathroute.tsx
More file actions
45 lines (43 loc) · 1.37 KB
/
route.tsx
File metadata and controls
45 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export const fetchCache = "force-no-store";
import { publicURL } from "@/lib/utils";
import type { NextRequest } from "next/server";
export function GET(request: NextRequest) {
const authHeader = request.headers.get("authorization");
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
console.error("[CRON] Unauthorized request: invalid authorization header");
return new Response("Unauthorized", {
status: 401,
});
}
try {
// Forward the action param if present (discover, fetch, sync)
const action = request.nextUrl.searchParams.get("action");
const params = action ? `?action=${action}` : "";
const url = `${publicURL()}/api/youtube/views${params}`;
console.log("[CRON] Triggering YouTube views update:", url);
fetch(url, {
method: "POST",
headers: {
authorization: `Bearer ${process.env.CRON_SECRET}`,
"Cache-Control": "no-cache",
},
})
.then((res) => {
if (!res.ok) {
console.error("[CRON] Failed to trigger YouTube views:", res.status);
} else {
console.log("[CRON] Successfully triggered YouTube views update.");
}
})
.catch((err) => {
console.error("[CRON] Error triggering YouTube views:", err);
});
return Response.json({ success: true });
} catch (err) {
console.error("[CRON] Unexpected error:", err);
return Response.json(
{ success: false, error: String(err) },
{ status: 500 },
);
}
}