✨ Features: - Analytics Dashboard with real-time metrics - Redis caching for performance optimization - Import/Export functionality for projects - Complete admin system with security - Production-ready Docker setup 🔧 Technical: - Removed Ghost CMS dependencies - Added Redis container with caching - Implemented API response caching - Enhanced admin interface with analytics - Optimized for dk0.dev domain 🛡️ Security: - Admin authentication with Basic Auth - Protected analytics endpoints - Secure environment configuration 📊 Analytics: - Performance metrics dashboard - Project statistics visualization - Real-time data with caching - Umami integration for GDPR compliance 🎯 Production Ready: - Multi-container Docker setup - Health checks for all services - Automatic restart policies - Resource limits configured - Ready for Nginx Proxy Manager
87 lines
3.2 KiB
TypeScript
87 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { projectService } from '@/lib/prisma';
|
|
import { analyticsCache } from '@/lib/redis';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
// Check admin authentication
|
|
const authHeader = request.headers.get('authorization');
|
|
const basicAuth = process.env.ADMIN_BASIC_AUTH;
|
|
|
|
if (!basicAuth) {
|
|
return new NextResponse('Admin access not configured', { status: 500 });
|
|
}
|
|
|
|
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
|
return new NextResponse('Authentication required', { status: 401 });
|
|
}
|
|
|
|
const credentials = authHeader.split(' ')[1];
|
|
const [username, password] = Buffer.from(credentials, 'base64').toString().split(':');
|
|
const [expectedUsername, expectedPassword] = basicAuth.split(':');
|
|
|
|
if (username !== expectedUsername || password !== expectedPassword) {
|
|
return new NextResponse('Invalid credentials', { status: 401 });
|
|
}
|
|
|
|
// Check cache first
|
|
const cachedStats = await analyticsCache.getOverallStats();
|
|
if (cachedStats) {
|
|
return NextResponse.json(cachedStats);
|
|
}
|
|
|
|
// Get analytics data
|
|
const projectsResult = await projectService.getAllProjects();
|
|
const projects = projectsResult.projects || projectsResult;
|
|
const performanceStats = await projectService.getPerformanceStats();
|
|
|
|
// Calculate analytics metrics
|
|
const analytics = {
|
|
overview: {
|
|
totalProjects: projects.length,
|
|
publishedProjects: projects.filter(p => p.published).length,
|
|
featuredProjects: projects.filter(p => p.featured).length,
|
|
totalViews: projects.reduce((sum, p) => sum + ((p.analytics as any)?.views || 0), 0),
|
|
totalLikes: projects.reduce((sum, p) => sum + ((p.analytics as any)?.likes || 0), 0),
|
|
totalShares: projects.reduce((sum, p) => sum + ((p.analytics as any)?.shares || 0), 0),
|
|
avgLighthouse: projects.length > 0
|
|
? Math.round(projects.reduce((sum, p) => sum + ((p.performance as any)?.lighthouse || 0), 0) / projects.length)
|
|
: 0
|
|
},
|
|
projects: projects.map(project => ({
|
|
id: project.id,
|
|
title: project.title,
|
|
category: project.category,
|
|
difficulty: project.difficulty,
|
|
views: (project.analytics as any)?.views || 0,
|
|
likes: (project.analytics as any)?.likes || 0,
|
|
shares: (project.analytics as any)?.shares || 0,
|
|
lighthouse: (project.performance as any)?.lighthouse || 0,
|
|
published: project.published,
|
|
featured: project.featured,
|
|
createdAt: project.createdAt,
|
|
updatedAt: project.updatedAt
|
|
})),
|
|
categories: performanceStats.byCategory,
|
|
difficulties: performanceStats.byDifficulty,
|
|
performance: {
|
|
avgLighthouse: performanceStats.avgLighthouse,
|
|
totalViews: performanceStats.totalViews,
|
|
totalLikes: performanceStats.totalLikes,
|
|
totalShares: performanceStats.totalShares
|
|
}
|
|
};
|
|
|
|
// Cache the results
|
|
await analyticsCache.setOverallStats(analytics);
|
|
|
|
return NextResponse.json(analytics);
|
|
} catch (error) {
|
|
console.error('Analytics dashboard error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch analytics data' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|