987f0236a9
构建上传工具 / build-tool (push) Successful in 1m16s
- 在 UploadDataToServer 函数中添加 context 参数支持 - 使用 http.NewRequest 替换 httpClient.Post 以更好地控制请求上下文 - 重构应用启动逻辑,在 StartUpload 中初始化上传器上下文 - 优化 StopUpload 方法中的上下文取消机制 - 移除上传过程中的 wg.Wait() 调用以改善并发性能
77 lines
1.5 KiB
Go
77 lines
1.5 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
|
|
}
|
|
|
|
var isRun = false
|
|
|
|
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)
|
|
}
|
|
}()
|
|
|
|
//在程序启动时运行上传程序
|
|
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)
|
|
}
|
|
}
|
|
|
|
// 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 isRun {
|
|
return
|
|
}
|
|
a.uploaderCTX, a.uploaderCancel = context.WithCancel(a.ctx)
|
|
go uploader.StartLooking(a.uploaderCTX, &a.logChan, config.APPConfig.CheckDir)
|
|
}
|
|
|
|
func (a *App) StopUpload() {
|
|
if isRun {
|
|
a.uploaderCancel()
|
|
}
|
|
}
|