news widget fix db 4
This commit is contained in:
parent
4cc30437a5
commit
654cc641e2
@ -1,39 +1,7 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { Pool } from 'pg';
|
||||
|
||||
// Log database configuration for debugging
|
||||
console.log('Database configuration:', {
|
||||
host: process.env.DB_HOST,
|
||||
port: process.env.DB_PORT,
|
||||
database: process.env.DB_NAME,
|
||||
user: process.env.DB_USER,
|
||||
// Don't log the actual password
|
||||
hasPassword: !!process.env.DB_PASSWORD
|
||||
});
|
||||
|
||||
// PostgreSQL connection configuration using environment variables
|
||||
const pool = new Pool({
|
||||
host: process.env.DB_HOST,
|
||||
port: parseInt(process.env.DB_PORT || '5432'),
|
||||
database: process.env.DB_NAME,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
connectionTimeoutMillis: 5000,
|
||||
query_timeout: 5000,
|
||||
ssl: false
|
||||
});
|
||||
|
||||
// Helper function to clean HTML content
|
||||
function cleanHtmlContent(content: string): string {
|
||||
if (!content) return '';
|
||||
return content
|
||||
.replace(/<[^>]*>/g, '')
|
||||
.replace(/ /g, ' ')
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.trim();
|
||||
}
|
||||
// FastAPI server configuration
|
||||
const API_URL = 'http://172.16.0.104:8000';
|
||||
|
||||
// Helper function to format time
|
||||
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
||||
@ -91,16 +59,23 @@ function formatSource(source: string): string {
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
let client;
|
||||
try {
|
||||
client = await pool.connect();
|
||||
console.log('Connected to PostgreSQL database');
|
||||
console.log('Fetching news from FastAPI server...');
|
||||
|
||||
const response = await fetch(`${API_URL}/news?limit=10`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
});
|
||||
|
||||
const result = await client.query(
|
||||
'SELECT * FROM news ORDER BY date DESC LIMIT 10'
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const formattedNews = result.rows.map((article: any) => {
|
||||
const articles = await response.json();
|
||||
|
||||
const formattedNews = articles.map((article: any) => {
|
||||
const { displayDate, timestamp } = formatDateTime(article.date);
|
||||
return {
|
||||
id: article.id,
|
||||
@ -118,10 +93,9 @@ export async function GET() {
|
||||
return NextResponse.json(formattedNews);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database error:', {
|
||||
console.error('API error:', {
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
host: process.env.DB_HOST,
|
||||
database: process.env.DB_NAME
|
||||
server: API_URL
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
@ -131,9 +105,5 @@ export async function GET() {
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
} finally {
|
||||
if (client) {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user