news widget fix db 3

This commit is contained in:
alma 2025-04-15 12:19:44 +02:00
parent a30e4fa107
commit 4cc30437a5

View File

@ -1,14 +1,26 @@
import { NextResponse } from 'next/server';
import { Pool } from 'pg';
// PostgreSQL connection configuration
// 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: '172.16.0.104',
port: 5432,
database: 'rivacube',
user: 'alma',
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
query_timeout: 5000,
ssl: false
});
// Helper function to clean HTML content
@ -31,14 +43,14 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri
const month = date.toLocaleString('fr-FR', { month: 'short' }).toLowerCase();
return {
displayDate: `${day} ${month}.`, // Added dot for better styling
displayDate: `${day} ${month}.`,
timestamp: date.toLocaleString('fr-FR', {
day: '2-digit',
month: 'short',
hour: '2-digit',
minute: '2-digit',
hour12: false
}).replace(',', ' à') // Format: "14 avr. à 15:30"
}).replace(',', ' à')
};
} catch (error) {
return { displayDate: 'N/A', timestamp: 'N/A' };
@ -48,7 +60,6 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri
// Helper function to truncate text
function truncateText(text: string, maxLength: number): string {
if (!text || text.length <= maxLength) return text;
// Find the last space before maxLength to avoid cutting words
const lastSpace = text.lastIndexOf(' ', maxLength);
const truncated = text.substring(0, lastSpace > 0 ? lastSpace : maxLength).trim();
return truncated.replace(/[.,!?]$/, '') + '...';
@ -57,7 +68,6 @@ function truncateText(text: string, maxLength: number): string {
// Helper function to format category
function formatCategory(category: string): string {
if (!category) return 'GENERAL';
// Make category names shorter and more readable
const categoryMap: { [key: string]: string } = {
'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD',
'UN NEWS - GLOBAL NEWS': 'UN NEWS',
@ -71,7 +81,6 @@ function formatCategory(category: string): string {
// Helper function to format source
function formatSource(source: string): string {
if (!source) return '';
// Extract domain name without TLD and clean it up
const sourceName = source
.replace(/^(https?:\/\/)?(www\.)?/i, '')
.split('.')[0]
@ -91,7 +100,7 @@ export async function GET() {
'SELECT * FROM news ORDER BY date DESC LIMIT 10'
);
const formattedNews = result.rows.map(article => {
const formattedNews = result.rows.map((article: any) => {
const { displayDate, timestamp } = formatDateTime(article.date);
return {
id: article.id,
@ -111,6 +120,8 @@ export async function GET() {
} catch (error) {
console.error('Database error:', {
error: error instanceof Error ? error.message : 'Unknown error',
host: process.env.DB_HOST,
database: process.env.DB_NAME
});
return NextResponse.json(