swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
51 lines · 1.5 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
46
47
48
49
50
51
/**
 * Cron endpoint for mirror synchronization
 * Call this endpoint periodically (e.g., every 15 minutes via Vercel Cron)
 */

import type { APIRoute } from "astro";
import { syncAllMirrors } from "@/lib/mirror-sync";
import { logger } from "@/lib/logger";

// Secret for cron authentication (prevents unauthorized triggers)
const CRON_SECRET = process.env.CRON_SECRET;

export const POST: APIRoute = async ({ request }) => {
    // Verify cron secret
    const authHeader = request.headers.get("Authorization");
    const providedSecret = authHeader?.replace("Bearer ", "");

    if (CRON_SECRET && providedSecret !== CRON_SECRET) {
        return new Response("Unauthorized", { status: 401 });
    }

    try {
        logger.info("Starting scheduled mirror sync");

        const result = await syncAllMirrors();

        return new Response(JSON.stringify({
            success: true,
            synced: result.synced,
            failed: result.failed,
        }), {
            status: 200,
            headers: { "Content-Type": "application/json" },
        });
    } catch (error) {
        const message = error instanceof Error ? error.message : "Unknown error";
        logger.error({ error: message }, "Mirror sync cron failed");

        return new Response(JSON.stringify({
            success: false,
            error: message,
        }), {
            status: 500,
            headers: { "Content-Type": "application/json" },
        });
    }
};

// Also support GET for simpler cron services
export const GET = POST;