topfans/backend
2026-04-08 01:30:57 +08:00
..
deploy 提交后端代码 2026-04-07 22:29:48 +08:00
docs 提交后端代码 2026-04-07 22:29:48 +08:00
gateway feat: 注册图生图 API 路由 2026-04-08 01:30:57 +08:00
google/api 提交后端代码 2026-04-07 22:29:48 +08:00
pkg 提交后端代码 2026-04-07 22:29:48 +08:00
proto 提交后端代码 2026-04-07 22:29:48 +08:00
scripts 提交后端代码 2026-04-07 22:29:48 +08:00
services fix: 修复解压炸弹漏洞和goroutine泄漏 2026-04-08 01:30:57 +08:00
swagger 提交后端代码 2026-04-07 22:29:48 +08:00
.env.example 提交后端代码 2026-04-07 22:29:48 +08:00
.gitignore 提交后端代码 2026-04-07 22:29:48 +08:00
.go-version 提交后端代码 2026-04-07 22:29:48 +08:00
activities_export.sql 提交后端代码 2026-04-07 22:29:48 +08:00
activity_items_export.sql 提交后端代码 2026-04-07 22:29:48 +08:00
check_oss_config.sh 提交后端代码 2026-04-07 22:29:48 +08:00
compile-and-restart.sh 提交后端代码 2026-04-07 22:29:48 +08:00
gen-swagger.sh 提交后端代码 2026-04-07 22:29:48 +08:00
go.mod 提交后端代码 2026-04-07 22:29:48 +08:00
go.sum 提交后端代码 2026-04-07 22:29:48 +08:00
go.work 提交后端代码 2026-04-07 22:29:48 +08:00
go.work.sum 提交后端代码 2026-04-07 22:29:48 +08:00
go.work使用说明.md 提交后端代码 2026-04-07 22:29:48 +08:00
install-swagger-deps.sh 提交后端代码 2026-04-07 22:29:48 +08:00
Makefile 提交后端代码 2026-04-07 22:29:48 +08:00
PROJECT_SUMMARY.md 提交后端代码 2026-04-07 22:29:48 +08:00
Proto编译完成总结.md 提交后端代码 2026-04-07 22:29:48 +08:00
QUICK_START.md 提交后端代码 2026-04-07 22:29:48 +08:00
readme.md 提交后端代码 2026-04-07 22:29:48 +08:00
start-with-swagger.sh 提交后端代码 2026-04-07 22:29:48 +08:00
start.sh 提交后端代码 2026-04-07 22:29:48 +08:00
stop.sh 提交后端代码 2026-04-07 22:29:48 +08:00
update-swagger.sh 提交后端代码 2026-04-07 22:29:48 +08:00

Top Fans Backend

微服务后端项目,使用 Go 1.25.5 和 gRPC。

项目架构

  • UserSocialService: 用户与社交服务(用户、社交、展位、任务)
  • AssetChainService: 资产与链服务
  • Gateway: API 网关REST API

环境要求

  • Go 1.25.5
  • Protocol Buffers Compiler (protoc) 3.x+
  • Make

快速开始

1. 安装 Go 环境

# 使用 Homebrew (macOS)
brew install go@1.25.5

# 或从官网下载
# https://go.dev/dl/

2. 配置环境

运行安装脚本:

chmod +x scripts/setup.sh
./scripts/setup.sh

或手动安装:

# 设置 Go 代理(可选,加速下载)
export GOPROXY=https://goproxy.cn,direct

# 下载依赖
go mod download
go mod tidy

# 安装 protoc 插件
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest

# 安装 googleapis
go get github.com/googleapis/googleapis

3. 安装 Protocol Buffers

# macOS
brew install protobuf

# Linux (Ubuntu/Debian)
apt-get install protobuf-compiler

# Linux (CentOS/RHEL)
yum install protobuf-compiler

4. 生成代码

# 生成 gRPC 代码
make proto

# 生成 REST API 代码和 Swagger 文档
make swagger

# 生成所有代码
make all

项目结构

backend/
├── api/
│   └── gateway/          # 生成的 REST API Gateway 代码
├── pkg/
│   └── proto/            # 生成的 gRPC 代码
├── proto/                # Proto 定义文件
│   ├── common.proto
│   ├── user.proto
│   └── asset-chain.proto
├── swagger/              # Swagger 文档
├── scripts/              # 脚本文件
├── go.mod                # Go 模块定义
├── go.work               # Go 工作区配置
├── Makefile              # 构建脚本
└── README.md

开发工具

代码检查

项目使用 golangci-lint 进行代码检查:

# 安装 golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

# 运行检查
golangci-lint run

代码格式化

# 格式化代码
go fmt ./...

# 整理 imports
goimports -w .

依赖管理

主要依赖:

  • google.golang.org/protobuf: Protocol Buffers Go 支持
  • google.golang.org/grpc: gRPC 框架
  • github.com/grpc-ecosystem/grpc-gateway/v2: gRPC 到 REST 的转换
  • github.com/gin-gonic/gin: HTTP Web 框架
  • github.com/googleapis/googleapis: Google API 定义

Makefile 命令

make help          # 显示帮助信息
make proto         # 生成 gRPC 代码
make swagger       # 生成 REST API 代码和 Swagger 文档
make all           # 生成所有代码
make clean-proto   # 清理生成的 proto 代码
make clean-swagger # 清理生成的 gateway 代码和 swagger
make clean         # 清理所有生成的代码

版本管理

  • Go 版本: 1.25.5 (在 .go-version 文件中指定)
  • 使用 Go Workspace 模式管理多模块项目

注意事项

  1. 生成的代码(pkg/protoapi/gateway)不要手动修改
  2. 修改接口时,先更新 .proto 文件,再运行 make all 重新生成
  3. 确保 GOPATH/binPATH 中,以便使用 protoc 插件

问题排查

protoc 插件未找到

确保 $GOPATH/bin$HOME/go/bin 在 PATH 中:

export PATH=$PATH:$(go env GOPATH)/bin

googleapis 未找到

go get github.com/googleapis/googleapis

生成代码失败

检查 protoc 版本:

protoc --version  # 应该是 3.x 版本

许可证

[添加许可证信息]