import { NextResponse } from 'next/server'; import { Pool } from 'pg'; // Create a new pool using the environment variables const pool = new Pool({ user: process.env.DB_USER, password: process.env.DB_PASSWORD, host: process.env.DB_HOST?.replace('@https://', ''), database: process.env.DB_NAME, ssl: { rejectUnauthorized: false // Required for some cloud databases } }); export async function GET() { try { // Connect to the database const client = await pool.connect(); try { // Query the news table for the latest 10 news items const result = await client.query(` SELECT id, title, url, date, source, description, category, sentiment_score, sentiment, symbols, symbol FROM news ORDER BY date DESC LIMIT 10 `); // Format the response const news = result.rows.map(row => ({ id: row.id, title: row.title, url: row.url, date: row.date, source: row.source, description: row.description, category: row.category, sentiment: { score: row.sentiment_score, label: row.sentiment }, symbols: row.symbols, symbol: row.symbol })); return NextResponse.json({ news }); } finally { // Release the client back to the pool client.release(); } } catch (error) { console.error('Database connection error:', error); return NextResponse.json( { error: 'Failed to fetch news' }, { status: 500 } ); } }