将 @internal 目录重命名为 src 目录并做以下变更:

1、将 @pkg  目录移动到变更后的 src 目录;
2、将 @cmd  下的 @main.go 移动到  src 目录;
3、移除 cmd 目录
This commit is contained in:
Table 2025-11-29 04:58:41 +08:00
parent 46e3a7b2c0
commit f490a0dac5
22 changed files with 62 additions and 63 deletions

View File

@ -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

View File

@ -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 ## 启动文档服务器

View File

@ -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/ # 可复用包
── service/ # 业务逻辑层
│ └── pkg/ # 可复用包
│ ├── auth/ # JWT 认证
│ ├── cache/ # Redis 缓存
│ ├── config/ # 配置管理
│ └── database/ # 数据库连接
├── config/ # 配置文件目录
│ ├── dev.yaml # 开发环境配置
│ ├── stage.yaml # 预发布环境配置
│ └── prod.yaml # 生产环境配置
├── docker/ # Docker 相关文件
├── sql/ # 数据库脚本
├── test/ # 测试文件

View File

@ -28,15 +28,14 @@
```
yinli-api/
├── cmd/ # 应用程序入口
│ └── main.go
├── internal/ # 内部应用代码
├── src/ # 源代码目录
│ ├── main.go # 应用程序入口
│ ├── handler/ # HTTP 处理器
│ ├── middleware/ # 中间件
│ ├── model/ # 数据模型
│ ├── repository/ # 数据访问层
── service/ # 业务逻辑层
├── pkg/ # 可复用的包
── service/ # 业务逻辑层
│ └── pkg/ # 可复用的包
│ ├── auth/ # 认证相关
│ ├── cache/ # 缓存操作
│ ├── config/ # 配置管理
@ -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` 部署。

2
go.mod
View File

@ -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

View File

@ -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"

View File

@ -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"
)

View File

@ -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"
)

View File

@ -3,7 +3,7 @@ package middleware
import (
"net/http"
"yinli-api/pkg/auth"
"yinli-api/src/pkg/auth"
"github.com/gin-gonic/gin"
)

View File

@ -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"

View File

@ -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"

View File

@ -5,7 +5,7 @@ import (
"fmt"
"time"
"yinli-api/pkg/config"
"yinli-api/src/pkg/config"
"github.com/golang-jwt/jwt/v5"
)

View File

@ -7,7 +7,7 @@ import (
"log"
"time"
"yinli-api/pkg/config"
"yinli-api/src/pkg/config"
"github.com/go-redis/redis/v8"
)

View File

@ -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"

View File

@ -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"
)

View File

@ -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"
)

View File

@ -4,7 +4,7 @@ import (
"os"
"testing"
"yinli-api/pkg/config"
"yinli-api/src/pkg/config"
"github.com/stretchr/testify/assert"
)

View File

@ -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"

View File

@ -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"