refactor(server): 更新服务器配置和部署脚本- 更改默认端口为3350,以适应生产环境。
- 增加了API信息端点,提供更详细的API状态信息。
- 提高了速率限制,以适应生产环境的更高请求量。
- 添加了错误处理中间件和404处理,增强了错误处理能力。
- 添加了优雅关机处理,确保服务器在接收到SIGINT或SIGTERM信号时能够优雅关闭。- 创建了生产环境配置文件示例 `.env.production.example`,并提供了详细的部署指南 `DEPLOYMENT_GUIDE.md`。
- 添加了启动脚本 `start-server.sh` 和同步脚本 `sync-to-server.sh`,简化了部署流程。
- 配置了Nginx配置文件 `xlxumu-api.conf`,支持HTTPS和反向代理。
```
This commit is contained in:
2025-09-11 17:11:12 +08:00
parent 216cf80eab
commit bd379d505c
6 changed files with 597 additions and 7 deletions

View File

@@ -0,0 +1,51 @@
# 锡林郭勒盟智慧养殖平台 - 生产环境配置
# 复制此文件为 .env 并修改实际值
# 环境配置
NODE_ENV=production
PORT=3350
# MySQL数据库配置
DB_HOST=your-mysql-host
DB_PORT=3306
DB_USER=your-mysql-user
DB_PASSWORD=your-mysql-password
DB_NAME=xlxumu_production
# JWT密钥配置
JWT_SECRET=your-super-secure-jwt-secret-key-at-least-32-characters
# API配置
API_PREFIX=/api
API_VERSION=v1
# 跨域配置
CORS_ORIGIN=https://xlapi.jiebanke.com
# 日志配置
LOG_LEVEL=info
LOG_FILE=/var/log/xlxumu-api.log
# 文件上传配置
UPLOAD_MAX_SIZE=10mb
UPLOAD_PATH=/data/uploads
# 监控配置
METRICS_ENABLED=true
METRICS_PORT=9090
# 缓存配置(可选)
CACHE_ENABLED=false
CACHE_TTL=300000
# 邮件配置(可选)
SMTP_HOST=smtp.your-email-provider.com
SMTP_PORT=587
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-password
# 第三方服务配置(根据需要添加)
# ALIYUN_ACCESS_KEY=your-aliyun-access-key
# ALIYUN_ACCESS_SECRET=your-aliyun-access-secret
# WECHAT_APP_ID=your-wechat-app-id
# WECHAT_APP_SECRET=your-wechat-app-secret

View File

@@ -9,7 +9,7 @@ dotenv.config();
// 创建Express应用
const app = express();
const PORT = process.env.PORT || 8000;
const PORT = process.env.PORT || 3350; // 生产环境使用3350端口
// 中间件
app.use(helmet()); // 安全头部
@@ -20,7 +20,7 @@ app.use(express.urlencoded({ extended: true, limit: '10mb' })); // URL编码解
// 速率限制
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15分钟
max: 100, // 限制每个IP 15分钟内最多100个请求
max: 1000, // 生产环境提高限制
message: '请求过于频繁,请稍后再试'
});
app.use(limiter);
@@ -30,14 +30,30 @@ app.get('/', (req, res) => {
res.json({
message: '欢迎使用锡林郭勒盟地区智慧养殖产业平台API服务',
version: '1.0.0',
timestamp: new Date().toISOString()
environment: process.env.NODE_ENV || 'development',
timestamp: new Date().toISOString(),
docs: 'https://xlapi.jiebanke.com/docs'
});
});
app.get('/health', (req, res) => {
res.json({
status: 'OK',
timestamp: new Date().toISOString()
timestamp: new Date().toISOString(),
uptime: process.uptime(),
memory: process.memoryUsage()
});
});
// API信息端点
app.get('/api/info', (req, res) => {
res.json({
name: 'xlxumu-api',
version: '1.0.0',
environment: process.env.NODE_ENV,
port: PORT,
node_version: process.version,
platform: process.platform
});
});
@@ -152,9 +168,46 @@ app.get('/api/v1/dashboard/map/region/:regionId', (req, res) => {
}
});
// 启动服务器
app.listen(PORT, () => {
console.log(`API服务器正在端口 ${PORT} 上运行`);
// 错误处理中间件
app.use((err, req, res, next) => {
console.error('服务器错误:', err.stack);
res.status(500).json({
error: '内部服务器错误',
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong!'
});
});
// 404处理
app.use((req, res) => {
res.status(404).json({
error: '接口未找到',
path: req.path,
method: req.method
});
});
// 优雅关机处理
process.on('SIGINT', () => {
console.log('\n收到SIGINT信号正在优雅关闭服务器...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n收到SIGTERM信号正在优雅关闭服务器...');
process.exit(0);
});
// 启动服务器
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 API服务器正在运行:`);
console.log(` 📍 本地: http://localhost:${PORT}`);
console.log(` 🌐 网络: http://0.0.0.0:${PORT}`);
console.log(` 🏷️ 环境: ${process.env.NODE_ENV || 'development'}`);
console.log(` ⏰ 启动时间: ${new Date().toLocaleString()}`);
});
// 设置超时
server.timeout = 60000;
server.keepAliveTimeout = 5000;
module.exports = app;