readme重写,加入半自动初始化脚本

This commit is contained in:
Hericium-sys
2026-04-25 13:55:13 +08:00
parent be95e23935
commit 31b2a8afce
2 changed files with 82 additions and 11 deletions
+25 -11
View File
@@ -24,7 +24,7 @@ go mod tidy
### 2. 配置环境 ### 2. 配置环境
修改 [`config/app.yaml`](/Users/xchou/Documents/code/skeleton/config/app.yaml) 修改 [`config/app.yaml`](config/app.yaml)
- `app`:主机、端口、版本、环境、时区 - `app`:主机、端口、版本、环境、时区
- `logger`:日志级别与输出方式 - `logger`:日志级别与输出方式
@@ -77,6 +77,20 @@ go run .
- `routes/ws/` 预留 WebSocket 扩展入口 - `routes/ws/` 预留 WebSocket 扩展入口
## 模板初始化
如果这是通过 GitHub Template 生成的新仓库,可以直接运行初始化脚本,把 `go.mod` 和源码里的包路径一次性改掉:
```bash
./scripts/init.sh github.com/yourname/yourrepo
```
脚本会自动完成:
- 更新 `go.mod` 的 module 名称
- 替换源码中的旧包路径
- 执行 `go mod tidy`
## 项目结构 ## 项目结构
```text ```text
@@ -97,14 +111,14 @@ go run .
## 主要目录说明 ## 主要目录说明
- [`main.go`](/Users/xchou/Documents/code/skeleton/main.go):启动入口,负责初始化配置、日志、JWT、数据库、Redis、路由和文档 - [`main.go`](main.go):启动入口,负责初始化配置、日志、JWT、数据库、Redis、路由和文档
- [`routes/`](/Users/xchou/Documents/code/skeleton/routes)REST 和 WebSocket 路由挂载 - [`routes/`](routes)REST 和 WebSocket 路由挂载
- [`middlewares/`](/Users/xchou/Documents/code/skeleton/middlewares):日志、CORS、JWT、恢复处理 - [`middlewares/`](middlewares):日志、CORS、JWT、恢复处理
- [`database/`](/Users/xchou/Documents/code/skeleton/database)PostgreSQL 和 Redis 连接封装 - [`database/`](database)PostgreSQL 和 Redis 连接封装
- [`modules/`](/Users/xchou/Documents/code/skeleton/modules):业务模块示例 - [`modules/`](modules):业务模块示例
- [`models/`](/Users/xchou/Documents/code/skeleton/models)GORM 模型 - [`models/`](models)GORM 模型
- [`utils/`](/Users/xchou/Documents/code/skeleton/utils):响应、分页、时区等通用工具 - [`utils/`](utils):响应、分页、时区等通用工具
- [`cmd/migrate/`](/Users/xchou/Documents/code/skeleton/cmd/migrate/main.go):迁移命令输出入口 - [`cmd/migrate/`](cmd/migrate/main.go):迁移命令输出入口
## 数据库迁移 ## 数据库迁移
@@ -112,8 +126,8 @@ go run .
### 相关文件 ### 相关文件
- [`atlas.hcl`](/Users/xchou/Documents/code/skeleton/atlas.hcl) - [`atlas.hcl`](atlas.hcl)
- [`atlas_loader.go`](/Users/xchou/Documents/code/skeleton/atlas_loader.go) - [`atlas_loader.go`](atlas_loader.go)
### 安装 Atlas ### 安装 Atlas
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage:
./scripts/init.sh <new-module-path>
Example:
./scripts/init.sh github.com/yourname/yourrepo
What it does:
- updates go.mod module path
- replaces old module imports across text files
- runs go mod tidy
EOF
}
if [[ $# -ne 1 ]]; then
usage
exit 1
fi
new_module="$1"
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd "$script_dir/.." && pwd)"
cd "$repo_root"
if [[ ! -f go.mod ]]; then
echo "go.mod not found in repository root"
exit 1
fi
old_module="$(awk 'NR==1 && $1 == "module" { print $2 }' go.mod)"
if [[ -z "$old_module" ]]; then
echo "failed to detect current module path from go.mod"
exit 1
fi
if [[ "$new_module" == "$old_module" ]]; then
echo "module path is already set to: $new_module"
exit 0
fi
echo "Updating module path:"
echo " old: $old_module"
echo " new: $new_module"
while IFS= read -r file; do
[[ -n "$file" ]] || continue
perl -0pi -e "s@\Q${old_module}/\E@${new_module}/@g" "$file"
done < <(rg -l --hidden --glob '!**/.git/**' --glob '!**/.idea/**' "${old_module}/" . || true)
go mod edit -module "$new_module"
go mod tidy
echo "Initialization complete."