From f490a0dac5c757acd1ff41e99efbaf6614f4351a Mon Sep 17 00:00:00 2001
From: Table
Date: Sat, 29 Nov 2025 04:58:41 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20@internal=20=20=E7=9B=AE=E5=BD=95?=
=?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BA=20src=20=E7=9B=AE=E5=BD=95?=
=?UTF-8?q?=E5=B9=B6=E5=81=9A=E4=BB=A5=E4=B8=8B=E5=8F=98=E6=9B=B4=EF=BC=9A?=
=?UTF-8?q?=201=E3=80=81=E5=B0=86=20@pkg=20=20=E7=9B=AE=E5=BD=95=E7=A7=BB?=
=?UTF-8?q?=E5=8A=A8=E5=88=B0=E5=8F=98=E6=9B=B4=E5=90=8E=E7=9A=84=20src=20?=
=?UTF-8?q?=E7=9B=AE=E5=BD=95=EF=BC=9B=202=E3=80=81=E5=B0=86=20@cmd=20=20?=
=?UTF-8?q?=E4=B8=8B=E7=9A=84=20@main.go=20=E7=A7=BB=E5=8A=A8=E5=88=B0=20?=
=?UTF-8?q?=20src=20=E7=9B=AE=E5=BD=95=EF=BC=9B=203=E3=80=81=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=20cmd=20=E7=9B=AE=E5=BD=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Dockerfile | 2 +-
Makefile | 14 +++++-----
PROJECT_SUMMARY.md | 26 +++++++++---------
README.md | 27 +++++++++----------
go.mod | 2 +-
{internal => src}/handler/router.go | 6 ++---
{internal => src}/handler/user_handler.go | 6 ++---
{cmd => src}/main.go | 8 +++---
{internal => src}/middleware/auth.go | 2 +-
{internal => src}/middleware/cors.go | 2 +-
{internal => src}/middleware/logger.go | 0
{internal => src}/middleware/rate_limit.go | 4 +--
{internal => src}/model/user.go | 0
{pkg => src/pkg}/auth/jwt.go | 2 +-
{pkg => src/pkg}/cache/redis.go | 2 +-
{pkg => src/pkg}/config/config.go | 0
{pkg => src/pkg}/database/database.go | 2 +-
.../repository/user_repository.go | 4 +--
{internal => src}/service/user_service.go | 6 ++---
test/config_test.go | 2 +-
test/middleware_test.go | 6 ++---
test/user_test.go | 2 +-
22 files changed, 62 insertions(+), 63 deletions(-)
rename {internal => src}/handler/router.go (97%)
rename {internal => src}/handler/user_handler.go (99%)
rename {cmd => src}/main.go (94%)
rename {internal => src}/middleware/auth.go (99%)
rename {internal => src}/middleware/cors.go (96%)
rename {internal => src}/middleware/logger.go (100%)
rename {internal => src}/middleware/rate_limit.go (98%)
rename {internal => src}/model/user.go (100%)
rename {pkg => src/pkg}/auth/jwt.go (99%)
rename {pkg => src/pkg}/cache/redis.go (99%)
rename {pkg => src/pkg}/config/config.go (100%)
rename {pkg => src/pkg}/database/database.go (98%)
rename {internal => src}/repository/user_repository.go (97%)
rename {internal => src}/service/user_service.go (98%)
diff --git a/Dockerfile b/Dockerfile
index 585c14a..0404521 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -17,7 +17,7 @@ RUN go mod download
COPY . .
# 构建应用
-RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o main cmd/main.go
+RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o main src/main.go
# 最终镜像
FROM alpine:latest
diff --git a/Makefile b/Makefile
index 9f3ef64..bd44e73 100644
--- a/Makefile
+++ b/Makefile
@@ -26,36 +26,36 @@ help: ## 显示帮助信息
.PHONY: dev
dev: ## 启动开发环境
@echo "启动开发环境..."
- $(GO) run cmd/main.go -env=dev
+ $(GO) run src/main.go -env=dev
.PHONY: stage
stage: ## 启动预发布环境
@echo "启动预发布环境..."
- $(GO) run cmd/main.go -env=stage
+ $(GO) run src/main.go -env=stage
.PHONY: prod
prod: ## 启动生产环境
@echo "启动生产环境..."
- $(GO) run cmd/main.go -env=prod
+ $(GO) run src/main.go -env=prod
# 构建相关
.PHONY: build
build: ## 构建应用程序
@echo "构建应用程序..."
@mkdir -p $(BUILD_DIR)
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME) cmd/main.go
+ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME) src/main.go
.PHONY: build-windows
build-windows: ## 构建Windows版本
@echo "构建Windows版本..."
@mkdir -p $(BUILD_DIR)
- CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME).exe cmd/main.go
+ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME).exe src/main.go
.PHONY: build-mac
build-mac: ## 构建macOS版本
@echo "构建macOS版本..."
@mkdir -p $(BUILD_DIR)
- CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME)-mac cmd/main.go
+ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GO) build -ldflags="-w -s" -o $(BUILD_DIR)/$(APP_NAME)-mac src/main.go
.PHONY: build-all
build-all: build build-windows build-mac ## 构建所有平台版本
@@ -116,7 +116,7 @@ docs: ## 生成API文档
@echo "生成API文档..."
@mkdir -p $(DOC_DIR)/dev $(DOC_DIR)/stage $(DOC_DIR)/prod
@echo "请先安装 swag: go install github.com/swaggo/swag/cmd/swag@latest"
- @echo "然后运行: swag init -g cmd/main.go -o doc/dev --parseDependency --parseInternal"
+ @echo "然后运行: swag init -g src/main.go -o doc/dev --parseDependency --parseInternal"
.PHONY: docs-serve
docs-serve: docs ## 启动文档服务器
diff --git a/PROJECT_SUMMARY.md b/PROJECT_SUMMARY.md
index 2d60f11..be8ba19 100644
--- a/PROJECT_SUMMARY.md
+++ b/PROJECT_SUMMARY.md
@@ -8,7 +8,7 @@
### 1. 项目基础架构
- ✅ 基于 Gin 框架的 RESTful API 服务
-- ✅ 清晰的项目目录结构(cmd、internal、pkg 分层)
+- ✅ 清晰的项目目录结构(src 目录统一管理源代码)
- ✅ Go 模块依赖管理
### 2. 配置管理系统
@@ -102,22 +102,22 @@ make test-coverage
```
yinli-api/
-├── cmd/main.go # 应用程序入口
-├── config/ # 配置文件目录
-│ ├── dev.yaml # 开发环境配置
-│ ├── stage.yaml # 预发布环境配置
-│ └── prod.yaml # 生产环境配置
-├── internal/ # 内部应用代码
+├── src/ # 源代码目录
+│ ├── main.go # 应用程序入口
│ ├── handler/ # HTTP 处理器
│ ├── middleware/ # 中间件
│ ├── model/ # 数据模型
│ ├── repository/ # 数据访问层
-│ └── service/ # 业务逻辑层
-├── pkg/ # 可复用包
-│ ├── auth/ # JWT 认证
-│ ├── cache/ # Redis 缓存
-│ ├── config/ # 配置管理
-│ └── database/ # 数据库连接
+│ ├── service/ # 业务逻辑层
+│ └── pkg/ # 可复用包
+│ ├── auth/ # JWT 认证
+│ ├── cache/ # Redis 缓存
+│ ├── config/ # 配置管理
+│ └── database/ # 数据库连接
+├── config/ # 配置文件目录
+│ ├── dev.yaml # 开发环境配置
+│ ├── stage.yaml # 预发布环境配置
+│ └── prod.yaml # 生产环境配置
├── docker/ # Docker 相关文件
├── sql/ # 数据库脚本
├── test/ # 测试文件
diff --git a/README.md b/README.md
index d72952b..0b2a5e0 100644
--- a/README.md
+++ b/README.md
@@ -28,19 +28,18 @@
```
yinli-api/
-├── cmd/ # 应用程序入口
-│ └── main.go
-├── internal/ # 内部应用代码
-│ ├── handler/ # HTTP 处理器
-│ ├── middleware/ # 中间件
+├── src/ # 源代码目录
+│ ├── main.go # 应用程序入口
+│ ├── handler/ # HTTP 处理器
+│ ├── middleware/ # 中间件
│ ├── model/ # 数据模型
│ ├── repository/ # 数据访问层
-│ └── service/ # 业务逻辑层
-├── pkg/ # 可复用的包
-│ ├── auth/ # 认证相关
-│ ├── cache/ # 缓存操作
-│ ├── config/ # 配置管理
-│ └── database/ # 数据库连接
+│ ├── service/ # 业务逻辑层
+│ └── pkg/ # 可复用的包
+│ ├── auth/ # 认证相关
+│ ├── cache/ # 缓存操作
+│ ├── config/ # 配置管理
+│ └── database/ # 数据库连接
├── config/ # 配置文件
│ ├── dev.yaml # 开发环境配置
│ ├── stage.yaml # 预发布环境配置
@@ -399,12 +398,12 @@ export YINLI_REDIS_PASSWORD=your_redis_password
A: 修改 `config/{env}.yaml` 文件中的 `database` 配置项。
### Q: 如何添加新的 API 接口?
-A: 1. 在 `internal/handler` 中添加处理函数
- 2. 在 `internal/handler/router.go` 中注册路由
+A: 1. 在 `src/handler` 中添加处理函数
+ 2. 在 `src/handler/router.go` 中注册路由
3. 添加相应的测试用例
### Q: 如何自定义中间件?
-A: 在 `internal/middleware` 目录下创建新的中间件文件,参考现有中间件的实现。
+A: 在 `src/middleware` 目录下创建新的中间件文件,参考现有中间件的实现。
### Q: 如何部署到生产环境?
A: 使用 `make docker-compose-prod` 生成生产环境配置,然后使用 `make docker-up-prod` 部署。
diff --git a/go.mod b/go.mod
index b21180c..fa6d7a8 100644
--- a/go.mod
+++ b/go.mod
@@ -13,6 +13,7 @@ require (
github.com/stretchr/testify v1.11.1
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.1
+ github.com/swaggo/swag v1.8.12
golang.org/x/crypto v0.45.0
golang.org/x/time v0.14.0
)
@@ -60,7 +61,6 @@ require (
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
- github.com/swaggo/swag v1.8.12 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.0 // indirect
go.uber.org/mock v0.5.0 // indirect
diff --git a/internal/handler/router.go b/src/handler/router.go
similarity index 97%
rename from internal/handler/router.go
rename to src/handler/router.go
index 598715b..7b8e870 100644
--- a/internal/handler/router.go
+++ b/src/handler/router.go
@@ -1,9 +1,9 @@
package handler
import (
- "yinli-api/internal/middleware"
- "yinli-api/internal/repository"
- "yinli-api/internal/service"
+ "yinli-api/src/middleware"
+ "yinli-api/src/repository"
+ "yinli-api/src/service"
"net/http/pprof"
diff --git a/internal/handler/user_handler.go b/src/handler/user_handler.go
similarity index 99%
rename from internal/handler/user_handler.go
rename to src/handler/user_handler.go
index 64ebf48..2fcb72d 100644
--- a/internal/handler/user_handler.go
+++ b/src/handler/user_handler.go
@@ -4,9 +4,9 @@ import (
"net/http"
"strconv"
- "yinli-api/internal/middleware"
- "yinli-api/internal/model"
- "yinli-api/internal/service"
+ "yinli-api/src/middleware"
+ "yinli-api/src/model"
+ "yinli-api/src/service"
"github.com/gin-gonic/gin"
)
diff --git a/cmd/main.go b/src/main.go
similarity index 94%
rename from cmd/main.go
rename to src/main.go
index 6ae5583..0a75b82 100644
--- a/cmd/main.go
+++ b/src/main.go
@@ -7,10 +7,10 @@ import (
"os/signal"
"syscall"
- "yinli-api/internal/handler"
- "yinli-api/pkg/cache"
- "yinli-api/pkg/config"
- "yinli-api/pkg/database"
+ "yinli-api/src/handler"
+ "yinli-api/src/pkg/cache"
+ "yinli-api/src/pkg/config"
+ "yinli-api/src/pkg/database"
"github.com/gin-gonic/gin"
)
diff --git a/internal/middleware/auth.go b/src/middleware/auth.go
similarity index 99%
rename from internal/middleware/auth.go
rename to src/middleware/auth.go
index 9990f42..b1bb0d0 100644
--- a/internal/middleware/auth.go
+++ b/src/middleware/auth.go
@@ -3,7 +3,7 @@ package middleware
import (
"net/http"
- "yinli-api/pkg/auth"
+ "yinli-api/src/pkg/auth"
"github.com/gin-gonic/gin"
)
diff --git a/internal/middleware/cors.go b/src/middleware/cors.go
similarity index 96%
rename from internal/middleware/cors.go
rename to src/middleware/cors.go
index 9dc1a70..86d15b1 100644
--- a/internal/middleware/cors.go
+++ b/src/middleware/cors.go
@@ -1,7 +1,7 @@
package middleware
import (
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
diff --git a/internal/middleware/logger.go b/src/middleware/logger.go
similarity index 100%
rename from internal/middleware/logger.go
rename to src/middleware/logger.go
diff --git a/internal/middleware/rate_limit.go b/src/middleware/rate_limit.go
similarity index 98%
rename from internal/middleware/rate_limit.go
rename to src/middleware/rate_limit.go
index 6509145..9e0eaa3 100644
--- a/internal/middleware/rate_limit.go
+++ b/src/middleware/rate_limit.go
@@ -6,8 +6,8 @@ import (
"strconv"
"time"
- "yinli-api/pkg/cache"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/cache"
+ "yinli-api/src/pkg/config"
"github.com/gin-gonic/gin"
"golang.org/x/time/rate"
diff --git a/internal/model/user.go b/src/model/user.go
similarity index 100%
rename from internal/model/user.go
rename to src/model/user.go
diff --git a/pkg/auth/jwt.go b/src/pkg/auth/jwt.go
similarity index 99%
rename from pkg/auth/jwt.go
rename to src/pkg/auth/jwt.go
index 1c18df1..6244959 100644
--- a/pkg/auth/jwt.go
+++ b/src/pkg/auth/jwt.go
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
"github.com/golang-jwt/jwt/v5"
)
diff --git a/pkg/cache/redis.go b/src/pkg/cache/redis.go
similarity index 99%
rename from pkg/cache/redis.go
rename to src/pkg/cache/redis.go
index 550865e..a3442e3 100644
--- a/pkg/cache/redis.go
+++ b/src/pkg/cache/redis.go
@@ -7,7 +7,7 @@ import (
"log"
"time"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
"github.com/go-redis/redis/v8"
)
diff --git a/pkg/config/config.go b/src/pkg/config/config.go
similarity index 100%
rename from pkg/config/config.go
rename to src/pkg/config/config.go
diff --git a/pkg/database/database.go b/src/pkg/database/database.go
similarity index 98%
rename from pkg/database/database.go
rename to src/pkg/database/database.go
index 0e049cc..aaf9d1d 100644
--- a/pkg/database/database.go
+++ b/src/pkg/database/database.go
@@ -6,7 +6,7 @@ import (
"log"
"time"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
diff --git a/internal/repository/user_repository.go b/src/repository/user_repository.go
similarity index 97%
rename from internal/repository/user_repository.go
rename to src/repository/user_repository.go
index 3078e26..91f4f1d 100644
--- a/internal/repository/user_repository.go
+++ b/src/repository/user_repository.go
@@ -1,8 +1,8 @@
package repository
import (
- "yinli-api/internal/model"
- "yinli-api/pkg/database"
+ "yinli-api/src/model"
+ "yinli-api/src/pkg/database"
"github.com/jinzhu/gorm"
)
diff --git a/internal/service/user_service.go b/src/service/user_service.go
similarity index 98%
rename from internal/service/user_service.go
rename to src/service/user_service.go
index d2c53b2..b3d5f78 100644
--- a/internal/service/user_service.go
+++ b/src/service/user_service.go
@@ -4,9 +4,9 @@ import (
"errors"
"fmt"
- "yinli-api/internal/model"
- "yinli-api/internal/repository"
- "yinli-api/pkg/auth"
+ "yinli-api/src/model"
+ "yinli-api/src/repository"
+ "yinli-api/src/pkg/auth"
"github.com/jinzhu/gorm"
)
diff --git a/test/config_test.go b/test/config_test.go
index 98c79b3..899b90b 100644
--- a/test/config_test.go
+++ b/test/config_test.go
@@ -4,7 +4,7 @@ import (
"os"
"testing"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
"github.com/stretchr/testify/assert"
)
diff --git a/test/middleware_test.go b/test/middleware_test.go
index ed5b1d8..9f0938e 100644
--- a/test/middleware_test.go
+++ b/test/middleware_test.go
@@ -5,9 +5,9 @@ import (
"net/http/httptest"
"testing"
- "yinli-api/internal/middleware"
- "yinli-api/pkg/auth"
- "yinli-api/pkg/config"
+ "yinli-api/src/middleware"
+ "yinli-api/src/pkg/auth"
+ "yinli-api/src/pkg/config"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
diff --git a/test/user_test.go b/test/user_test.go
index 395ea23..55c3c21 100644
--- a/test/user_test.go
+++ b/test/user_test.go
@@ -6,7 +6,7 @@ import (
"net/http/httptest"
"testing"
- "yinli-api/pkg/config"
+ "yinli-api/src/pkg/config"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"