feat: 初步完成GUI界面添加
Some checks failed
构建上传工具 / build-tool (push) Failing after 2m9s

This commit is contained in:
2025-10-13 21:32:02 +08:00
parent 3b6705d6ef
commit cccb31cbc5
10 changed files with 607 additions and 74 deletions

63
config/config.go Normal file
View File

@@ -0,0 +1,63 @@
package config
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Config struct {
Url string `mapstructure:"url"`
Token string `mapstructure:"token"`
ThreadCount int `mapstructure:"thread-count"`
IsRunOnStart bool `mapstructure:"is-run-on-start"`
LookingPath string `mapstructure:"looking-path"`
}
var APPConfig Config
func InitConfig() {
// 设置默认配置
defaultConfig := Config{
Url: "http://127.0.0.1:8080",
Token: "",
ThreadCount: 10,
IsRunOnStart: false,
LookingPath: "",
}
viper.SetDefault("url", defaultConfig.Url)
viper.SetDefault("token", defaultConfig.Token)
viper.SetDefault("thread-count", defaultConfig.ThreadCount)
viper.SetDefault("is-run-on-start", defaultConfig.IsRunOnStart)
viper.SetDefault("looking-path", defaultConfig.LookingPath)
//设置配置文件名和路径 ./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) {
viper.Set(key, value)
viper.WriteConfig()
}