Spaces:
Running
Running
import { Hono } from 'hono'; | |
import { config } from '../config'; | |
import { cache } from '../services/cache'; | |
const querySuggestionsApp = new Hono(); | |
// Query suggestions endpoint - matches Next.js /api/query-suggestions | |
querySuggestionsApp.get('/', async (c) => { | |
try { | |
const query = c.req.query('query'); | |
if (!query) { | |
return c.json({ suggestions: [] }); | |
} | |
// Check cache | |
const cacheKey = `suggestions:${query}`; | |
const cachedSuggestions = cache.get(cacheKey); | |
if (cachedSuggestions) { | |
c.header('X-Cache', 'HIT'); | |
return c.json(cachedSuggestions); | |
} | |
// Proxy to backend | |
const suggestionsUrl = `${config.backendUrl}/suggestions?query=${encodeURIComponent(query)}`; | |
const response = await fetch(suggestionsUrl); | |
if (!response.ok) { | |
throw new Error(`Backend returned ${response.status}`); | |
} | |
const data = await response.json(); | |
// Cache for 5 minutes | |
cache.set(cacheKey, data, 300); | |
c.header('X-Cache', 'MISS'); | |
return c.json(data); | |
} catch (error) { | |
console.error('Suggestions error:', error); | |
return c.json({ | |
error: 'Failed to fetch suggestions', | |
suggestions: [] | |
}, 500); | |
} | |
}); | |
export { querySuggestionsApp }; |