fix(config): 完成生产配置向 config.toml 的迁移

This commit is contained in:
2026-09-19 00:39:09 +08:00
parent 4599ea32b2
commit 045d598400
26 changed files with 908 additions and 254 deletions
+6 -102
View File
@@ -25,14 +25,16 @@ const (
DefaultAuthSkew = 5 * time.Minute
ClientPatchHost = "prod-clientpatch.bluearchiveyostar.com"
ServerInfoHost = "yostar-serverinfo.bluearchiveyostar.com"
EnvFileName = ".env"
ConfigFileName = "config.toml"
ConfigExampleName = "config.toml.example"
)
// Config holds bat-api process configuration.
//
// Values come from (highest wins): CLI flags > process environment > .env file
// > built-in defaults. Database-related keys are reserved for future API
// persistence and are loaded but not required for the resource CDN surface.
// Values come from (highest wins): CLI flags > process environment >
// config.toml > built-in defaults. Database-related keys are reserved for
// future API persistence and are loaded but not required for the resource CDN
// surface.
type Config struct {
// Listen is the HTTP listen address, e.g. ":18080" or "127.0.0.1:18080".
Listen string
@@ -54,8 +56,6 @@ type Config struct {
RPCTimeout time.Duration
// RefreshInterval periodically re-discovers the release through RPC. Zero disables it.
RefreshInterval time.Duration
// SkipEnvFile disables loading .env when true (BAT_API_SKIP_ENV_FILE=1).
SkipEnvFile bool
// AuthToken enables HTTP token authentication when non-empty.
AuthToken string
// AuthQueryParam is the optional query parameter accepted for token auth.
@@ -148,41 +148,6 @@ func (c *Config) Normalize() error {
return nil
}
// LoadEnvFile reads KEY=VALUE lines from path into the process environment
// without overriding variables that are already set.
func LoadEnvFile(path string) error {
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
key, value, ok := strings.Cut(line, "=")
if !ok {
continue
}
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
value = strings.Trim(value, `"'`)
if key == "" {
continue
}
if _, exists := os.LookupEnv(key); exists {
continue
}
if err := os.Setenv(key, value); err != nil {
return err
}
}
return nil
}
// ApplyEnv overlays environment variables onto cfg.
func ApplyEnv(cfg *Config) {
if v := os.Getenv("BAT_API_LISTEN"); v != "" {
@@ -239,9 +204,6 @@ func ApplyEnv(cfg *Config) {
if v := os.Getenv("BAT_API_REDIS_PASSWORD"); v != "" {
cfg.RedisPassword = v
}
if v := os.Getenv("BAT_API_SKIP_ENV_FILE"); v != "" {
cfg.SkipEnvFile = parseBool(v, false)
}
if v := os.Getenv("BAT_API_AUTH_TOKEN"); v != "" {
cfg.AuthToken = v
}
@@ -313,61 +275,3 @@ func normalizePathList(paths []string) []string {
}
return out
}
// EnvTemplate is written next to the bat-api binary on first run when missing.
const EnvTemplate = `# bat-api configuration
# Priority: CLI flags > process environment > this file > built-in defaults.
# Resource auto-pull/sync is owned by the Rust bat daemon, not bat-api.
# bat-api only bootstraps/distributes already-published resources and exposes
# a small management surface for release inspection.
# HTTP listen address (host:port or :port)
BAT_API_LISTEN=:18080
# Public base URL used when rewriting AddressablesCatalogUrlRoot
BAT_API_PUBLIC_BASE_URL=http://127.0.0.1:18080
# Rust bat daemon RPC socket (primary discovery path)
# Prefer BAT_API_SOCKET; BAT_API_STATE_DIR only derives the default socket path.
BAT_API_STATE_DIR=/tmp/bat-pid
# BAT_API_SOCKET=/tmp/bat-pid/bat.sock
# Optional override of the published release root (fixtures / emergency only).
# Production obtains resource_root from daemon RPC (release.attestation +
# generation-bound resource.manifest).
# BAT_API_RESOURCE_ROOT=
# Optional server-info JSON for Addressables root rewrite
# BAT_API_SERVER_INFO_FILE=
# CDN safety
BAT_API_REQUIRE_INDEXED=true
BAT_API_VERIFY_SIZE=true
BAT_API_RPC_TIMEOUT=30s
# Periodically re-read bat.sock so bat-api follows Rust bat release switches.
# Set to 0 to disable in fixture-only local development.
BAT_API_REFRESH_INTERVAL=1m
# Player-facing HTTP controls.
# Prefer setting BAT_API_AUTH_TOKEN through a secret manager or process
# environment. Reverse proxies may inject Authorization: Bearer <token> or
# X-BAT-Token to authenticated upstream requests. Query token fallback uses
# BAT_API_AUTH_QUERY_PARAM and is supported for clients that cannot set headers.
# BAT_API_AUTH_TOKEN=
BAT_API_AUTH_QUERY_PARAM=bat_token
# BAT_API_AUTH_EXEMPT_PATHS=
BAT_API_TRUST_PROXY_HEADERS=false
BAT_API_ACCESS_LOG=false
# Zero disables in-process limiting; production may still enforce edge limits.
BAT_API_RATE_LIMIT_RPS=0
BAT_API_RATE_LIMIT_BURST=0
BAT_API_MAX_RESOURCE_LIMIT=1000
# Reserved for future API persistence (not required for resource CDN)
# BAT_API_DATABASE_URL=postgres://bat:@127.0.0.1:5432/bat?sslmode=disable
# BAT_API_DATABASE_PASSWORD=
# BAT_API_REDIS_URL=redis://127.0.0.1:6379/0
# BAT_API_REDIS_PASSWORD=
# Set to 1 to ignore this file entirely
# BAT_API_SKIP_ENV_FILE=0
`