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
|
||||
COPY --from=build /app/bin/marketing-data-server /app/bin/marketing-data-server
|
||||
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
|
||||
EXPOSE 8077
|
||||
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
|
||||
docker rm "$CID_NAME" >/dev/null 2>&1 || true
|
||||
fi
|
||||
CONFIG_PATH="$ROOT_DIR/server/config.$ENV_NAME.yaml"
|
||||
CONFIG_PATH="$ROOT_DIR/server/config.yaml"
|
||||
if [ ! -f "$CONFIG_PATH" ]; then
|
||||
echo "配置文件缺失:$CONFIG_PATH" >&2
|
||||
exit 1
|
||||
|
|
@ -30,10 +30,9 @@ fi
|
|||
docker run -d \
|
||||
--name "$CID_NAME" \
|
||||
--restart unless-stopped \
|
||||
--env APP_ENV="$ENV_NAME" \
|
||||
-p "$PORT:8077" \
|
||||
-v "$ROOT_DIR/storage/export:/app/storage/export" \
|
||||
-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"
|
||||
echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT"
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import (
|
|||
|
||||
func main() {
|
||||
cfg := config.Load()
|
||||
log.Println("APP_ENV:", os.Getenv("APP_ENV"))
|
||||
if cfg.YMTKeyDecryptKeyB64 != "" { os.Setenv("YMT_KEY_DECRYPT_KEY_B64", cfg.YMTKeyDecryptKeyB64) }
|
||||
_ = logging.Init("log")
|
||||
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 {
|
||||
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
|
||||
for _, p := range paths {
|
||||
if readYAML(p, &cfg) {
|
||||
break
|
||||
}
|
||||
}
|
||||
LoadEnv()
|
||||
var cfg App
|
||||
// unified single config: prefer server/config.yaml, then config.yaml
|
||||
if !readYAML(filepath.Join("server", "config.yaml"), &cfg) {
|
||||
_ = readYAML("config.yaml", &cfg)
|
||||
}
|
||||
LoadEnv()
|
||||
if v := os.Getenv("MARKETING_DB_HOST"); v != "" {
|
||||
cfg.MarketingDB.Host = v
|
||||
}
|
||||
|
|
@ -109,44 +99,37 @@ func (d DB) DSN() string {
|
|||
}
|
||||
|
||||
func LoadEnv() {
|
||||
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
|
||||
paths := []string{}
|
||||
if env != "" {
|
||||
paths = append(paths,
|
||||
".env."+env,
|
||||
filepath.Join("server", ".env."+env),
|
||||
)
|
||||
}
|
||||
paths = append(paths,
|
||||
".env.local",
|
||||
filepath.Join("server", ".env.local"),
|
||||
)
|
||||
for _, p := range paths {
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
b, err := io.ReadAll(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
lines := strings.Split(string(b), "\n")
|
||||
for _, ln := range lines {
|
||||
s := strings.TrimSpace(ln)
|
||||
if s == "" || strings.HasPrefix(s, "#") {
|
||||
continue
|
||||
}
|
||||
kv := strings.SplitN(s, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
k := strings.TrimSpace(kv[0])
|
||||
v := strings.TrimSpace(kv[1])
|
||||
if k != "" {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
// optional local env override only
|
||||
paths := []string{
|
||||
".env.local",
|
||||
filepath.Join("server", ".env.local"),
|
||||
}
|
||||
for _, p := range paths {
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
b, err := io.ReadAll(f)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
lines := strings.Split(string(b), "\n")
|
||||
for _, ln := range lines {
|
||||
s := strings.TrimSpace(ln)
|
||||
if s == "" || strings.HasPrefix(s, "#") {
|
||||
continue
|
||||
}
|
||||
kv := strings.SplitN(s, "=", 2)
|
||||
if len(kv) != 2 {
|
||||
continue
|
||||
}
|
||||
k := strings.TrimSpace(kv[0])
|
||||
v := strings.TrimSpace(kv[1])
|
||||
if k != "" {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue