构建上传工具 / build (push) Successful in 1m14s
- 注释掉本地回环地址配置 - 修改默认URL为生产环境地址 http://112.124.71.39:8080 - 保留其他配置项不变
93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Url string `json:"url" mapstructure:"url"`
|
|
Token string `json:"token" mapstructure:"token"`
|
|
HasVideoToken string `json:"has_video_token" mapstructure:"has-video-token"`
|
|
NoVideoToken string `json:"no_video_token" mapstructure:"no-video-token"`
|
|
ThreadCount int `json:"thread_count" mapstructure:"thread-count"`
|
|
HandleFileCount int `json:"handle_file_count" mapstructure:"handle-file-count"`
|
|
IsRunOnStart bool `json:"is_run_on_start" mapstructure:"is-run-on-start"`
|
|
CheckDir string `json:"check_dir" mapstructure:"check-dir"`
|
|
ClearFilesNoPrompt bool `json:"clear_files_no_prompt" mapstructure:"clear-files-no-prompt"`
|
|
}
|
|
|
|
var APPConfig Config
|
|
var configMu sync.Mutex
|
|
|
|
const (
|
|
Url = "url"
|
|
Token = "token"
|
|
HasVideoToken = "has-video-token"
|
|
NoVideoToken = "no-video-token"
|
|
ThreadCount = "thread-count"
|
|
HandleFileCount = "handle-file-count"
|
|
IsRunOnStart = "is-run-on-start"
|
|
CheckDir = "check-dir"
|
|
ClearFilesNoPrompt = "clear-files-no-prompt"
|
|
)
|
|
|
|
func InitConfig() {
|
|
// 设置默认配置
|
|
defaultConfig := Config{
|
|
//Url: "http://127.0.0.1:8080",
|
|
Url: "http://112.124.71.39:8080",
|
|
Token: "",
|
|
HasVideoToken: "1234",
|
|
NoVideoToken: "5678",
|
|
ThreadCount: 10,
|
|
HandleFileCount: 25,
|
|
IsRunOnStart: false,
|
|
CheckDir: "",
|
|
ClearFilesNoPrompt: false,
|
|
}
|
|
viper.SetDefault(Url, defaultConfig.Url)
|
|
viper.SetDefault(Token, defaultConfig.Token)
|
|
viper.SetDefault(HasVideoToken, defaultConfig.HasVideoToken)
|
|
viper.SetDefault(NoVideoToken, defaultConfig.NoVideoToken)
|
|
viper.SetDefault(ThreadCount, defaultConfig.ThreadCount)
|
|
viper.SetDefault(HandleFileCount, defaultConfig.HandleFileCount)
|
|
viper.SetDefault(IsRunOnStart, defaultConfig.IsRunOnStart)
|
|
viper.SetDefault(CheckDir, defaultConfig.CheckDir)
|
|
viper.SetDefault(ClearFilesNoPrompt, defaultConfig.ClearFilesNoPrompt)
|
|
|
|
//设置配置文件名和路径 ./config.toml
|
|
viper.AddConfigPath(".")
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("toml")
|
|
viper.SafeWriteConfig() //安全写入默认配置
|
|
|
|
//读取配置文件
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
fmt.Errorf("无法读取配置文件: %w", err)
|
|
}
|
|
if err := viper.Unmarshal(&APPConfig); err != nil {
|
|
fmt.Errorf("无法解析配置: %w", err)
|
|
}
|
|
|
|
viper.WatchConfig()
|
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
fmt.Errorf("无法读取配置文件: %w", err)
|
|
}
|
|
if err := viper.Unmarshal(&APPConfig); err != nil {
|
|
fmt.Errorf("无法解析配置: %w", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func WriteConfig(key string, value any) {
|
|
configMu.Lock()
|
|
viper.Set(key, value)
|
|
viper.WriteConfig()
|
|
configMu.Unlock()
|
|
}
|