qcc/cmd/qccd/config.go
2026-06-30 12:52:09 +03:00

54 lines
1.4 KiB
Go

package qccd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("./data")
}
viper.SetEnvPrefix("QCC")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
viper.SetDefault("server.listen", ":1963")
viper.SetDefault("acme.type", "dns")
viper.SetDefault("pow.difficulty", 22)
viper.SetDefault("pow.challenge_ttl", "30s")
viper.SetDefault("identity.cooldown", "24h")
viper.SetDefault("identity.prefix", "+0")
viper.SetDefault("ca.key_type", "ed25519")
viper.SetDefault("ca.validity", "87600h")
viper.SetDefault("relay.bandwidth_up", "100 mbps")
viper.SetDefault("relay.bandwidth_down", "100 mbps")
viper.SetDefault("relay.media_timeout", "30s")
viper.SetDefault("data_dir", "./data")
viper.SetDefault("log_level", "info")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok && cfgFile == "" {
fmt.Fprintln(os.Stderr, "Warning: config.yaml not found, using defaults")
} else {
fmt.Fprintf(os.Stderr, "Error reading config file: %s\n", err)
os.Exit(1)
}
}
dataDir := viper.GetString("data_dir")
if err := os.MkdirAll(filepath.Join(dataDir, "acme"), 0700); err != nil {
fmt.Fprintf(os.Stderr, "Error creating data directory: %s\n", err)
os.Exit(1)
}
}