Skip to content

Commit 0954eab

Browse files
authored
Merge pull request #201 from codeunia-dev/fix/cache-management-crashes
🚀 Comprehensive Webhook System & Cache Management Overhaul
2 parents aaef236 + a006272 commit 0954eab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5032
-516
lines changed

app/admin/page.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import {
2121
ArrowDownRight,
2222
ClipboardCheck,
2323
Zap,
24+
Webhook,
2425
} from "lucide-react"
2526
import { createClient } from "@/lib/supabase/client"
2627
import type { BlogPost } from "@/components/data/blog-posts"
2728
import { RealtimeChannel } from "@supabase/supabase-js"
2829
import { CacheAnalyticsDashboard } from "@/components/admin/CacheAnalyticsSimple"
30+
// import { CacheManagementDashboard } from "@/components/admin/CacheManagementDashboard"
31+
import { WebhookManagementDashboard } from "@/components/admin/WebhookManagementDashboard"
2932
import PerformanceMonitoring from "@/components/admin/PerformanceMonitoring"
3033
import SecurityMonitoring from "@/components/admin/SecurityMonitoring"
3134

@@ -550,7 +553,7 @@ export default function AdminDashboard() {
550553

551554
<div className="relative z-10">
552555
<Tabs defaultValue="cache" className="w-full">
553-
<TabsList className="grid w-full grid-cols-3 bg-zinc-900/60 border border-zinc-800">
556+
<TabsList className="grid w-full grid-cols-4 bg-zinc-900/60 border border-zinc-800">
554557
<TabsTrigger value="cache" className="data-[state=active]:bg-blue-600 data-[state=active]:text-white">
555558
<Zap className="w-4 h-4 mr-2" />
556559
Cache Analytics
@@ -563,6 +566,10 @@ export default function AdminDashboard() {
563566
<AlertTriangle className="w-4 h-4 mr-2" />
564567
Security
565568
</TabsTrigger>
569+
<TabsTrigger value="webhooks" className="data-[state=active]:bg-purple-600 data-[state=active]:text-white">
570+
<Webhook className="w-4 h-4 mr-2" />
571+
Webhooks
572+
</TabsTrigger>
566573
</TabsList>
567574

568575
<TabsContent value="cache" className="mt-6">
@@ -576,6 +583,10 @@ export default function AdminDashboard() {
576583
<TabsContent value="security" className="mt-6">
577584
<SecurityMonitoring />
578585
</TabsContent>
586+
587+
<TabsContent value="webhooks" className="mt-6">
588+
<WebhookManagementDashboard />
589+
</TabsContent>
579590
</Tabs>
580591
</div>
581592

app/api/admin/cache-analytics/route.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextRequest } from 'next/server'
2-
import { createCachedResponse } from '@/lib/simple-cache'
2+
import { UnifiedCache } from '@/lib/unified-cache-system'
33

44
/**
55
* API endpoint for cache analytics - Admin only
@@ -16,11 +16,11 @@ export async function GET(request: NextRequest) {
1616
// }
1717

1818
const url = new URL(request.url)
19-
const period = url.searchParams.get('period')
19+
// const period = url.searchParams.get('period')
2020
const format = url.searchParams.get('format') as 'json' | 'csv' | null
2121

22-
// Default to 24 hours
23-
const periodMs = period ? parseInt(period) * 1000 : 24 * 60 * 60 * 1000
22+
// Default to 24 hours - period is used for future analytics filtering
23+
// const _periodMs = period ? parseInt(period) * 1000 : 24 * 60 * 60 * 1000
2424

2525
// Provide basic analytics data for now
2626
const basicAnalytics = {
@@ -66,12 +66,12 @@ export async function GET(request: NextRequest) {
6666
})
6767
}
6868

69-
return createCachedResponse(basicAnalytics, 'API_SHORT')
69+
return UnifiedCache.createResponse(basicAnalytics, 'API_STANDARD')
7070
} catch (error) {
7171
console.error('Cache analytics API error:', error)
72-
return createCachedResponse(
72+
return UnifiedCache.createResponse(
7373
{ error: 'Failed to fetch cache analytics' },
74-
'NO_CACHE'
74+
'USER_PRIVATE'
7575
)
7676
}
7777
}

app/api/admin/cache-invalidate/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextRequest } from 'next/server'
22
import { cacheAnalytics } from '@/lib/cache-analytics-server'
3-
import { createCachedApiResponse } from '@/lib/production-cache'
3+
import { UnifiedCache } from '@/lib/unified-cache-system'
44

55
/**
66
* API endpoint to trigger manual cache invalidation - Admin only
@@ -27,15 +27,15 @@ export async function POST(request: NextRequest) {
2727
// Here you could add additional invalidation logic
2828
// For example, clearing specific cache keys or triggering CDN purge
2929

30-
return createCachedApiResponse({
30+
return UnifiedCache.createResponse({
3131
success: true,
3232
message: 'Cache invalidation triggered',
3333
timestamp: new Date().toISOString()
3434
}, 'USER_PRIVATE')
3535

3636
} catch (error) {
3737
console.error('Cache invalidation API error:', error)
38-
return createCachedApiResponse(
38+
return UnifiedCache.createResponse(
3939
{ error: 'Failed to trigger cache invalidation' },
4040
'USER_PRIVATE'
4141
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Cache Management API
3+
*
4+
* Provides endpoints for:
5+
* - Manual cache purging
6+
* - Cache status monitoring
7+
* - Webhook handling for automatic purges
8+
*/
9+
10+
import { NextRequest } from 'next/server'
11+
import { handleCachePurgeWebhook, CacheManager } from '@/lib/cloudflare-cache-purge'
12+
import { UnifiedCache } from '@/lib/unified-cache-system'
13+
14+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15+
export async function GET(_request: NextRequest) {
16+
try {
17+
// TODO: Add admin authentication
18+
// const user = await getUser(_request)
19+
// if (!user?.isAdmin) {
20+
// return new Response('Unauthorized', { status: 401 })
21+
// }
22+
23+
const status = await CacheManager.getStatus()
24+
25+
return UnifiedCache.createResponse(status, 'USER_PRIVATE')
26+
} catch (error) {
27+
console.error('Cache status error:', error)
28+
return UnifiedCache.createResponse(
29+
{ error: 'Failed to get cache status' },
30+
'USER_PRIVATE'
31+
)
32+
}
33+
}
34+
35+
export async function POST(request: NextRequest) {
36+
try {
37+
// TODO: Add admin authentication
38+
// const user = await getUser(request)
39+
// if (!user?.isAdmin) {
40+
// return new Response('Unauthorized', { status: 401 })
41+
// }
42+
43+
const { action, type } = await request.json()
44+
45+
if (action === 'purge') {
46+
const result = await CacheManager.manualPurge(type)
47+
return UnifiedCache.createResponse(result, 'USER_PRIVATE')
48+
}
49+
50+
if (action === 'webhook') {
51+
const { webhookAction, data } = await request.json()
52+
const result = await handleCachePurgeWebhook(webhookAction, data)
53+
return UnifiedCache.createResponse(result, 'USER_PRIVATE')
54+
}
55+
56+
return UnifiedCache.createResponse(
57+
{ success: false, message: 'Invalid action' },
58+
'USER_PRIVATE'
59+
)
60+
} catch (error) {
61+
console.error('Cache management error:', error)
62+
return UnifiedCache.createResponse(
63+
{ error: 'Cache management failed' },
64+
'USER_PRIVATE'
65+
)
66+
}
67+
}

app/api/admin/cache-test-data/route.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextRequest } from 'next/server'
22
import { cacheAnalytics } from '@/lib/cache-analytics-server'
3-
import { createCachedApiResponse } from '@/lib/production-cache'
3+
import { UnifiedCache } from '@/lib/unified-cache-system'
44

55
const routes = [
66
'/api/hackathons',
@@ -79,15 +79,15 @@ export async function POST(request: NextRequest) {
7979
// Return current analytics data
8080
const analytics = cacheAnalytics.getDetailedAnalytics()
8181

82-
return createCachedApiResponse({
82+
return UnifiedCache.createResponse({
8383
success: true,
8484
message: `Generated ${totalGenerated} cache events`,
8585
analytics
86-
}, 'API_REALTIME')
86+
}, 'REALTIME')
8787

8888
} catch (error) {
8989
console.error('Error generating cache test data:', error)
90-
return createCachedApiResponse(
90+
return UnifiedCache.createResponse(
9191
{
9292
success: false,
9393
error: 'Failed to generate cache test data',
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Enhanced Hackathons API with Production Caching
3+
*/
4+
5+
import { NextRequest } from 'next/server'
6+
import { UnifiedCache } from '@/lib/unified-cache-system'
7+
import { createClient } from '@/lib/supabase/server'
8+
9+
export async function GET(request: NextRequest) {
10+
try {
11+
const url = new URL(request.url)
12+
const featured = url.searchParams.get('featured') === 'true'
13+
const limit = parseInt(url.searchParams.get('limit') || '10')
14+
15+
// Create cache key based on parameters
16+
const cacheKey = `hackathons:${featured ? 'featured' : 'all'}:${limit}`
17+
18+
// Use cached query with 5-minute TTL
19+
const hackathons = await UnifiedCache.cachedQuery(
20+
cacheKey,
21+
async () => {
22+
const supabase = await createClient()
23+
24+
let query = supabase
25+
.from('hackathons')
26+
.select('*')
27+
.order('created_at', { ascending: false })
28+
.limit(limit)
29+
30+
if (featured) {
31+
query = query.eq('featured', true)
32+
}
33+
34+
const { data, error } = await query
35+
36+
if (error) {
37+
throw new Error(`Database error: ${error.message}`)
38+
}
39+
40+
return data || []
41+
},
42+
'DATABASE_QUERIES'
43+
)
44+
45+
return UnifiedCache.createResponse(
46+
{
47+
hackathons,
48+
count: hackathons.length,
49+
featured,
50+
timestamp: new Date().toISOString(),
51+
},
52+
'DYNAMIC_CONTENT'
53+
)
54+
} catch (error) {
55+
console.error('Hackathons API error:', error)
56+
return UnifiedCache.createResponse(
57+
{
58+
error: 'Failed to fetch hackathons',
59+
hackathons: [],
60+
count: 0,
61+
},
62+
'USER_PRIVATE'
63+
)
64+
}
65+
}

0 commit comments

Comments
 (0)