refactor(config): 简化配置加载逻辑,使用单一配置文件
移除多环境配置文件支持,统一使用 server/config.yaml 或 config.yaml 删除 APP_ENV 相关代码和日志输出 更新部署脚本以匹配新的配置加载方式
This commit is contained in:
parent
0f631479e5
commit
5a2626693c
|
|
@ -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
|
|
@ -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"
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -29,22 +29,12 @@ type root struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() App {
|
func Load() App {
|
||||||
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
|
var cfg App
|
||||||
paths := []string{}
|
// unified single config: prefer server/config.yaml, then config.yaml
|
||||||
if env != "" {
|
if !readYAML(filepath.Join("server", "config.yaml"), &cfg) {
|
||||||
paths = append(paths,
|
_ = readYAML("config.yaml", &cfg)
|
||||||
"config."+env+".yaml",
|
}
|
||||||
filepath.Join("server", "config."+env+".yaml"),
|
LoadEnv()
|
||||||
)
|
|
||||||
}
|
|
||||||
// no global fallback to config.yaml; rely on APP_ENV or environment variables
|
|
||||||
var cfg App
|
|
||||||
for _, p := range paths {
|
|
||||||
if readYAML(p, &cfg) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LoadEnv()
|
|
||||||
if v := os.Getenv("MARKETING_DB_HOST"); v != "" {
|
if v := os.Getenv("MARKETING_DB_HOST"); v != "" {
|
||||||
cfg.MarketingDB.Host = v
|
cfg.MarketingDB.Host = v
|
||||||
}
|
}
|
||||||
|
|
@ -109,44 +99,37 @@ 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 != "" {
|
".env.local",
|
||||||
paths = append(paths,
|
filepath.Join("server", ".env.local"),
|
||||||
".env."+env,
|
}
|
||||||
filepath.Join("server", ".env."+env),
|
for _, p := range paths {
|
||||||
)
|
f, err := os.Open(p)
|
||||||
}
|
if err != nil {
|
||||||
paths = append(paths,
|
continue
|
||||||
".env.local",
|
}
|
||||||
filepath.Join("server", ".env.local"),
|
b, err := io.ReadAll(f)
|
||||||
)
|
f.Close()
|
||||||
for _, p := range paths {
|
if err != nil {
|
||||||
f, err := os.Open(p)
|
continue
|
||||||
if err != nil {
|
}
|
||||||
continue
|
lines := strings.Split(string(b), "\n")
|
||||||
}
|
for _, ln := range lines {
|
||||||
b, err := io.ReadAll(f)
|
s := strings.TrimSpace(ln)
|
||||||
f.Close()
|
if s == "" || strings.HasPrefix(s, "#") {
|
||||||
if err != nil {
|
continue
|
||||||
continue
|
}
|
||||||
}
|
kv := strings.SplitN(s, "=", 2)
|
||||||
lines := strings.Split(string(b), "\n")
|
if len(kv) != 2 {
|
||||||
for _, ln := range lines {
|
continue
|
||||||
s := strings.TrimSpace(ln)
|
}
|
||||||
if s == "" || strings.HasPrefix(s, "#") {
|
k := strings.TrimSpace(kv[0])
|
||||||
continue
|
v := strings.TrimSpace(kv[1])
|
||||||
}
|
if k != "" {
|
||||||
kv := strings.SplitN(s, "=", 2)
|
os.Setenv(k, v)
|
||||||
if len(kv) != 2 {
|
}
|
||||||
continue
|
}
|
||||||
}
|
break
|
||||||
k := strings.TrimSpace(kv[0])
|
}
|
||||||
v := strings.TrimSpace(kv[1])
|
|
||||||
if k != "" {
|
|
||||||
os.Setenv(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue