feat: add API endpoints for Debian and Ubuntu package syncing

- Implemented /api/sync-debian and /api/sync-ubuntu routes with authentication and status tracking
- Added GET endpoints to check sync status and POST endpoints to trigger package fetches
- Included concurrent sync prevention and async error handling for long-running operations
This commit is contained in:
Yusuf İpek
2025-11-11 20:19:48 +03:00
parent 87f28e4da9
commit db91b8d96a
2 changed files with 100 additions and 0 deletions
+50
View File
@@ -0,0 +1,50 @@
import { NextRequest, NextResponse } from 'next/server'
import { DebianPackageFetcher } from '@/services/debianPackageFetcher'
import { SyncAuth } from '@/lib/sync/auth'
export const dynamic = 'force-dynamic'
export const maxDuration = 300
let syncInProgress = false
let syncStatus = {
status: 'idle' as 'idle' | 'running' | 'complete' | 'error',
error: null as string | null
}
export async function GET() {
return NextResponse.json(syncStatus)
}
export async function POST(request: NextRequest) {
const authResult = await SyncAuth.isSyncAllowed(request)
if (!authResult.allowed) {
return NextResponse.json(
{ error: 'Sync operation not allowed', reason: authResult.reason },
{ status: 403 }
)
}
if (syncInProgress) {
return NextResponse.json(
{ error: 'Sync already in progress' },
{ status: 409 }
)
}
syncInProgress = true
syncStatus = { status: 'running', error: null }
;(async () => {
try {
await DebianPackageFetcher.fetchDebianPackages()
syncStatus.status = 'complete'
} catch (error) {
syncStatus.status = 'error'
syncStatus.error = error instanceof Error ? error.message : 'Unknown error'
} finally {
syncInProgress = false
}
})()
return NextResponse.json({ message: 'Debian packages sync started', status: syncStatus })
}