f96f23360c
- 增加了运行时自动启动上传配置选项 - 实现了日志输出的滚动控制功能 - 优化了上传进度显示和状态同步机制 - 提升了HTTP客户端连接池配置至500 - 改进了文件上传完成后的清理逻辑 - 添加了上下文取消检查避免资源泄露 - 完善了上传开始时的日志信息输出
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"dypid-client/internal/config"
|
|
"dypid-client/internal/uploader"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// App struct
|
|
type App struct {
|
|
ctx context.Context
|
|
logChan chan string
|
|
uploaderCTX context.Context
|
|
uploaderCancel context.CancelFunc
|
|
isRun bool
|
|
}
|
|
|
|
func NewApp() *App {
|
|
return &App{}
|
|
}
|
|
|
|
// startup 程序初始化
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
|
|
// 后台 goroutine 持续推送日志
|
|
a.logChan = make(chan string, 100)
|
|
go func() {
|
|
for log := range a.logChan {
|
|
runtime.EventsEmit(a.ctx, "log", log)
|
|
}
|
|
}()
|
|
|
|
// 后台 goroutine 持续推送运行状态
|
|
go func() {
|
|
for {
|
|
time.Sleep(500 * time.Millisecond)
|
|
runtime.EventsEmit(a.ctx, "is-run", a.isRun)
|
|
}
|
|
}()
|
|
|
|
//在程序启动时运行上传程序
|
|
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
|
if config.APPConfig.IsRunOnStart {
|
|
time.Sleep(time.Second)
|
|
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
|
a.isRun = true
|
|
}
|
|
}
|
|
|
|
// SelectPath 打开选择路径弹框
|
|
func (a *App) SelectPath() string {
|
|
dialog, _ := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
|
|
fmt.Println("选择路径:", dialog)
|
|
return dialog
|
|
}
|
|
|
|
func (a *App) GetConfig() config.Config {
|
|
return config.APPConfig
|
|
}
|
|
|
|
func (a *App) WriteConfig(key string, value any) {
|
|
fmt.Println(key, value)
|
|
config.WriteConfig(key, value)
|
|
}
|
|
|
|
func (a *App) StartUpload() {
|
|
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 a.isRun {
|
|
a.uploaderCancel()
|
|
}
|
|
a.isRun = false
|
|
uploader.AddLog(&a.logChan, "上传程序已退出")
|
|
}
|