173 lines
4.9 KiB
JavaScript
173 lines
4.9 KiB
JavaScript
const express = require('express')
|
|
const cors = require('cors')
|
|
const helmet = require('helmet')
|
|
const morgan = require('morgan')
|
|
const rateLimit = require('express-rate-limit')
|
|
const compression = require('compression')
|
|
const swaggerJsdoc = require('swagger-jsdoc')
|
|
const swaggerUi = require('swagger-ui-express')
|
|
require('dotenv').config()
|
|
|
|
// 数据库连接
|
|
const { testConnection, syncModels } = require('./models')
|
|
|
|
const app = express()
|
|
|
|
// 中间件配置
|
|
app.use(helmet()) // 安全头
|
|
app.use(cors()) // 跨域
|
|
app.use(compression()) // 压缩
|
|
app.use(morgan('combined')) // 日志
|
|
app.use(express.json({ limit: '10mb' }))
|
|
app.use(express.urlencoded({ extended: true, limit: '10mb' }))
|
|
|
|
// Swagger 配置
|
|
const swaggerOptions = {
|
|
definition: {
|
|
openapi: '3.0.0',
|
|
info: {
|
|
title: '活牛采购智能数字化系统 API',
|
|
version: '1.0.0',
|
|
description: '活牛采购标准化操作流程系统接口文档',
|
|
contact: {
|
|
name: 'API支持',
|
|
email: 'support@niumall.com'
|
|
}
|
|
},
|
|
servers: [
|
|
{
|
|
url: 'http://localhost:4330/api',
|
|
description: '开发环境'
|
|
},
|
|
{
|
|
url: 'https://wapi.yunniushi.cn/api',
|
|
description: '生产环境'
|
|
}
|
|
],
|
|
components: {
|
|
securitySchemes: {
|
|
BearerAuth: {
|
|
type: 'http',
|
|
scheme: 'bearer',
|
|
bearerFormat: 'JWT'
|
|
}
|
|
},
|
|
schemas: {
|
|
ApiResponse: {
|
|
type: 'object',
|
|
properties: {
|
|
success: { type: 'boolean', description: '请求是否成功' },
|
|
message: { type: 'string', description: '提示信息' },
|
|
data: { type: 'object', description: '响应数据' },
|
|
timestamp: { type: 'string', format: 'date-time', description: '时间戳' }
|
|
}
|
|
},
|
|
PaginationParams: {
|
|
type: 'object',
|
|
properties: {
|
|
page: { type: 'integer', description: '当前页码' },
|
|
limit: { type: 'integer', description: '每页数量' },
|
|
sort: { type: 'string', description: '排序字段' },
|
|
order: { type: 'string', enum: ['asc', 'desc'], description: '排序方向' }
|
|
}
|
|
},
|
|
PaginatedResponse: {
|
|
type: 'object',
|
|
properties: {
|
|
items: { type: 'array', description: '数据列表' },
|
|
total: { type: 'integer', description: '总记录数' },
|
|
page: { type: 'integer', description: '当前页码' },
|
|
limit: { type: 'integer', description: '每页数量' },
|
|
totalPages: { type: 'integer', description: '总页数' }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
apis: ['./routes/*.js', './models/*.js'] // API路由文件路径
|
|
};
|
|
|
|
const swaggerSpec = swaggerJsdoc(swaggerOptions);
|
|
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
|
|
|
// 限流
|
|
const limiter = rateLimit({
|
|
windowMs: 15 * 60 * 1000, // 15 分钟
|
|
max: 100, // 限制每个IP最多100个请求
|
|
message: {
|
|
success: false,
|
|
message: '请求过于频繁,请稍后重试'
|
|
}
|
|
})
|
|
app.use('/api', limiter)
|
|
|
|
// 健康检查
|
|
app.get('/health', (req, res) => {
|
|
res.json({
|
|
success: true,
|
|
message: '服务运行正常',
|
|
timestamp: new Date().toISOString(),
|
|
version: process.env.npm_package_version || '1.0.0'
|
|
})
|
|
})
|
|
|
|
// API 路由
|
|
app.use('/api/auth', require('./routes/auth'))
|
|
app.use('/api/users', require('./routes/users'))
|
|
app.use('/api/orders', require('./routes/orders'))
|
|
app.use('/api/suppliers', require('./routes/suppliers'))
|
|
app.use('/api/transport', require('./routes/transport'))
|
|
app.use('/api/finance', require('./routes/finance'))
|
|
app.use('/api/quality', require('./routes/quality'))
|
|
|
|
// 404 处理
|
|
app.use((req, res) => {
|
|
res.status(404).json({
|
|
success: false,
|
|
message: '接口不存在',
|
|
path: req.path
|
|
})
|
|
})
|
|
|
|
// 错误处理中间件
|
|
app.use((err, req, res, next) => {
|
|
console.error('Error:', err)
|
|
|
|
res.status(err.status || 500).json({
|
|
success: false,
|
|
message: err.message || '服务器内部错误',
|
|
timestamp: new Date().toISOString(),
|
|
...(process.env.NODE_ENV === 'development' && { stack: err.stack })
|
|
})
|
|
})
|
|
|
|
const PORT = process.env.PORT || 3000
|
|
|
|
// 启动服务器
|
|
const startServer = async () => {
|
|
try {
|
|
// 测试数据库连接
|
|
const dbConnected = await testConnection();
|
|
if (!dbConnected) {
|
|
console.error('❌ 数据库连接失败,服务器启动终止');
|
|
process.exit(1);
|
|
}
|
|
|
|
// 同步数据库模型
|
|
await syncModels();
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`🚀 服务器启动成功`)
|
|
console.log(`📱 运行环境: ${process.env.NODE_ENV || 'development'}`)
|
|
console.log(`🌐 访问地址: http://localhost:${PORT}`)
|
|
console.log(`📊 健康检查: http://localhost:${PORT}/health`)
|
|
console.log(`📚 API文档: http://localhost:${PORT}/api/docs`)
|
|
})
|
|
} catch (error) {
|
|
console.error('❌ 服务器启动失败:', error)
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
startServer()
|