为了让 git 版本控制和 goframe 的配置文件能和谐相处,引入了 gomplate。 git 只对模板进行控制,敏感数据只在本地。
https://github.com/hairyhenderson/gomplate
创建
- 初始化项目
gf init -u test
cd test
gf up -a
- 创建 manifest/config/config.template, 填入以下配置
# https://goframe.org/docs/web/server-config-file-template
server:
address: "{{(ds "env").SERVER_PORT}}"
openapiPath: "/api.json"
swaggerPath: "/swagger"
# https://goframe.org/docs/core/glog-config
logger:
path: "storage/logs/daily" # 日志文件路径。默认为空,表示关闭,仅输出到终端
file: "{Y-m-d}.log" # 日志文件格式。默认为"{Y-m-d}.log"
prefix: "" # 日志内容输出前缀。默认为空
level: "all" # 日志输出级别
timeFormat: "2006-01-02T15:04:05" # 自定义日志输出的时间格式,使用Golang标准的时间格式配置
ctxKeys: [] # 自定义Context上下文变量名称,自动打印Context的变量到日志中。默认为空
header: true # 是否打印日志的头信息。默认true
stdout: true # 日志是否同时输出到终端。默认true
rotateSize: 0 # 按照日志文件大小对文件进行滚动切分。默认为0,表示关闭滚动切分特性
rotateExpire: 0 # 按照日志文件时间间隔对文件滚动切分。默认为0,表示关闭滚动切分特性
rotateBackupLimit: 0 # 按照切分的文件数量清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除
rotateBackupExpire: 0 # 按照切分的文件有效期清理切分文件,当滚动切分特性开启时有效。默认为0,表示不备份,切分则删除
rotateBackupCompress: 0 # 滚动切分文件的压缩比(0-9)。默认为0,表示不压缩
rotateCheckInterval: "1h" # 滚动切分的时间检测间隔,一般不需要设置。默认为1小时
stdoutColorDisabled: false # 关闭终端的颜色打印。默认开启
writerColorEnable: false # 日志文件是否带上颜色。默认false,表示不带颜色
error:
path: "storage/logs/error/"
file: "{Y-m-d}.log"
stdout: true
test:
path: "storage/logs/test"
file: "{Y-m-d}.log"
stdout: true
plantform:
tencent:
secretId: {{(ds "env").TENCENT_SECRETID}}
secretKey: {{(ds "env").TENCENT_SECRETKEY}}
- 在项目根目录下创建
.env.dev、.env.prod、.env.example这三个文件。仅.env.example添加到版本控制中。内容如下。其他文件根据示例文件,按场景需要配置。
# 端口号
SERVER_PORT=:8090
# 平台信息
TENCENT_SECRETID=
TENCENT_SECRETKEY=
- 创建
hack/config.mk文件。内容如下:
# 环境: dev / prod
# https://docs.gomplate.ca/
# https://github.com/hairyhenderson/gomplate
ENV ?= dev
ENV_FILE := .env.$(ENV)
TPL_SRC := manifest/config/config.template
TPL_OUT := manifest/config/config.yaml
.PHONY: config
config:
@echo ----------------------------------------
@echo create config template using $(ENV_FILE)
@echo ----------------------------------------
@echo.
gomplate -d env=$(ENV_FILE)?type=application/x-env -f $(TPL_SRC) -o $(TPL_OUT)
@echo.
@echo Config created: $(TPL_OUT)
@echo.
- 在根目录下的
Makefile文件追加配置。
…
include ./hack/config.mk
- 添加 git 版本控制
git init -b main
# 在 .gitignore 中增加忽略文件
.env.dev
.env.prod
# 添加配置
git add .
git commit -m 'initialize'
使用
# 生成测试环境配置
make config
# 生成正式服环境配置
make config ENV=prod
使用上边命令就可以在 manifest/config/ 下生成对应的配置文件。
