This commit is contained in:
alma 2025-04-14 17:29:13 +02:00
parent 1ab9638fe9
commit 4a969a0fed

View File

@ -1,63 +1,41 @@
import { NextResponse } from 'next/server';
import { Pool } from 'pg';
// Get database configuration from environment variables
const DB_HOST = process.env.DB_HOST || '172.16.0.104';
const DB_PORT = process.env.DB_PORT || '5432';
const DB_USER = process.env.DB_USER || 'alma';
const DB_PASSWORD = process.env.DB_PASSWORD || 'alma'; // Default password
const DB_NAME = process.env.DB_NAME || 'rivacube';
// Construct connection string from components
const connectionString = `postgresql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`;
// Log the connection string (with password masked for security)
const maskedConnectionString = connectionString.replace(/\/\/[^:]+:[^@]+@/, '//***:***@');
console.log('Using connection string:', maskedConnectionString);
const pool = new Pool({
connectionString,
// Add connection timeout
connectionTimeoutMillis: 5000,
// Add query timeout
query_timeout: 5000,
});
// FastAPI server configuration
const API_HOST = process.env.API_HOST || 'http://172.16.0.104:8000';
export async function GET() {
let client;
try {
console.log(`Attempting to connect to database at ${DB_HOST}:${DB_PORT}...`);
client = await pool.connect();
console.log('Database connection successful');
const result = await client.query(
`SELECT id, title, date, source, description, category, url
FROM news
ORDER BY date DESC
LIMIT 5`
);
console.log(`Fetching news from FastAPI server at ${API_HOST}...`);
console.log(`Successfully fetched ${result.rows.length} news articles`);
return NextResponse.json(result.rows);
const response = await fetch(`${API_HOST}/news`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const news = await response.json();
console.log(`Successfully fetched ${news.length} news articles`);
return NextResponse.json(news);
} catch (error) {
console.error('Error in news API:', {
error: error instanceof Error ? error.message : 'Unknown error',
host: DB_HOST,
port: DB_PORT,
database: DB_NAME,
apiHost: API_HOST,
stack: error instanceof Error ? error.stack : undefined
});
return NextResponse.json(
{
error: 'Failed to fetch news',
details: error instanceof Error ? error.message : 'Unknown error',
server: DB_HOST
server: API_HOST
},
{ status: 500 }
);
} finally {
if (client) {
client.release();
}
}
}