feat(hail-recon): enable live API — USE_MOCK off, KV dynamic import for graceful degradation

This commit is contained in:
Satyam-Rastogi
2026-05-18 16:13:29 +05:30
parent 9e2173fd43
commit 4c1c454681
3 changed files with 27 additions and 20 deletions
+7 -6
View File
@@ -2,12 +2,13 @@
* Returns current storm status — polled by the frontend every 60 seconds. * Returns current storm status — polled by the frontend every 60 seconds.
* Storm is considered "active" if an event was received within the last 24 hours. * Storm is considered "active" if an event was received within the last 24 hours.
* *
* Requires Vercel KV to be connected for auto-trigger to work.
* Without KV: always returns stormActive: false (manual Storm Mode still works).
*
* Response: { stormActive: boolean, event: StormEvent | null, checkedAt: number } * Response: { stormActive: boolean, event: StormEvent | null, checkedAt: number }
*/ */
import { kv } from '@vercel/kv'; const STORM_ACTIVE_WINDOW_MS = 24 * 60 * 60 * 1000;
const STORM_ACTIVE_WINDOW_MS = 24 * 60 * 60 * 1000; // 24 hours
export default async function handler(req, res) { export default async function handler(req, res) {
res.setHeader('Cache-Control', 'no-store'); res.setHeader('Cache-Control', 'no-store');
@@ -17,6 +18,7 @@ export default async function handler(req, res) {
} }
try { try {
const { kv } = await import('@vercel/kv');
const event = await kv.get('lastStormEvent'); const event = await kv.get('lastStormEvent');
const stormActive = !!event && const stormActive = !!event &&
@@ -28,9 +30,8 @@ export default async function handler(req, res) {
event: stormActive ? event : null, event: stormActive ? event : null,
checkedAt: Date.now(), checkedAt: Date.now(),
}); });
} catch (err) { } catch {
// KV not configured (local dev without env vars) — return inactive // KV not configured — storm auto-trigger unavailable, manual toggle still works
console.warn('[hail-status] KV unavailable:', err.message);
return res.status(200).json({ return res.status(200).json({
stormActive: false, stormActive: false,
event: null, event: null,
+10 -4
View File
@@ -4,10 +4,11 @@
* *
* Configure in Hail Recon dashboard: * Configure in Hail Recon dashboard:
* Webhook URL: https://yourapp.vercel.app/api/hail-webhook?secret=YOUR_WEBHOOK_SECRET * Webhook URL: https://yourapp.vercel.app/api/hail-webhook?secret=YOUR_WEBHOOK_SECRET
*
* Requires Vercel KV to be connected for persistence.
* Without KV: logs the event but auto Storm Mode won't trigger on next poll.
*/ */
import { kv } from '@vercel/kv';
export default async function handler(req, res) { export default async function handler(req, res) {
if (req.method !== 'POST') { if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' }); return res.status(405).json({ error: 'Method not allowed' });
@@ -31,9 +32,14 @@ export default async function handler(req, res) {
receivedAt: Date.now(), receivedAt: Date.now(),
}; };
try {
const { kv } = await import('@vercel/kv');
await kv.set('lastStormEvent', stormEvent); await kv.set('lastStormEvent', stormEvent);
console.log('[hail-webhook] Storm event stored in KV:', stormEvent);
console.log('[hail-webhook] Storm event stored:', stormEvent); } catch {
// KV not configured — log only, auto Storm Mode won't trigger
console.log('[hail-webhook] KV unavailable. Event received but not persisted:', stormEvent);
}
return res.status(200).json({ ok: true, received: stormEvent }); return res.status(200).json({ ok: true, received: stormEvent });
} }
+1 -1
View File
@@ -12,7 +12,7 @@
import { useState, useEffect, useCallback } from 'react'; import { useState, useEffect, useCallback } from 'react';
import { MOCK_HAIL_HISTORY } from '../data/mockStore'; import { MOCK_HAIL_HISTORY } from '../data/mockStore';
const USE_MOCK = true; const USE_MOCK = false;
// ── Helpers ────────────────────────────────────────────────────────────────── // ── Helpers ──────────────────────────────────────────────────────────────────