File size: 1,258 Bytes
5dfbe50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 };