Merge remote-tracking branch 'origin/main'

This commit is contained in:
2025-09-12 13:15:03 +08:00
committed by aiotagro
28 changed files with 10237 additions and 1945 deletions

View File

@@ -4,13 +4,15 @@ 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')
const path = require('path')
require('dotenv').config()
// 数据库连接
const { testConnection, syncModels } = require('./models')
// 导入Swagger配置
const { specs, swaggerUi } = require('./config/swagger')
const app = express()
// 中间件配置
@@ -21,74 +23,7 @@ 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({
@@ -111,6 +46,13 @@ app.get('/health', (req, res) => {
})
})
// 配置Swagger UI
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(specs, {
explorer: true,
customCss: '.swagger-ui .topbar { background-color: #3B82F6; }',
customSiteTitle: 'NiuMall API 文档'
}))
// API 路由
app.use('/api/auth', require('./routes/auth'))
app.use('/api/users', require('./routes/users'))
@@ -120,6 +62,14 @@ app.use('/api/transport', require('./routes/transport'))
app.use('/api/finance', require('./routes/finance'))
app.use('/api/quality', require('./routes/quality'))
// 静态文件服务
app.use('/static', express.static('public'));
// API文档路由重定向
app.get('/docs', (req, res) => {
res.redirect('/swagger');
});
// 404 处理
app.use((req, res) => {
res.status(404).json({
@@ -161,7 +111,7 @@ const startServer = async () => {
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`)
console.log(`📚 API文档: http://localhost:${PORT}/swagger`)
})
} catch (error) {
console.error('❌ 服务器启动失败:', error)