File size: 3,744 Bytes
b7560a4 bb8a633 b7560a4 bb8a633 2f630e2 40dd9a0 b7560a4 40dd9a0 b7560a4 40dd9a0 bb8a633 b7560a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import authRoutes from './routes/auth.js';
import pptRoutes from './routes/ppt.js';
import publicRoutes from './routes/public.js';
import { authenticateToken } from './middleware/auth.js';
import { errorHandler } from './middleware/errorHandler.js';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 7860; // 修改为7860端口
// 安全中间件
app.use(helmet({
contentSecurityPolicy: false, // 为了兼容前端静态文件
}));
// 限流中间件
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15分钟
max: 100, // 每个IP每15分钟最多100个请求
message: 'Too many requests from this IP, please try again later.'
});
app.use('/api', limiter);
// CORS配置
app.use(cors({
origin: process.env.FRONTEND_URL || '*',
credentials: true
}));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// 提供前端静态文件
app.use(express.static(path.join(__dirname, '../../frontend/dist')));
// 提供数据文件
app.use('/data', express.static(path.join(__dirname, '../../frontend/public/mocks')));
// API路由
console.log('Registering API routes...');
// 健康检查 - 需要在其他路由之前
app.get('/api/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// GitHub连接状态检查
app.get('/api/github/status', async (req, res) => {
try {
const { default: githubService } = await import('./services/githubService.js');
const validation = await githubService.validateConnection();
res.json({
github: validation,
environment: {
tokenConfigured: !!process.env.GITHUB_TOKEN,
reposConfigured: !!process.env.GITHUB_REPOS,
nodeEnv: process.env.NODE_ENV
}
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// 添加路由注册日志
console.log('Importing route modules...');
console.log('Auth routes imported:', !!authRoutes);
console.log('PPT routes imported:', !!pptRoutes);
console.log('Public routes imported:', !!publicRoutes);
// 认证相关路由(不需要认证)
app.use('/api/auth', authRoutes);
// 公共访问路由(不需要认证)
app.use('/api/public', publicRoutes);
// 添加测试路由来验证 PPT 路由是否工作(不需要认证)
app.get('/api/ppt/test', (req, res) => {
res.json({ message: 'PPT routes are working', timestamp: new Date().toISOString() });
});
// PPT管理路由(需要认证)
app.use('/api/ppt', (req, res, next) => {
console.log(`PPT route accessed: ${req.method} ${req.path}`);
next();
}, authenticateToken, pptRoutes);
console.log('All routes registered successfully');
// 添加调试中间件 - 处理未匹配的API路由
app.use('/api/*', (req, res) => {
console.log(`Unmatched API route: ${req.method} ${req.path}`);
res.status(404).json({ error: 'API route not found', path: req.path });
});
// 前端路由处理 - 必须在API路由之后
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../../frontend/dist/index.html'));
});
// 错误处理中间件
app.use(errorHandler);
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
}); |