Files
dashboard/backend/src/app.js
T
2026-07-24 15:12:15 +08:00

63 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 入口文件 — 对应 Python: main.py
*/
// 统一时区为东八区(服务器可能在其他时区,但业务面向中国用户)
process.env.TZ = 'Asia/Shanghai';
require('./config/loadEnv');
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 { ensureBootstrapSuperAdmin } = require('./services/userBootstrap');
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();
await ensureBootstrapSuperAdmin();
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();