diff --git a/go-backend/README.md b/go-backend/README.md index 7e4a6f6..30db02f 100644 --- a/go-backend/README.md +++ b/go-backend/README.md @@ -1,30 +1,147 @@ -# Go Backend for Niumall +# NiūMall Go后端 -This is the Go implementation of the Niumall backend using the Gin framework. +这是一个使用Go语言和Gin框架构建的后端服务,为NiūMall活牛采购智能数字化系统提供API支持。 -## Project Structure +## 目录结构 ``` go-backend/ -├── README.md -├── go.mod -├── go.sum -├── main.go -├── config/ -├── models/ -├── routes/ -├── controllers/ -├── middleware/ -├── utils/ -└── docs/ +├── config/ # 配置文件 +├── controllers/ # 控制器 +├── docs/ # API文档 +├── middleware/ # 中间件 +├── models/ # 数据模型 +├── routes/ # 路由定义 +├── utils/ # 工具函数 +├── main.go # 主程序入口 +├── go.mod # Go模块定义 +├── go.sum # Go模块校验和 +├── .env.example # 环境变量示例 +├── .env # 本地环境变量配置 +├── Dockerfile # Docker构建文件 +├── docker-compose.yml # Docker编排文件 +├── Makefile # 构建脚本 +├── start_dev.sh # 本地开发启动脚本 +└── README.md # 项目说明 ``` -## Getting Started +## 本地开发 -1. Install Go (version 1.16 or later) -2. Run `go mod tidy` to install dependencies -3. Run `go run main.go` to start the server +### 环境要求 -## API Documentation +- Go 1.21或更高版本 +- MySQL 8.0或更高版本 +- Docker (可选,用于容器化部署) -API documentation is available in the `docs/` directory. \ No newline at end of file +### 安装依赖 + +```bash +go mod tidy +``` + +### 配置环境变量 + +复制`.env.example`文件并根据需要修改配置: + +```bash +cp .env.example .env +``` + +### 启动服务 + +#### 方法1:直接运行(需要本地安装Go和MySQL) + +```bash +./start_dev.sh +``` + +#### 方法2:使用Docker(推荐) + +```bash +# 构建并启动所有服务 +docker-compose up -d + +# 查看服务状态 +docker-compose ps + +# 查看服务日志 +docker-compose logs -f +``` + +### API文档 + +API文档位于`docs/`目录下,使用OpenAPI 3.0规范编写: + +- 用户管理: `docs/users.yaml` +- 订单管理: `docs/orders.yaml` +- 支付管理: `docs/payments.yaml` + +可以使用[Swagger UI](https://editor.swagger.io/)等工具查看文档。 + +## 部署 + +### 使用Docker部署 + +```bash +# 构建镜像 +docker build -t niumall-go-backend . + +# 运行容器 +docker run -p 8080:8080 niumall-go-backend +``` + +### 使用Docker Compose部署 + +```bash +# 在生产环境中启动服务 +docker-compose up -d +``` + +## 开发指南 + +### 代码结构 + +- `main.go`: 程序入口,负责初始化和启动服务 +- `config/`: 数据库连接等配置 +- `models/`: 数据模型定义 +- `controllers/`: 业务逻辑处理 +- `routes/`: 路由注册 +- `middleware/`: 中间件 +- `utils/`: 工具函数 + +### 添加新功能 + +1. 在`models/`目录下创建数据模型 +2. 在`controllers/`目录下创建控制器 +3. 在`routes/routes.go`中注册路由 +4. 更新`docs/`目录下的API文档 + +### 测试 + +运行单元测试: + +```bash +go test -v ./... +``` + +## 故障排除 + +### 网络连接问题 + +如果在构建Docker镜像时遇到网络连接问题,请配置Docker使用国内镜像源: + +1. 在Docker Desktop中打开Settings → Docker Engine +2. 添加以下配置: + ```json + { + "registry-mirrors": [ + "https://docker.mirrors.ustc.edu.cn", + "https://hub-mirror.c.163.com" + ] + } + ``` +3. 点击"Apply & Restart" + +### 数据库连接问题 + +确保MySQL服务正在运行,并且在`.env`文件中配置了正确的数据库连接信息。 \ No newline at end of file diff --git a/go-backend/start_dev.sh b/go-backend/start_dev.sh new file mode 100755 index 0000000..c43ddfc --- /dev/null +++ b/go-backend/start_dev.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# 本地开发启动脚本 + +echo "正在启动NiūMall Go后端开发环境..." + +# 检查是否安装了Go +if ! command -v go &> /dev/null +then + echo "未检测到Go环境,请先安装Go 1.21或更高版本" + exit 1 +fi + +# 检查是否安装了Docker +if ! command -v docker &> /dev/null +then + echo "未检测到Docker,请先安装Docker" + exit 1 +fi + +# 检查是否安装了docker-compose +if ! command -v docker-compose &> /dev/null +then + echo "未检测到docker-compose,请先安装docker-compose" + exit 1 +fi + +# 设置环境变量 +export DB_HOST=localhost +export DB_PORT=3306 +export DB_USER=root +export DB_PASSWORD= +export DB_NAME=niumall_dev +export PORT=8080 +export GIN_MODE=debug +export JWT_SECRET=mysecretkey + +# 创建开发数据库(如果不存在) +echo "正在创建开发数据库..." +mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p$DB_PASSWORD -e "CREATE DATABASE IF NOT EXISTS $DB_NAME;" 2>/dev/null || { + echo "警告:无法创建数据库,请确保MySQL服务正在运行且凭据正确" +} + +# 启动Go应用 +echo "正在启动Go应用..." +go run main.go \ No newline at end of file