- database.ts 改为按 DB_TYPE 切换 SQLite / MariaDB(mysql2),sqlite3 按需动态导入 - index.ts 改为初始化数据库后再监听端口 - 新增 AuthService / HealthService / AnalysisService / GarminService - 新增 .gitignore、server/.env(忽略) 与 .env.example - README 增加数据库配置说明;package.json 增加 mysql2/@types
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import { initializeDatabase } from './utils/database';
|
|
import authRoutes from './routes/auth';
|
|
import garminRoutes from './routes/garmin';
|
|
import healthRoutes from './routes/health';
|
|
import analysisRoutes from './routes/analysis';
|
|
import { errorHandler } from './middleware/errorHandler';
|
|
|
|
dotenv.config();
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 5000;
|
|
|
|
// Middleware
|
|
app.use(express.json());
|
|
app.use(cors({
|
|
origin: process.env.CORS_ORIGIN || 'http://localhost:3000',
|
|
credentials: true
|
|
}));
|
|
|
|
// Routes
|
|
app.use('/api/auth', authRoutes);
|
|
app.use('/api/garmin', garminRoutes);
|
|
app.use('/api/health', healthRoutes);
|
|
app.use('/api/analysis', analysisRoutes);
|
|
|
|
// Health check
|
|
app.get('/api/health/status', (req, res) => {
|
|
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
|
});
|
|
|
|
// Error handling
|
|
app.use(errorHandler);
|
|
|
|
// Initialize database, then start accepting requests
|
|
initializeDatabase()
|
|
.then(() => {
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.error('[db] Failed to initialize database:', err);
|
|
process.exit(1);
|
|
});
|