mirror of
https://github.com/yusufipk/RepoHub.git
synced 2026-09-11 10:36:07 +00:00
feat: Add authentication to platform and auto-sync APIs and remove debug endpoints.
This commit is contained in:
@@ -14,10 +14,19 @@ export const maxDuration = 1800 // 30 minutes timeout for auto sync
|
||||
let lastAutoSync: Date | null = null
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
// Check auth - require sync secret or localhost
|
||||
const authResult = await SyncAuth.isSyncAllowed(request)
|
||||
if (!authResult.allowed) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Sync operation not allowed', reason: authResult.reason },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
// Check if auto sync is enabled
|
||||
if (!SyncAuth.isAutoSyncEnabled()) {
|
||||
return NextResponse.json({
|
||||
return NextResponse.json({
|
||||
message: 'Auto sync is disabled',
|
||||
next_sync: null
|
||||
})
|
||||
@@ -26,9 +35,9 @@ export async function POST(request: NextRequest) {
|
||||
// Check if enough time has passed since last sync
|
||||
const now = new Date()
|
||||
const nextSyncTime = SyncAuth.getNextSyncTime(lastAutoSync || undefined)
|
||||
|
||||
|
||||
if (now < nextSyncTime) {
|
||||
return NextResponse.json({
|
||||
return NextResponse.json({
|
||||
message: 'Auto sync not due yet',
|
||||
last_sync: lastAutoSync?.toISOString(),
|
||||
next_sync: nextSyncTime.toISOString(),
|
||||
@@ -37,10 +46,10 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
console.log('🔄 Starting automatic package sync...')
|
||||
|
||||
|
||||
// Sync all platforms in sequence
|
||||
const syncResults = []
|
||||
|
||||
|
||||
try {
|
||||
// Sync Debian/Ubuntu packages
|
||||
console.log('Syncing Debian/Ubuntu packages...')
|
||||
@@ -102,12 +111,12 @@ export async function POST(request: NextRequest) {
|
||||
// Update last sync time
|
||||
lastAutoSync = now
|
||||
const nextSync = SyncAuth.getNextSyncTime(lastAutoSync)
|
||||
|
||||
|
||||
const successCount = syncResults.filter(r => r.status === 'success').length
|
||||
const totalCount = syncResults.length
|
||||
|
||||
|
||||
console.log(`✅ Auto sync completed: ${successCount}/${totalCount} platforms synced successfully`)
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
message: 'Auto sync completed',
|
||||
timestamp: now.toISOString(),
|
||||
@@ -124,8 +133,8 @@ export async function POST(request: NextRequest) {
|
||||
} catch (error) {
|
||||
console.error('Auto sync failed:', error)
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Auto sync failed',
|
||||
{
|
||||
error: 'Auto sync failed',
|
||||
details: error instanceof Error ? error.message : 'Unknown error',
|
||||
timestamp: new Date().toISOString()
|
||||
},
|
||||
@@ -137,7 +146,7 @@ export async function POST(request: NextRequest) {
|
||||
export async function GET() {
|
||||
const now = new Date()
|
||||
const nextSyncTime = SyncAuth.getNextSyncTime(lastAutoSync || undefined)
|
||||
|
||||
|
||||
return NextResponse.json({
|
||||
auto_sync_enabled: SyncAuth.isAutoSyncEnabled(),
|
||||
auto_sync_days: SyncAuth.getAutoSyncDays(),
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { query } from '@/lib/database/config'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Check if arch platform exists
|
||||
const platformResult = await query(
|
||||
'SELECT * FROM platforms WHERE id = $1',
|
||||
['arch']
|
||||
)
|
||||
|
||||
// Count arch packages
|
||||
const countResult = await query(
|
||||
'SELECT COUNT(*) FROM packages WHERE platform_id = $1',
|
||||
['arch']
|
||||
)
|
||||
|
||||
// Get sample packages
|
||||
const sampleResult = await query(
|
||||
'SELECT name, version, description FROM packages WHERE platform_id = $1 LIMIT 5',
|
||||
['arch']
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
platform: platformResult.rows[0] || null,
|
||||
totalPackages: parseInt(countResult.rows[0].count),
|
||||
samplePackages: sampleResult.rows
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Debug error:', error)
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'Unknown error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ export async function GET(
|
||||
) {
|
||||
try {
|
||||
const platform = await PlatformService.getById(params.id)
|
||||
|
||||
|
||||
if (!platform) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Platform not found' },
|
||||
@@ -25,14 +25,25 @@ export async function GET(
|
||||
}
|
||||
}
|
||||
|
||||
import { SyncAuth } from '@/lib/sync/auth'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
// Check auth
|
||||
const auth = await SyncAuth.isWriteAllowed(request)
|
||||
if (!auth.allowed) {
|
||||
return NextResponse.json(
|
||||
{ error: auth.reason || 'Unauthorized' },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const platform = await PlatformService.update(params.id, body)
|
||||
|
||||
|
||||
if (!platform) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Platform not found' },
|
||||
@@ -54,9 +65,18 @@ export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
// Check auth
|
||||
const auth = await SyncAuth.isWriteAllowed(request)
|
||||
if (!auth.allowed) {
|
||||
return NextResponse.json(
|
||||
{ error: auth.reason || 'Unauthorized' },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const success = await PlatformService.delete(params.id)
|
||||
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Platform not found' },
|
||||
|
||||
@@ -14,7 +14,18 @@ export async function GET() {
|
||||
}
|
||||
}
|
||||
|
||||
import { SyncAuth } from '@/lib/sync/auth'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
// Check auth
|
||||
const auth = await SyncAuth.isWriteAllowed(request)
|
||||
if (!auth.allowed) {
|
||||
return NextResponse.json(
|
||||
{ error: auth.reason || 'Unauthorized' },
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const platform = await PlatformService.create(body)
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { query } from '@/lib/database/config'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Test 1: Database connection
|
||||
const dbTest = await query('SELECT NOW() as current_time', [])
|
||||
|
||||
// Test 2: Packages table count
|
||||
const packageCount = await query('SELECT COUNT(*) as count FROM packages', [])
|
||||
|
||||
// Test 3: Debian packages count
|
||||
const debianCount = await query('SELECT COUNT(*) as count FROM packages WHERE platform_id = $1', ['debian'])
|
||||
|
||||
// Test 4: Active packages count
|
||||
const activeCount = await query('SELECT COUNT(*) as count FROM packages WHERE is_active = true', [])
|
||||
|
||||
// Test 5: Sample packages
|
||||
const samplePackages = await query(`
|
||||
SELECT id, name, version, platform_id
|
||||
FROM packages
|
||||
WHERE platform_id = $1 AND is_active = true
|
||||
LIMIT 5
|
||||
`, ['debian'])
|
||||
|
||||
return NextResponse.json({
|
||||
database: {
|
||||
connected: true,
|
||||
currentTime: dbTest.rows[0].current_time
|
||||
},
|
||||
packages: {
|
||||
total: packageCount.rows[0].count,
|
||||
debian: debianCount.rows[0].count,
|
||||
active: activeCount.rows[0].count,
|
||||
sample: samplePackages.rows
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database test error:', error)
|
||||
return NextResponse.json({
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
database: { connected: false }
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user