news widget fix db 4
This commit is contained in:
parent
4cc30437a5
commit
654cc641e2
@ -1,39 +1,7 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { Pool } from 'pg';
|
|
||||||
|
|
||||||
// Log database configuration for debugging
|
// FastAPI server configuration
|
||||||
console.log('Database configuration:', {
|
const API_URL = 'http://172.16.0.104:8000';
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function to format time
|
// Helper function to format time
|
||||||
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
||||||
@ -91,16 +59,23 @@ function formatSource(source: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
let client;
|
|
||||||
try {
|
try {
|
||||||
client = await pool.connect();
|
console.log('Fetching news from FastAPI server...');
|
||||||
console.log('Connected to PostgreSQL database');
|
|
||||||
|
const response = await fetch(`${API_URL}/news?limit=10`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const result = await client.query(
|
if (!response.ok) {
|
||||||
'SELECT * FROM news ORDER BY date DESC LIMIT 10'
|
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);
|
const { displayDate, timestamp } = formatDateTime(article.date);
|
||||||
return {
|
return {
|
||||||
id: article.id,
|
id: article.id,
|
||||||
@ -118,10 +93,9 @@ export async function GET() {
|
|||||||
return NextResponse.json(formattedNews);
|
return NextResponse.json(formattedNews);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Database error:', {
|
console.error('API error:', {
|
||||||
error: error instanceof Error ? error.message : 'Unknown error',
|
error: error instanceof Error ? error.message : 'Unknown error',
|
||||||
host: process.env.DB_HOST,
|
server: API_URL
|
||||||
database: process.env.DB_NAME
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@ -131,9 +105,5 @@ export async function GET() {
|
|||||||
},
|
},
|
||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
} finally {
|
|
||||||
if (client) {
|
|
||||||
client.release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user