133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
// FastAPI server configuration - use localhost since both servers are on the same machine
|
|
const API_HOST = process.env.API_HOST || 'http://localhost:8000';
|
|
|
|
// 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
|
|
function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } {
|
|
try {
|
|
const date = new Date(dateStr);
|
|
const day = date.getDate();
|
|
const month = date.toLocaleString('fr-FR', { month: 'short' }).toLowerCase();
|
|
|
|
return {
|
|
displayDate: `${day} ${month}`,
|
|
timestamp: date.toLocaleString('fr-FR', {
|
|
day: '2-digit',
|
|
month: 'short',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
hour12: false
|
|
})
|
|
};
|
|
} catch (error) {
|
|
return { displayDate: 'N/A', timestamp: 'N/A' };
|
|
}
|
|
}
|
|
|
|
// Helper function to truncate text
|
|
function truncateText(text: string, maxLength: number): string {
|
|
if (!text || text.length <= maxLength) return text;
|
|
const truncated = text.substring(0, maxLength).trim();
|
|
return truncated.replace(/[.,!?]$/, '') + '...';
|
|
}
|
|
|
|
// Helper function to format category
|
|
function formatCategory(category: string): string {
|
|
if (!category) return 'GENERAL';
|
|
// Simplify long category names
|
|
return category
|
|
.split('-')
|
|
.map(part => part.trim().toUpperCase())
|
|
.join(' - ');
|
|
}
|
|
|
|
// 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]
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9]/g, ' ')
|
|
.trim();
|
|
return sourceName.charAt(0).toUpperCase() + sourceName.slice(1);
|
|
}
|
|
|
|
export async function GET() {
|
|
try {
|
|
console.log(`Attempting to connect to FastAPI server at ${API_HOST}...`);
|
|
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
|
|
const response = await fetch(`${API_HOST}/news?limit=10`, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
signal: controller.signal,
|
|
keepalive: true,
|
|
});
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
throw new Error(`HTTP error! status: ${response.status}, body: ${errorText}`);
|
|
}
|
|
|
|
const rawNews = await response.json();
|
|
|
|
// Format and clean the news data
|
|
const formattedNews = rawNews.map((article: any) => {
|
|
const { displayDate, timestamp } = formatDateTime(article.date);
|
|
return {
|
|
id: article.id,
|
|
title: truncateText(cleanHtmlContent(article.title), 100),
|
|
description: truncateText(cleanHtmlContent(article.description), 150),
|
|
displayDate,
|
|
timestamp,
|
|
source: formatSource(article.source),
|
|
category: formatCategory(article.category),
|
|
url: article.url || '#',
|
|
};
|
|
});
|
|
|
|
console.log(`Successfully fetched and formatted ${formattedNews.length} news articles`);
|
|
|
|
return NextResponse.json(formattedNews);
|
|
} catch (error) {
|
|
console.error('Error in news API:', {
|
|
error: error instanceof Error ? error.message : 'Unknown error',
|
|
apiHost: API_HOST,
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
timestamp: new Date().toISOString(),
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{
|
|
error: 'Failed to fetch news',
|
|
details: error instanceof Error ? error.message : 'Unknown error',
|
|
server: API_HOST,
|
|
timestamp: new Date().toISOString(),
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|