refactor(app): 重构应用状态管理和配置常量定义
- 将全局变量 isRun 移动到 App 结构体内部作为实例字段 - 在 config.go 中定义配置键名为常量,提高代码可维护性 - 使用结构体实例字段替代全局变量管理上传状态 - 修改 StartLooking 函数中的上下文取消处理逻辑 - 移除上传程序退出日志的重复记录
This commit is contained in:
@@ -16,10 +16,9 @@ type App struct {
|
||||
logChan chan string
|
||||
uploaderCTX context.Context
|
||||
uploaderCancel context.CancelFunc
|
||||
isRun bool
|
||||
}
|
||||
|
||||
var isRun = false
|
||||
|
||||
func NewApp() *App {
|
||||
return &App{}
|
||||
}
|
||||
@@ -40,8 +39,8 @@ func (a *App) startup(ctx context.Context) {
|
||||
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
||||
if config.APPConfig.IsRunOnStart {
|
||||
time.Sleep(time.Second)
|
||||
isRun = true
|
||||
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
||||
a.isRun = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,15 +61,18 @@ func (a *App) WriteConfig(key string, value any) {
|
||||
}
|
||||
|
||||
func (a *App) StartUpload() {
|
||||
if isRun {
|
||||
if a.isRun {
|
||||
return
|
||||
}
|
||||
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
||||
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
||||
a.isRun = true
|
||||
}
|
||||
|
||||
func (a *App) StopUpload() {
|
||||
if isRun {
|
||||
if a.isRun {
|
||||
a.uploaderCancel()
|
||||
}
|
||||
a.isRun = false
|
||||
uploader.AddLog(&a.logChan, "上传程序已退出")
|
||||
}
|
||||
|
||||
@@ -18,6 +18,15 @@ type Config struct {
|
||||
|
||||
var APPConfig Config
|
||||
|
||||
const (
|
||||
Url = "url"
|
||||
Token = "token"
|
||||
ThreadCount = "thread-count"
|
||||
HandleFileCount = "handle-file-count"
|
||||
IsRunOnStart = "is-run-on-start"
|
||||
CheckDir = "check-dir"
|
||||
)
|
||||
|
||||
func InitConfig() {
|
||||
// 设置默认配置
|
||||
defaultConfig := Config{
|
||||
@@ -28,12 +37,12 @@ func InitConfig() {
|
||||
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)
|
||||
viper.SetDefault(Url, defaultConfig.Url)
|
||||
viper.SetDefault(Token, defaultConfig.Token)
|
||||
viper.SetDefault(ThreadCount, defaultConfig.ThreadCount)
|
||||
viper.SetDefault(HandleFileCount, defaultConfig.HandleFileCount)
|
||||
viper.SetDefault(IsRunOnStart, defaultConfig.IsRunOnStart)
|
||||
viper.SetDefault(CheckDir, defaultConfig.CheckDir)
|
||||
|
||||
//设置配置文件名和路径 ./config.toml
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
@@ -33,8 +33,13 @@ type Progress struct {
|
||||
}
|
||||
|
||||
func StartLooking(ctx context.Context, logChan *chan string, lookingPath string) {
|
||||
//推送上传进度
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
var pg []Progress
|
||||
@@ -45,6 +50,7 @@ func StartLooking(ctx context.Context, logChan *chan string, lookingPath string)
|
||||
})
|
||||
runtime.EventsEmit(ctx, "progress", pg)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
@@ -52,7 +58,6 @@ func StartLooking(ctx context.Context, logChan *chan string, lookingPath string)
|
||||
select {
|
||||
case <-time.After(time.Minute):
|
||||
case <-ctx.Done():
|
||||
AddLog(logChan, "上传程序已退出")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user