Define as a NextJS API

This commit is contained in:
Andrew Pennington 2024-08-15 23:05:14 +01:00
parent 1b71a9cfd7
commit 293391dd20
No known key found for this signature in database
GPG key ID: E9DA097213FD17EA
2 changed files with 42 additions and 4 deletions

View file

@ -1,5 +1,34 @@
import process from 'process';
import { NextResponse } from 'next/server';
export async function getServerEnv(envVar: string) {
return process.env[envVar];
}
// In-memory cache for configuration data
let cachedConfig: { [key: string]: any } | null = null;
let cacheTimestamp: number | null = null;
const CACHE_DURATION_MS = 5 * 60 * 1000; // Cache duration: 5 minutes
async function fetchConfig() {
try {
const response = await fetch('/api/config');
if (response.ok) {
const data = await response.json();
cachedConfig = data;
cacheTimestamp = Date.now();
} else {
throw new Error('Failed to fetch config');
}
} catch (error) {
console.error('Error fetching config:', error);
throw error;
}
}
export async function getServerEnv(envVar: string): Promise<string | null> {
// Check if the cache is still valid
if (cachedConfig && cacheTimestamp && Date.now() - cacheTimestamp < CACHE_DURATION_MS) {
return cachedConfig[envVar] || null;
}
// Fetch and cache the config if not in cache or cache is expired
await fetchConfig();
return cachedConfig ? cachedConfig[envVar] || null : null;
}