26afd30e84
- 添加 Go 后端实现,包括配置管理、文件上传逻辑和 Wails 应用接口 - 实现前端 Vue 界面,提供服务器配置、目录选择、上传控制等功能 - 集成 Element Plus 组件库构建用户界面 - 添加文件上传进度显示和实时日志输出功能 - 实现后台文件监控和上传任务管理 - 配置 Wails 框架支持前后端交互 - 更新项目依赖,移除 Fyne 框架,集成 Wails v2 - 添加项目配置文件管理和自动保存功能
67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Config struct {
|
|
Url string `json:"url" mapstructure:"url"`
|
|
Token string `json:"token" mapstructure:"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"`
|
|
}
|
|
|
|
var APPConfig Config
|
|
|
|
func InitConfig() {
|
|
// 设置默认配置
|
|
defaultConfig := Config{
|
|
Url: "http://127.0.0.1:8080",
|
|
Token: "",
|
|
ThreadCount: 10,
|
|
HandleFileCount: 25,
|
|
IsRunOnStart: false,
|
|
CheckDir: "",
|
|
}
|
|
viper.SetDefault("url", defaultConfig.Url)
|
|
viper.SetDefault("token", defaultConfig.Token)
|
|
viper.SetDefault("thread-count", defaultConfig.ThreadCount)
|
|
viper.SetDefault("handle-file-count", defaultConfig.HandleFileCount)
|
|
viper.SetDefault("is-run-on-start", defaultConfig.IsRunOnStart)
|
|
viper.SetDefault("looking-path", defaultConfig.CheckDir)
|
|
|
|
//设置配置文件名和路径 ./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()
|
|
}
|