Spaces:
Build error
Build error
File size: 2,063 Bytes
6a059d3 | 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 52 53 54 55 56 57 58 59 60 61 62 63 64 | import { NextRequest, NextResponse } from "next/server"
import { getCached, setCached, deleteCacheKey, CACHE_KEYS, CACHE_TTL } from "@/lib/redis"
export async function GET(request: NextRequest) {
try {
const cacheKey = CACHE_KEYS.ADMIN_CONFIG
// Try to get from cache first
const cached = await getCached(cacheKey)
if (cached) {
return NextResponse.json(cached, {
headers: { "X-Cache": "HIT" },
})
}
// Fetch from backend
const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"
const userToken = request.headers.get("X-Session-Token") || null
const headers: Record<string, string> = {}
if (userToken) {
headers["X-Session-Token"] = userToken
}
const response = await fetch(`${apiBaseUrl}/api/admin/config`, { headers })
if (!response.ok) {
return NextResponse.json(
{ error: "Failed to fetch config" },
{ status: response.status }
)
}
const config = await response.json()
// Cache the result
await setCached(cacheKey, config, CACHE_TTL.ADMIN_CONFIG)
return NextResponse.json(config, {
headers: { "X-Cache": "MISS" },
})
} catch (error: any) {
console.error("Config cache error:", error)
return NextResponse.json(
{ error: error.message || "Internal server error" },
{ status: 500 }
)
}
}
// DELETE: Invalidate config cache
export async function DELETE() {
try {
const cacheKey = CACHE_KEYS.ADMIN_CONFIG
await deleteCacheKey(cacheKey)
return NextResponse.json({ success: true })
} catch (error: any) {
console.error("Delete config cache error:", error)
return NextResponse.json(
{ error: error.message || "Internal server error" },
{ status: 500 }
)
}
}
|