96 lines
2.6 KiB
TypeScript
96 lines
2.6 KiB
TypeScript
import 'express-async-errors';
|
||
import express from 'express';
|
||
import cors from 'cors';
|
||
import helmet from 'helmet';
|
||
import rateLimit from 'express-rate-limit';
|
||
import dotenv from 'dotenv';
|
||
import { logger } from './utils/logger';
|
||
import { errorHandler } from './middleware/errorHandler';
|
||
import { notFoundHandler } from './middleware/notFoundHandler';
|
||
import authRoutes from './routes/auth';
|
||
import userRoutes from './routes/users';
|
||
import accountRoutes from './routes/accounts';
|
||
import adminRoutes from './routes/admin';
|
||
import { connectDatabase } from './config/database';
|
||
|
||
// Load environment variables
|
||
dotenv.config();
|
||
|
||
const app = express();
|
||
const PORT = process.env.PORT || 3001;
|
||
|
||
// 信任代理,确保正确获取客户端IP地址
|
||
app.set('trust proxy', true);
|
||
|
||
// Security middleware
|
||
app.use(helmet());
|
||
app.use(cors({
|
||
origin: process.env.NODE_ENV === 'production'
|
||
? ['http://frontend:3000',"http://backend:3001"]
|
||
: ['http://localhost:3000', 'http://localhost:5173'],
|
||
credentials: true
|
||
}));
|
||
|
||
// Rate limiting
|
||
const limiter = rateLimit({
|
||
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || '900000'), // 15 minutes
|
||
max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '100'), // limit each IP to 100 requests per windowMs
|
||
message: {
|
||
error: 'Too many requests from this IP, please try again later.'
|
||
}
|
||
});
|
||
app.use('/api/', limiter);
|
||
|
||
// Body parsing middleware
|
||
app.use(express.json({ limit: '10mb' }));
|
||
app.use(express.urlencoded({ extended: true }));
|
||
|
||
// Request logging
|
||
app.use((req, res, next) => {
|
||
logger.info(`${req.method} ${req.path}`, {
|
||
ip: (req.headers['x-forwarded-for'] as string) || req.socket.remoteAddress,
|
||
userAgent: req.get('User-Agent')
|
||
});
|
||
next();
|
||
});
|
||
|
||
// API routes
|
||
app.use('/api/auth', authRoutes);
|
||
app.use('/api/users', userRoutes);
|
||
app.use('/api/accounts', accountRoutes);
|
||
app.use('/api/admin', adminRoutes);
|
||
|
||
// Error handling middleware
|
||
app.use(notFoundHandler);
|
||
app.use(errorHandler);
|
||
|
||
// Start server
|
||
async function startServer() {
|
||
try {
|
||
// Connect to database
|
||
await connectDatabase();
|
||
logger.info('Database connected successfully');
|
||
|
||
// Start server
|
||
app.listen(PORT, () => {
|
||
logger.info(`Server running on port ${PORT}`);
|
||
logger.info(`Environment: ${process.env.NODE_ENV}`);
|
||
});
|
||
} catch (error) {
|
||
logger.error('Failed to start server:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// Handle graceful shutdown
|
||
process.on('SIGTERM', () => {
|
||
logger.info('SIGTERM received, shutting down gracefully');
|
||
process.exit(0);
|
||
});
|
||
|
||
process.on('SIGINT', () => {
|
||
logger.info('SIGINT received, shutting down gracefully');
|
||
process.exit(0);
|
||
});
|
||
|
||
startServer();
|