fix: split gemini-token into GET health check + POST token request
All checks were successful
Build & Push / build-and-push (push) Successful in 1m33s

Health check no longer triggers rate limiter, fixing toggle not appearing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-28 14:02:06 +01:00
parent 067164645b
commit b4a265077e
2 changed files with 13 additions and 10 deletions

View File

@@ -4,9 +4,18 @@ import { generateEphemeralToken } from '@/lib/gemini-live';
// ─── Rate Limiting ────────────────────────────────────────────────────────────
const rateLimitMap = new Map<string, number>();
const RATE_LIMIT_MS = 60_000; // 1 token per minute per IP
const RATE_LIMIT_MS = 30_000; // 1 token per 30 seconds per IP
// ─── Route Handler ────────────────────────────────────────────────────────────
// ─── Health Check (GET — no rate limit) ──────────────────────────────────────
export async function GET() {
if (!process.env.GEMINI_API_KEY) {
return NextResponse.json({ success: false }, { status: 503 });
}
return NextResponse.json({ success: true });
}
// ─── Token Request (POST — rate limited) ─────────────────────────────────────
export async function POST(request: NextRequest) {
try {
@@ -26,12 +35,10 @@ export async function POST(request: NextRequest) {
rateLimitMap.set(ip, Date.now());
const { locale } = (await request.json()) as { locale?: string };
const result = await generateEphemeralToken(locale === 'fr' ? 'fr' : 'en');
const result = generateEphemeralToken(locale === 'fr' ? 'fr' : 'en');
return NextResponse.json({
success: true,
// In production, replace apiKey with an ephemeral token from ai.auth.tokens.create()
// to avoid exposing the long-lived API key to the client.
apiKey: process.env.GEMINI_API_KEY,
model: result.model,
config: result.config,

View File

@@ -24,11 +24,7 @@ export default function ModeToggle({ mode, onChange }: ModeToggleProps) {
if (!navigator.mediaDevices?.getUserMedia) return;
try {
const res = await fetch('/api/gemini-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ locale: 'en' }),
});
const res = await fetch('/api/gemini-token');
const data = (await res.json()) as { success: boolean };
if (data.success) setVoiceSupported(true);
} catch {