'use client' import { useState, useEffect } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import { Button } from '@/components/ui/button' import Link from 'next/link' import { apiFetch } from '@/lib/config' interface Endpoint { id: number; name: string; url: string; description?: string; is_active: boolean; show_on_homepage: boolean; sort_order: number; } interface SystemMetrics { uptime: number; // 纳秒 start_time: string; num_cpu: number; num_goroutine: number; average_latency: number; memory_stats: { heap_alloc: number; heap_sys: number; }; } async function getHomePageConfig() { try { const res = await apiFetch('/api/home-config') if (!res.ok) { throw new Error('Failed to fetch home page config') } const data = await res.json() return data.data?.content || '# 欢迎使用随机API服务\n\n服务正在启动中...' } catch (error) { console.error('Error fetching home page config:', error) return '# 欢迎使用随机API服务\n\n这是一个可配置的随机API服务。' } } async function getStats() { try { const res = await apiFetch('/api/stats') if (!res.ok) { throw new Error('Failed to fetch stats') } return await res.json() } catch (error) { console.error('Error fetching stats:', error) return {} } } async function getURLStats() { try { const res = await apiFetch('/api/urlstats') if (!res.ok) { throw new Error('Failed to fetch URL stats') } return await res.json() } catch (error) { console.error('Error fetching URL stats:', error) return {} } } async function getSystemMetrics(): Promise { try { const res = await apiFetch('/api/metrics') if (!res.ok) { throw new Error('Failed to fetch system metrics') } return await res.json() } catch (error) { console.error('Error fetching system metrics:', error) return null } } async function getEndpoints() { try { const res = await apiFetch('/api/endpoints') if (!res.ok) { throw new Error('Failed to fetch endpoints') } const data = await res.json() return data.data || [] } catch (error) { console.error('Error fetching endpoints:', error) return [] } } function formatUptime(uptimeNs: number): string { const uptimeMs = uptimeNs / 1000000; // 纳秒转毫秒 const days = Math.floor(uptimeMs / (1000 * 60 * 60 * 24)); const hours = Math.floor((uptimeMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((uptimeMs % (1000 * 60 * 60)) / (1000 * 60)); if (days > 0) { return `${days}天 ${hours}小时 ${minutes}分钟`; } else if (hours > 0) { return `${hours}小时 ${minutes}分钟`; } else { return `${minutes}分钟`; } } function formatBytes(bytes: number): string { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } function formatStartTime(startTime: string): string { const date = new Date(startTime); return date.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }); } // 复制到剪贴板的函数 function copyToClipboard(text: string) { if (navigator.clipboard && window.isSecureContext) { return navigator.clipboard.writeText(text); } else { // 降级方案 - 抑制弃用警告 const textArea = document.createElement('textarea'); textArea.value = text; textArea.style.position = 'fixed'; textArea.style.left = '-999999px'; textArea.style.top = '-999999px'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); return new Promise((resolve, reject) => { try { const success = document.execCommand('copy'); if (success) { resolve(); } else { reject(new Error('Copy command failed')); } } catch (err) { reject(err); } finally { textArea.remove(); } }); } } export default function Home() { const [content, setContent] = useState('') const [stats, setStats] = useState<{Stats?: Record}>({}) const [urlStats, setUrlStats] = useState>({}) const [systemMetrics, setSystemMetrics] = useState(null) const [endpoints, setEndpoints] = useState([]) const [copiedUrl, setCopiedUrl] = useState(null) useEffect(() => { const loadData = async () => { const [contentData, statsData, urlStatsData, systemMetricsData, endpointsData] = await Promise.all([ getHomePageConfig(), getStats(), getURLStats(), getSystemMetrics(), getEndpoints() ]) setContent(contentData) setStats(statsData) setUrlStats(urlStatsData) setSystemMetrics(systemMetricsData) setEndpoints(endpointsData) } loadData() }, []) // 过滤出首页可见的端点 const visibleEndpoints = endpoints.filter((endpoint: Endpoint) => endpoint.is_active && endpoint.show_on_homepage ) const handleCopyUrl = async (endpoint: Endpoint) => { const fullUrl = `${window.location.origin}/${endpoint.url}` try { await copyToClipboard(fullUrl) setCopiedUrl(endpoint.url) setTimeout(() => setCopiedUrl(null), 2000) // 2秒后清除复制状态 } catch (err) { console.error('复制失败:', err) } } return (
{/* 背景遮罩 - 让背景图若影若现 */}
{/* 简洁背景 */}
{/* Header - 简洁 */}

Random API Service

随机API服务

{/* System Status Section - 冷淡风格 */} {systemMetrics && (

系统状态

{/* 运行时间 */}

运行时间

{formatUptime(systemMetrics.uptime)}

{formatStartTime(systemMetrics.start_time)}

{/* CPU核心数 */}

CPU核心

{systemMetrics.num_cpu}

核心

{/* Goroutine数量 */}

协程数

{systemMetrics.num_goroutine}

{/* 平均延迟 */}

平均延迟

{systemMetrics.average_latency.toFixed(2)}

毫秒

{/* 堆内存分配 */}

堆内存

{formatBytes(systemMetrics.memory_stats.heap_alloc)}

系统: {formatBytes(systemMetrics.memory_stats.heap_sys)}

)} {/* API端点统计 - 简洁风格 */} {visibleEndpoints.length > 0 && (

API 端点

{visibleEndpoints.map((endpoint: Endpoint) => { const endpointStats = stats.Stats?.[endpoint.url] || { TotalCalls: 0, TodayCalls: 0 } const urlCount = urlStats[endpoint.url]?.total_urls || 0 return (

{endpoint.name}

/{endpoint.url}

今日

{endpointStats.TodayCalls}

总计

{endpointStats.TotalCalls}

URL

{urlCount}

{endpoint.description && (

{endpoint.description}

)}
) })}
)} {/* Main Content - 简洁内容区 */}

{children}

, h2: ({children}) =>

{children}

, h3: ({children}) =>

{children}

, p: ({children}) =>

{children}

, ul: ({children}) =>
    {children}
, ol: ({children}) =>
    {children}
, li: ({children}) =>
  • {children}
  • , strong: ({children}) => {children}, em: ({children}) => {children}, code: ({children}) => {children}, pre: ({children}) =>
    {children}
    , blockquote: ({children}) =>
    {children}
    , a: ({href, children}) => {children}, }} > {content}
    {/* Footer - 包含管理后台链接 */}

    随机API服务 - 基于 Next.js 和 Go 构建

    GitHub | 管理后台

    ) }