30 lines
694 B
TypeScript
30 lines
694 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import { Pool } from 'pg';
|
|
|
|
const pool = new Pool({
|
|
connectionString: process.env.NEWSDB_URL,
|
|
});
|
|
|
|
export async function GET() {
|
|
try {
|
|
const client = await pool.connect();
|
|
try {
|
|
const result = await client.query(
|
|
`SELECT id, title, date, source, description, category, url
|
|
FROM news
|
|
ORDER BY date DESC
|
|
LIMIT 5`
|
|
);
|
|
|
|
return NextResponse.json(result.rows);
|
|
} finally {
|
|
client.release();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching news:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch news' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|