fastapi
This commit is contained in:
parent
1ab9638fe9
commit
4a969a0fed
@ -1,63 +1,41 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { Pool } from 'pg';
|
|
||||||
|
|
||||||
// Get database configuration from environment variables
|
// FastAPI server configuration
|
||||||
const DB_HOST = process.env.DB_HOST || '172.16.0.104';
|
const API_HOST = process.env.API_HOST || 'http://172.16.0.104:8000';
|
||||||
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,
|
|
||||||
});
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
let client;
|
|
||||||
try {
|
try {
|
||||||
console.log(`Attempting to connect to database at ${DB_HOST}:${DB_PORT}...`);
|
console.log(`Fetching news from FastAPI server at ${API_HOST}...`);
|
||||||
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(`Successfully fetched ${result.rows.length} news articles`);
|
const response = await fetch(`${API_HOST}/news`, {
|
||||||
return NextResponse.json(result.rows);
|
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) {
|
} catch (error) {
|
||||||
console.error('Error in news API:', {
|
console.error('Error in news API:', {
|
||||||
error: error instanceof Error ? error.message : 'Unknown error',
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
host: DB_HOST,
|
apiHost: API_HOST,
|
||||||
port: DB_PORT,
|
|
||||||
database: DB_NAME,
|
|
||||||
stack: error instanceof Error ? error.stack : undefined
|
stack: error instanceof Error ? error.stack : undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{
|
{
|
||||||
error: 'Failed to fetch news',
|
error: 'Failed to fetch news',
|
||||||
details: error instanceof Error ? error.message : 'Unknown error',
|
details: error instanceof Error ? error.message : 'Unknown error',
|
||||||
server: DB_HOST
|
server: API_HOST
|
||||||
},
|
},
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
} finally {
|
|
||||||
if (client) {
|
|
||||||
client.release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user