mirror of
https://github.com/yusufipk/RepoHub.git
synced 2026-09-11 10:36:07 +00:00
- Removed debug-packages and test-sync API endpoints no longer needed - Extracted sync status management into shared lib/sync/status module - Updated Next.js config to remove deprecated experimental appDir flag - Added port specification to production start script for consistency
102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { SimplePackageFetcher } from '@/services/simplePackageFetcher'
|
|
import { PlatformInitializer } from '@/services/platformInitializer'
|
|
import { setSyncInProgress, setSyncProgress } from '@/lib/sync/status'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
export const revalidate = 0
|
|
|
|
export async function GET(request: NextRequest) {
|
|
// Skip any heavy sync work during static generation/build unless explicitly enabled
|
|
if (process.env.ENABLE_SYNC_DURING_BUILD !== 'true') {
|
|
// Return a lightweight JSON response so build doesn't hang
|
|
return NextResponse.json({ disabled: true, reason: 'Sync disabled during build' })
|
|
}
|
|
const encoder = new TextEncoder()
|
|
let cancelled = false
|
|
|
|
// Set sync as in progress
|
|
setSyncInProgress(true)
|
|
|
|
// Create a readable stream for SSE
|
|
const stream = new ReadableStream({
|
|
async start(controller) {
|
|
try {
|
|
// Helper function to send progress updates
|
|
const sendProgress = (message: string, progress?: number, total?: number) => {
|
|
if (cancelled) return
|
|
|
|
const data = JSON.stringify({
|
|
message,
|
|
progress,
|
|
total,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
controller.enqueue(encoder.encode(`data: ${data}\n\n`))
|
|
setSyncProgress(message, progress, total)
|
|
}
|
|
|
|
sendProgress('🔄 Starting package synchronization...')
|
|
|
|
// Initialize platforms
|
|
sendProgress('📋 Initializing platforms...')
|
|
await PlatformInitializer.initializePlatforms()
|
|
sendProgress('✅ Platforms initialized')
|
|
|
|
// Fetch Debian packages
|
|
sendProgress('🐧 Fetching Debian packages...')
|
|
await SimplePackageFetcher.fetchDebianPackagesWithProgress(sendProgress)
|
|
|
|
if (cancelled) {
|
|
sendProgress('❌ Sync cancelled during Debian processing')
|
|
controller.close()
|
|
return
|
|
}
|
|
|
|
sendProgress('✅ Debian packages completed')
|
|
|
|
// Fetch Ubuntu packages
|
|
sendProgress('🐧 Fetching Ubuntu packages...')
|
|
await SimplePackageFetcher.fetchUbuntuPackagesWithProgress(sendProgress)
|
|
|
|
if (cancelled) {
|
|
sendProgress('❌ Sync cancelled during Ubuntu processing')
|
|
controller.close()
|
|
return
|
|
}
|
|
|
|
sendProgress('✅ Ubuntu packages completed')
|
|
|
|
// Final success message
|
|
sendProgress('🎉 All packages synchronized successfully!')
|
|
|
|
// Close the stream
|
|
controller.close()
|
|
setSyncInProgress(false)
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
const errorData = JSON.stringify({
|
|
error: errorMessage,
|
|
timestamp: new Date().toISOString()
|
|
})
|
|
controller.enqueue(encoder.encode(`data: ${errorData}\n\n`))
|
|
controller.close()
|
|
setSyncInProgress(false)
|
|
}
|
|
},
|
|
|
|
cancel() {
|
|
cancelled = true
|
|
setSyncInProgress(false)
|
|
}
|
|
})
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
'Connection': 'keep-alive',
|
|
},
|
|
})
|
|
}
|