(ctx context.Context, configFile ...string)
| 68 | } |
| 69 | |
| 70 | func Init(ctx context.Context, configFile ...string) error { |
| 71 | viper.SetConfigType("toml") |
| 72 | viper.SetEnvPrefix("SAVEANY") |
| 73 | viper.AutomaticEnv() |
| 74 | replacer := strings.NewReplacer(".", "_") |
| 75 | viper.SetEnvKeyReplacer(replacer) |
| 76 | |
| 77 | // 如果指定了配置文件路径,则使用指定的配置文件 |
| 78 | // 配置文件支持传入一个 http(s) URL 地址 |
| 79 | if len(configFile) > 0 && configFile[0] != "" { |
| 80 | cfg := configFile[0] |
| 81 | if strings.HasPrefix(cfg, "http://") || strings.HasPrefix(cfg, "https://") { |
| 82 | // 使用远程配置文件 |
| 83 | resp, err := http.Get(cfg) |
| 84 | if err != nil { |
| 85 | return fmt.Errorf("failed to fetch remote config file: %w", err) |
| 86 | } |
| 87 | defer resp.Body.Close() |
| 88 | if resp.StatusCode != http.StatusOK { |
| 89 | return fmt.Errorf("failed to fetch remote config file: status code %d", resp.StatusCode) |
| 90 | } |
| 91 | if err := viper.ReadConfig(resp.Body); err != nil { |
| 92 | return fmt.Errorf("failed to read remote config file: %w", err) |
| 93 | } |
| 94 | } else { |
| 95 | viper.SetConfigFile(cfg) |
| 96 | } |
| 97 | } else { |
| 98 | viper.SetConfigName("config") |
| 99 | viper.AddConfigPath(".") |
| 100 | viper.AddConfigPath("/etc/saveany/") |
| 101 | } |
| 102 | |
| 103 | defaultConfigs := map[string]any{ |
| 104 | // 基础配置 |
| 105 | "lang": "zh-Hans", |
| 106 | "workers": 3, |
| 107 | "retry": 3, |
| 108 | "threads": 4, |
| 109 | "log.level": "debug", |
| 110 | |
| 111 | // 缓存配置 |
| 112 | "cache.ttl": 86400, |
| 113 | "cache.num_counters": 1e5, |
| 114 | "cache.max_cost": 1e6, |
| 115 | |
| 116 | // Telegram |
| 117 | "telegram.app_id": 1025907, |
| 118 | "telegram.app_hash": "452b0359b988148995f22ff0f4229750", |
| 119 | "telegram.rpc_retry": 5, |
| 120 | "telegram.userbot.enable": false, |
| 121 | "telegram.userbot.session": "data/usersession.db", |
| 122 | |
| 123 | // 临时目录 |
| 124 | "temp.base_path": "cache/", |
| 125 | |
| 126 | // 数据库 |
| 127 | "db.path": "data/saveany.db", |
no test coverage detected