refactor(config): 统一共享配置模板来源

This commit is contained in:
2026-09-19 08:00:55 +08:00
parent 56ad014199
commit e6622b32c7
7 changed files with 141 additions and 186 deletions
+4 -91
View File
@@ -8,100 +8,13 @@ import (
"runtime"
"time"
"bat-api/internal/configtemplate"
"github.com/pelletier/go-toml/v2"
)
// ConfigTOMLTemplate is the only template generated by bat-api. Rust bat
// accepts the same file and ignores the [api] section.
const ConfigTOMLTemplate = `# BlueArchive Toolkit shared application configuration.
# Priority: CLI flags > process environment > config.toml > built-in defaults.
# This file is the application configuration. The application never reads .env.
#
# Rust bat consumes [runtime], [resource], [localized], [repository], [network]
# and [translation.worker]. Go bat-api consumes [api]. Each binary ignores the
# other application's section.
[runtime]
state_dir = '/tmp/bat-pid'
interval_seconds = 3600
error_retry_seconds = 60
quiet_up_to_date = false
output_format = 'human'
banner = true
progress = true
tail_lines = 200
[resource]
output_root = './bat-resources'
auto_discover = true
app_version = ''
connection_group = ''
launcher_version = '1.7.2'
platforms = ['windows', 'android']
snapshot_path = ''
dry_run = false
plan = false
force = false
audit_local = true
repair = true
[resource.server_info]
kind = 'none'
value = ''
[localized]
output_root = './bat-localized'
[repository]
import_repository = false
import_cas_root = ''
import_resource_repository_path = ''
[network]
curl_command = 'curl'
proxy = 'auto'
unzip_command = 'unzip'
zip_command = 'zip'
download_concurrency = 8
[translation.worker]
provider = 'mock'
fixture = ''
translation_memory_path = ''
glossary_path = ''
concurrency = 8
max_attempts = 3
lease_seconds = 300
retry_backoff_seconds = 5
max_tasks = ''
worker_id = ''
[api]
listen = ':18080'
public_base_url = 'http://127.0.0.1:18080'
state_dir = '/tmp/bat-pid'
socket_path = ''
resource_root = ''
server_info_file = ''
require_indexed = true
verify_size = true
rpc_timeout = '30s'
refresh_interval = '1m'
auth_token = ''
auth_query_param = 'bat_token'
auth_exempt_paths = []
trust_proxy_headers = false
access_log = false
rate_limit_rps = 0
rate_limit_burst = 0
max_resource_page_limit = 1000
# Reserved for a future API persistence layer.
database_url = ''
database_password = ''
redis_url = ''
redis_password = ''
`
// ConfigTOMLTemplate is the canonical template generated by bat-api. Rust
// bat consumes the same file and ignores the [api] section.
var ConfigTOMLTemplate = configtemplate.Template
// LoadConfigFromCurrentExe loads config.toml next to the running binary.
func LoadConfigFromCurrentExe(cfg *Config) error {
+19
View File
@@ -27,12 +27,31 @@ func TestLoadConfigFromBinaryDirFirstLaunchCreatesExampleOnly(t *testing.T) {
if _, err := os.Stat(filepath.Join(dir, ConfigExampleName)); err != nil {
t.Fatalf("config.toml.example was not created: %v", err)
}
examplePath := filepath.Join(dir, ConfigExampleName)
example, err := os.ReadFile(examplePath)
if err != nil {
t.Fatal(err)
}
if string(example) != ConfigTOMLTemplate {
t.Fatal("first-launch example differs from the canonical runtime template")
}
if _, err := os.Stat(filepath.Join(dir, ".env")); !os.IsNotExist(err) {
t.Fatalf("unexpected .env after first launch, err=%v", err)
}
if cfg.Listen != DefaultListen || cfg.RPCTimeout != 30*time.Second {
t.Fatalf("first launch changed defaults: %+v", cfg)
}
if err := LoadConfigFromBinaryDir(dir, &cfg); err != nil {
t.Fatal(err)
}
unchanged, err := os.ReadFile(examplePath)
if err != nil {
t.Fatal(err)
}
if string(unchanged) != string(example) {
t.Fatal("existing config.toml.example was modified")
}
}
func TestCheckedInBatAPIConfigExampleMatchesRuntimeTemplate(t *testing.T) {