2026-06-09 16:36:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 入口文件 — 对应 Python: main.py
|
|
|
|
|
|
*/
|
2026-06-10 15:37:20 +08:00
|
|
|
|
require('./config/loadEnv');
|
2026-06-09 16:36:23 +08:00
|
|
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const cors = require('cors');
|
|
|
|
|
|
const sequelize = require('./config/database');
|
|
|
|
|
|
const logger = require('./config/logger');
|
|
|
|
|
|
const routes = require('./routes');
|
|
|
|
|
|
const { bodyLimit } = require('./middleware/bodyLimit');
|
|
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
|
|
|
|
// 中间件
|
|
|
|
|
|
app.use(cors({ origin: '*', credentials: true }));
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
|
app.use(bodyLimit);
|
|
|
|
|
|
|
|
|
|
|
|
// 根路径(无需登录)
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
message: '欢迎使用 Audio Dashboard API',
|
|
|
|
|
|
docs: '/docs',
|
|
|
|
|
|
redoc: '/redoc',
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 健康检查(无需登录)
|
|
|
|
|
|
app.get('/health', (req, res) => {
|
|
|
|
|
|
res.json({ status: 'healthy' });
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 业务路由(需登录的路由在各自文件中挂载 authMiddleware)
|
|
|
|
|
|
routes.forEach((r) => app.use(r));
|
|
|
|
|
|
|
|
|
|
|
|
// 同步数据库并启动
|
|
|
|
|
|
const PORT = process.env.PORT || 8083;
|
|
|
|
|
|
|
|
|
|
|
|
async function start() {
|
|
|
|
|
|
try {
|
|
|
|
|
|
logger.info('Creating database tables...');
|
|
|
|
|
|
await sequelize.sync();
|
|
|
|
|
|
logger.info('Database tables created successfully');
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
logger.error(`Warning: Could not create tables: ${e.message}`);
|
|
|
|
|
|
logger.error('Continuing without table creation...');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(PORT, '0.0.0.0', () => {
|
|
|
|
|
|
logger.info(`Server running on http://localhost:${PORT}`);
|
|
|
|
|
|
logger.info(`API docs: http://localhost:${PORT}/docs`);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
start();
|