From 9fd888d5ff3f56def5108271ec4af0adf9633f36 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Wed, 12 Jul 2023 00:35:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90=E5=86=85=E7=BD=AEhttp?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + app/wails/app.go | 106 ++++- app/wails/cmd/server/main.go | 7 + app/wails/frontend/src/App.vue | 2 +- app/wails/frontend/src/views/home/Home.vue | 61 +-- app/wails/frontend/src/views/home/Test.vue | 22 +- app/wails/frontend/src/views/home/index.vue | 2 +- app/wails/frontend/vite.config.js | 9 +- app/wails/frontend/wails-plugin.ts | 36 ++ app/wails/frontend/wailsjs/go/main/App.d.ts | 4 +- app/wails/frontend/wailsjs/go/main/App.js | 8 +- app/wails/global/global.go | 5 + app/wails/go.mod | 45 +- app/wails/go.sum | 157 ++++++- app/wails/main.go | 14 +- app/wails/pkg/config/config.go | 28 ++ app/wails/pkg/config/toml/config.go | 50 +++ app/wails/pkg/config/toml/toml_test.go | 10 + app/wails/{lib => pkg}/env/env.go | 0 app/wails/{lib => pkg}/general_test.go | 2 +- app/wails/pkg/logger/adapter.go | 63 +++ app/wails/pkg/logger/logger.go | 56 +++ app/wails/pkg/logger/logger_test.go | 17 + app/wails/pkg/pid/pid.go | 24 ++ app/wails/pkg/pid/pid_test.go | 21 + app/wails/pkg/pid/pid_unix.go | 36 ++ app/wails/pkg/pid/pid_windows.go | 119 +++++ app/wails/pkg/sdk/system/file/manager.go | 168 ++++++++ app/wails/pkg/sdk/system/io.go | 11 + app/wails/pkg/sdk/system/io_test.go | 75 ++++ app/wails/pkg/sdk/system/system.go | 68 +++ app/wails/pkg/server/controller/config.go | 22 + app/wails/pkg/server/controller/file.go | 88 ++++ app/wails/pkg/server/controller/wol.go | 43 ++ app/wails/pkg/server/docs/docs.go | 407 ++++++++++++++++++ app/wails/pkg/server/docs/swagger.json | 384 +++++++++++++++++ app/wails/pkg/server/docs/swagger.yaml | 248 +++++++++++ app/wails/pkg/server/generate.go | 3 + app/wails/pkg/server/init.go | 10 + app/wails/pkg/server/main.go | 70 +++ app/wails/pkg/server/middleware/cors.go | 14 + app/wails/pkg/server/middleware/error.go | 39 ++ app/wails/pkg/server/router/error.go | 26 ++ app/wails/pkg/server/router/router.go | 36 ++ app/wails/pkg/server/router/swagger.go | 11 + app/wails/pkg/server/server.go | 28 ++ app/wails/pkg/server/services/wol/wol.go | 87 ++++ app/wails/pkg/server/services/wol/wol_test.go | 92 ++++ app/wails/{lib => pkg}/sqlite/sqlite.go | 2 +- app/wails/{lib => pkg}/system/cpu.go | 0 app/wails/{lib => pkg}/system/disk.go | 0 app/wails/{lib => pkg}/system/memory.go | 0 app/wails/{lib => pkg}/system/network.go | 0 app/wails/{lib => pkg}/system/process.go | 0 app/wails/{lib => pkg}/system/system.go | 0 app/wails/{lib => pkg}/system/system_test.go | 2 +- app/wails/pkg/utils/errorx/errorx.go | 59 +++ app/wails/pkg/utils/idutils/sonyflake.go | 20 + app/wails/pkg/utils/resources/relativefs.go | 27 ++ app/wails/pkg/utils/response/response.go | 40 ++ app/wails/pkg/utils/time/time.go | 31 ++ app/wails/{lib => pkg}/utils/utils.go | 0 app/wails/pkg/utils/validator/validator.go | 44 ++ 63 files changed, 2979 insertions(+), 82 deletions(-) create mode 100644 app/wails/cmd/server/main.go create mode 100644 app/wails/frontend/wails-plugin.ts create mode 100644 app/wails/global/global.go create mode 100644 app/wails/pkg/config/config.go create mode 100644 app/wails/pkg/config/toml/config.go create mode 100644 app/wails/pkg/config/toml/toml_test.go rename app/wails/{lib => pkg}/env/env.go (100%) rename app/wails/{lib => pkg}/general_test.go (99%) create mode 100644 app/wails/pkg/logger/adapter.go create mode 100644 app/wails/pkg/logger/logger.go create mode 100644 app/wails/pkg/logger/logger_test.go create mode 100644 app/wails/pkg/pid/pid.go create mode 100644 app/wails/pkg/pid/pid_test.go create mode 100644 app/wails/pkg/pid/pid_unix.go create mode 100644 app/wails/pkg/pid/pid_windows.go create mode 100644 app/wails/pkg/sdk/system/file/manager.go create mode 100644 app/wails/pkg/sdk/system/io.go create mode 100644 app/wails/pkg/sdk/system/io_test.go create mode 100644 app/wails/pkg/sdk/system/system.go create mode 100644 app/wails/pkg/server/controller/config.go create mode 100644 app/wails/pkg/server/controller/file.go create mode 100644 app/wails/pkg/server/controller/wol.go create mode 100644 app/wails/pkg/server/docs/docs.go create mode 100644 app/wails/pkg/server/docs/swagger.json create mode 100644 app/wails/pkg/server/docs/swagger.yaml create mode 100644 app/wails/pkg/server/generate.go create mode 100644 app/wails/pkg/server/init.go create mode 100644 app/wails/pkg/server/main.go create mode 100644 app/wails/pkg/server/middleware/cors.go create mode 100644 app/wails/pkg/server/middleware/error.go create mode 100644 app/wails/pkg/server/router/error.go create mode 100644 app/wails/pkg/server/router/router.go create mode 100644 app/wails/pkg/server/router/swagger.go create mode 100644 app/wails/pkg/server/server.go create mode 100644 app/wails/pkg/server/services/wol/wol.go create mode 100644 app/wails/pkg/server/services/wol/wol_test.go rename app/wails/{lib => pkg}/sqlite/sqlite.go (97%) rename app/wails/{lib => pkg}/system/cpu.go (100%) rename app/wails/{lib => pkg}/system/disk.go (100%) rename app/wails/{lib => pkg}/system/memory.go (100%) rename app/wails/{lib => pkg}/system/network.go (100%) rename app/wails/{lib => pkg}/system/process.go (100%) rename app/wails/{lib => pkg}/system/system.go (100%) rename app/wails/{lib => pkg}/system/system_test.go (97%) create mode 100644 app/wails/pkg/utils/errorx/errorx.go create mode 100644 app/wails/pkg/utils/idutils/sonyflake.go create mode 100644 app/wails/pkg/utils/resources/relativefs.go create mode 100644 app/wails/pkg/utils/response/response.go create mode 100644 app/wails/pkg/utils/time/time.go rename app/wails/{lib => pkg}/utils/utils.go (100%) create mode 100644 app/wails/pkg/utils/validator/validator.go diff --git a/.gitignore b/.gitignore index cc26017..d614fa7 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,5 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +config.toml +.pid diff --git a/app/wails/app.go b/app/wails/app.go index af53038..3d200ee 100644 --- a/app/wails/app.go +++ b/app/wails/app.go @@ -3,25 +3,121 @@ package main import ( "context" "fmt" + "github.com/wailsapp/wails/v2/pkg/runtime" + "go.uber.org/zap/zapcore" + "net" + "net/http" + "os" + "skapp/global" + "skapp/pkg/config/toml" + "skapp/pkg/logger" + "skapp/pkg/pid" + server "skapp/pkg/server" + "time" ) +var pidFile = "./.pid" + // App struct type App struct { - ctx context.Context + ctx context.Context + srv *http.Server + Handler http.Handler + pidLock *pid.PidLock } // NewApp creates a new App application struct func NewApp() *App { - return &App{} + app := &App{} + app.load() + return app +} + +func (a *App) ReloadApp() { + runtime.WindowReloadApp(a.ctx) +} + +func (a *App) ForceClose() { + a.shutdown(a.ctx) + os.Exit(0) +} + +func (a *App) load() { + a.pidLock = pid.NewPidLock(pidFile) + + conf, err := toml.LoadConfig() + if err != nil { + logger.Log.Fatalf("%s", err) + } + + global.Config = conf + + if conf.HasDebug() && conf.Debug.Enable { + logger.SetLevel(zapcore.DebugLevel) + } else { + logger.SetLevel(zapcore.ErrorLevel) + } + + engine := server.Server(conf) + + a.srv = &http.Server{ + Addr: fmt.Sprintf("%s:%d", conf.Server.Host, conf.Server.Port), + Handler: engine, + } + + a.Handler = engine.Handler() } // startup is called when the app starts. The context is saved // so we can call the runtime methods func (a *App) startup(ctx context.Context) { + if !global.Config.HasDebug() || !global.Config.Debug.Enable { + err := a.pidLock.Lock() + if err != nil { + os.Exit(-1) + } + } + a.ctx = ctx + + go func() { + logger.Log.Infof("启动本地后台服务: %s", a.srv.Addr) + // 服务连接 + if err := a.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + switch err.(type) { + case *net.OpError: + _, _ = runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ + Title: "错误", + Type: runtime.ErrorDialog, + Message: fmt.Sprintf("服务启动失败: 请检查 %s 是否被其他进程占用", a.srv.Addr), + }) + default: + _, _ = runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ + Title: "错误", + Type: runtime.ErrorDialog, + Message: err.Error(), + }) + } + + logger.Log.Fatalf("listen: %s\n", err) + os.Exit(-1) + } + }() } -// Greet returns a greeting for the given name -func (a *App) Greet(name string) string { - return fmt.Sprintf("Hello %s, It's show time!", name) +func (a *App) beforeClose(ctx context.Context) bool { + return false +} + +func (a *App) shutdown(ctx context.Context) { + ctx2, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if !global.Config.HasDebug() || !global.Config.Debug.Enable { + a.pidLock.UnLock() + } + + if err := a.srv.Shutdown(ctx2); err != nil { + logger.Log.Fatalf("Server Shutdown:", err) + } } diff --git a/app/wails/cmd/server/main.go b/app/wails/cmd/server/main.go new file mode 100644 index 0000000..d6ef334 --- /dev/null +++ b/app/wails/cmd/server/main.go @@ -0,0 +1,7 @@ +package main + +import "skapp/pkg/server" + +func main() { + server.Main() +} diff --git a/app/wails/frontend/src/App.vue b/app/wails/frontend/src/App.vue index 39a9e1c..cf5192e 100644 --- a/app/wails/frontend/src/App.vue +++ b/app/wails/frontend/src/App.vue @@ -1,5 +1,5 @@