refactor(config): 简化配置加载逻辑,使用单一配置文件

移除多环境配置文件支持,统一使用 server/config.yaml 或 config.yaml
删除 APP_ENV 相关代码和日志输出
更新部署脚本以匹配新的配置加载方式
This commit is contained in:
zhouyonggao 2025-11-28 17:37:42 +08:00
parent 0f631479e5
commit 5a2626693c
5 changed files with 44 additions and 62 deletions

View File

@ -10,8 +10,6 @@ FROM alpine:3.19
WORKDIR /app WORKDIR /app
COPY --from=build /app/bin/marketing-data-server /app/bin/marketing-data-server COPY --from=build /app/bin/marketing-data-server /app/bin/marketing-data-server
COPY --from=build /app/web /app/web COPY --from=build /app/web /app/web
COPY --from=build /app/server/config.test.yaml /app/server/config.test.yaml
COPY --from=build /app/server/config.prod.yaml /app/server/config.prod.yaml
RUN mkdir -p /app/storage/export RUN mkdir -p /app/storage/export
EXPOSE 8077 EXPOSE 8077
ENTRYPOINT ["/app/bin/marketing-data-server"] ENTRYPOINT ["/app/bin/marketing-data-server"]

File diff suppressed because one or more lines are too long

View File

@ -22,7 +22,7 @@ fi
if docker ps -a --format '{{.Names}}' | grep -q "^${CID_NAME}$"; then if docker ps -a --format '{{.Names}}' | grep -q "^${CID_NAME}$"; then
docker rm "$CID_NAME" >/dev/null 2>&1 || true docker rm "$CID_NAME" >/dev/null 2>&1 || true
fi fi
CONFIG_PATH="$ROOT_DIR/server/config.$ENV_NAME.yaml" CONFIG_PATH="$ROOT_DIR/server/config.yaml"
if [ ! -f "$CONFIG_PATH" ]; then if [ ! -f "$CONFIG_PATH" ]; then
echo "配置文件缺失:$CONFIG_PATH" >&2 echo "配置文件缺失:$CONFIG_PATH" >&2
exit 1 exit 1
@ -30,10 +30,9 @@ fi
docker run -d \ docker run -d \
--name "$CID_NAME" \ --name "$CID_NAME" \
--restart unless-stopped \ --restart unless-stopped \
--env APP_ENV="$ENV_NAME" \
-p "$PORT:8077" \ -p "$PORT:8077" \
-v "$ROOT_DIR/storage/export:/app/storage/export" \ -v "$ROOT_DIR/storage/export:/app/storage/export" \
-v "$ROOT_DIR/log:/app/log" \ -v "$ROOT_DIR/log:/app/log" \
-v "$CONFIG_PATH:/app/server/config.$ENV_NAME.yaml:ro" \ -v "$CONFIG_PATH:/app/server/config.yaml:ro" \
"$IMAGE:$TAG" "$IMAGE:$TAG"
echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT" echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT"

View File

@ -13,7 +13,6 @@ import (
func main() { func main() {
cfg := config.Load() cfg := config.Load()
log.Println("APP_ENV:", os.Getenv("APP_ENV"))
if cfg.YMTKeyDecryptKeyB64 != "" { os.Setenv("YMT_KEY_DECRYPT_KEY_B64", cfg.YMTKeyDecryptKeyB64) } if cfg.YMTKeyDecryptKeyB64 != "" { os.Setenv("YMT_KEY_DECRYPT_KEY_B64", cfg.YMTKeyDecryptKeyB64) }
_ = logging.Init("log") _ = logging.Init("log")
log.Println("connecting YMT MySQL:", cfg.YMTDB.Host+":"+cfg.YMTDB.Port, "db", cfg.YMTDB.Name, "user", cfg.YMTDB.User) log.Println("connecting YMT MySQL:", cfg.YMTDB.Host+":"+cfg.YMTDB.Port, "db", cfg.YMTDB.Name, "user", cfg.YMTDB.User)

View File

@ -29,20 +29,10 @@ type root struct {
} }
func Load() App { func Load() App {
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
paths := []string{}
if env != "" {
paths = append(paths,
"config."+env+".yaml",
filepath.Join("server", "config."+env+".yaml"),
)
}
// no global fallback to config.yaml; rely on APP_ENV or environment variables
var cfg App var cfg App
for _, p := range paths { // unified single config: prefer server/config.yaml, then config.yaml
if readYAML(p, &cfg) { if !readYAML(filepath.Join("server", "config.yaml"), &cfg) {
break _ = readYAML("config.yaml", &cfg)
}
} }
LoadEnv() LoadEnv()
if v := os.Getenv("MARKETING_DB_HOST"); v != "" { if v := os.Getenv("MARKETING_DB_HOST"); v != "" {
@ -109,18 +99,11 @@ func (d DB) DSN() string {
} }
func LoadEnv() { func LoadEnv() {
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV"))) // optional local env override only
paths := []string{} paths := []string{
if env != "" {
paths = append(paths,
".env."+env,
filepath.Join("server", ".env."+env),
)
}
paths = append(paths,
".env.local", ".env.local",
filepath.Join("server", ".env.local"), filepath.Join("server", ".env.local"),
) }
for _, p := range paths { for _, p := range paths {
f, err := os.Open(p) f, err := os.Open(p)
if err != nil { if err != nil {