Skip to content

Commit 2b61c0b

Browse files
author
yangtao
committed
feat(api): 添加代理请求功能并优化日志记录
- 新增代理请求处理函数,支持自签名证书的 HTTPS 请求 - 添加日志工具类,实现不同级别的日志输出 - 在代理请求中使用日志工具记录请求和错误信息 - 移除仪表盘 SSE 监听相关代码 - 在 Tunnel 表中添加 lastEventTime 字段并创建索引
1 parent f07ca2d commit 2b61c0b

File tree

4 files changed

+113
-18
lines changed

4 files changed

+113
-18
lines changed

app/api/proxy/route.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { NextRequest, NextResponse } from 'next/server';
2+
import { logger } from '@/lib/logger';
3+
import { fetchWithSSLSupport } from '@/lib/utils/fetch';
4+
5+
/**
6+
* 代理请求处理函数
7+
*/
8+
export async function POST(request: NextRequest) {
9+
try {
10+
// 获取请求数据
11+
const data = await request.json();
12+
const { url, method = 'GET', headers = {}, body } = data;
13+
14+
if (!url) {
15+
return NextResponse.json({ error: '缺少必要的URL参数' }, { status: 400 });
16+
}
17+
18+
// 记录代理请求信息
19+
logger.info('[代理请求]', method, url, {
20+
headers,
21+
'数据': body || '无'
22+
});
23+
24+
// 准备请求选项
25+
const fetchOptions: RequestInit = {
26+
method,
27+
headers: {
28+
'Content-Type': 'application/json',
29+
...headers
30+
}
31+
};
32+
33+
// 如果有请求体,添加到选项中
34+
if (body) {
35+
fetchOptions.body = JSON.stringify(body);
36+
}
37+
38+
// 使用支持自签名证书的fetch发送请求
39+
const response = await fetchWithSSLSupport(url, fetchOptions);
40+
41+
// 获取响应数据
42+
const responseData = await response.text();
43+
44+
// 返回响应
45+
return new NextResponse(responseData, {
46+
status: response.status,
47+
headers: {
48+
'Content-Type': response.headers.get('Content-Type') || 'application/json'
49+
}
50+
});
51+
52+
} catch (error) {
53+
// 记录错误
54+
logger.error('[代理请求错误]', error);
55+
56+
// 返回错误响应
57+
return NextResponse.json({
58+
error: '代理请求失败',
59+
message: error instanceof Error ? error.message : '未知错误'
60+
}, { status: 500 });
61+
}
62+
}

app/bk/page.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Icon } from "@iconify/react";
2929
import { FlowTrafficChart } from "@/components/ui/flow-traffic-chart";
3030
import { useRouter } from "next/navigation";
3131
import { EndpointStatus } from '@prisma/client';
32-
import { useGlobalSSE, useDashboardSSE } from '@/lib/hooks/use-sse';
32+
import { useGlobalSSE } from '@/lib/hooks/use-sse';
3333
import { motion } from 'framer-motion';
3434
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3535
import {
@@ -300,23 +300,6 @@ export default function DashboardPage() {
300300
}
301301
}
302302
});
303-
304-
// 使用仪表盘SSE监听流量趋势更新
305-
useDashboardSSE({
306-
onConnected: () => {
307-
console.log('仪表盘SSE连接成功');
308-
},
309-
onMessage: (data) => {
310-
if (data.type === 'dashboard_update') {
311-
// 处理流量趋势更新
312-
console.log('收到仪表盘更新:', data);
313-
// 这里可以更新UI状态
314-
}
315-
},
316-
onError: (error) => {
317-
console.error('仪表盘SSE错误:', error);
318-
}
319-
});
320303

321304
// 初始化数据
322305
useEffect(() => {

lib/logger.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 日志级别枚举
3+
*/
4+
export enum LogLevel {
5+
DEBUG = 'DEBUG',
6+
INFO = 'INFO',
7+
WARN = 'WARN',
8+
ERROR = 'ERROR'
9+
}
10+
11+
/**
12+
* 日志工具类
13+
*/
14+
class Logger {
15+
/**
16+
* 输出调试日志
17+
*/
18+
debug(...args: any[]) {
19+
console.debug(`[${new Date().toISOString()}] [DEBUG]`, ...args);
20+
}
21+
22+
/**
23+
* 输出信息日志
24+
*/
25+
info(...args: any[]) {
26+
console.info(`[${new Date().toISOString()}] [INFO]`, ...args);
27+
}
28+
29+
/**
30+
* 输出警告日志
31+
*/
32+
warn(...args: any[]) {
33+
console.warn(`[${new Date().toISOString()}] [WARN]`, ...args);
34+
}
35+
36+
/**
37+
* 输出错误日志
38+
*/
39+
error(...args: any[]) {
40+
console.error(`[${new Date().toISOString()}] [ERROR]`, ...args);
41+
}
42+
}
43+
44+
// 导出单例实例
45+
export const logger = new Logger();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- AlterTable
2+
ALTER TABLE "Tunnel" ADD COLUMN "lastEventTime" DATETIME;
3+
4+
-- CreateIndex
5+
CREATE INDEX "Tunnel_lastEventTime_idx" ON "Tunnel"("lastEventTime");

0 commit comments

Comments
 (0)