-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
123 lines (115 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"chatcat/backend/config"
"chatcat/backend/pkg/chttp"
"chatcat/backend/pkg/clog"
"chatcat/backend/pkg/cws"
"chatcat/backend/service"
"chatcat/backend/service/chat"
"chatcat/backend/service/prompt"
"chatcat/backend/service/setting"
"chatcat/backend/service/version"
"embed"
"encoding/json"
"errors"
"fmt"
gowebsocket "github.com/MQEnergy/go-websocket"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"net/url"
"os"
"runtime"
"time"
)
//go:embed all:frontend/dist
var assets embed.FS
var (
env = "test"
app *service.App
frameless = true
)
func main() {
if env != "test" && env != "prod" {
panic(errors.New("环境变量异常,只能设置test、prod"))
}
// Create an instance of the app structure
config.ConfEnv = env
app = service.NewApp()
app.LogInfo("env:", env)
app.LogInfo("sqlite db:", app.GetDatabasePath())
app.LogInfo("runtime path:", clog.GetRuntimePath())
// initialize groutine
InitGoroutine()
// frameless
if runtime.GOOS == "darwin" {
frameless = false
}
// Create application with options
if err := wails.Run(&options.App{
Title: app.Cfg.App.AppName,
Width: app.Cfg.App.Width,
Height: app.Cfg.App.Height,
MinWidth: app.Cfg.App.MinWidth, // 最小宽度
MinHeight: app.Cfg.App.MinHeight, // 最小高度
MaxWidth: app.Cfg.App.Width * 10, // 最大宽度
MaxHeight: app.Cfg.App.Height * 10, // 最大高度
DisableResize: false, // 调整窗口尺寸
Frameless: frameless, // 无边框
StartHidden: false, // 启动后隐藏
HideWindowOnClose: false, // 关闭窗口将隐藏而不退出应用程序
LogLevel: logger.DEBUG, // 日志级别
OnStartup: app.OnStartUp,
OnDomReady: app.OnDomReady,
OnBeforeClose: app.OnBeforeClose, //
OnShutdown: app.OnShutdown, //
AssetServer: &assetserver.Options{
Assets: assets,
},
Mac: &mac.Options{
TitleBar: mac.TitleBarHiddenInset(),
WebviewIsTransparent: true,
WindowIsTranslucent: true,
},
//BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
Bind: []interface{}{
chat.New(app),
prompt.New(app),
setting.New(app),
version.New(app),
},
}); err != nil {
app.LogErrorf("Error: %s", err.Error())
panic(err)
}
}
// InitGoroutine ...
func InitGoroutine() {
// start websocket :9991
hub := gowebsocket.NewHub()
go cws.Hub(hub, app)
go hub.Run()
go func() {
for {
select {
// ws push
case pushInfo := <-app.WsPushChan:
payload, _ := json.Marshal(pushInfo)
params := url.Values{}
params.Add("client_id", app.ClientId)
params.Add("group_id", "chat")
params.Add("data", string(payload))
pushUrl := fmt.Sprintf("%s?%s", app.Cfg.App.PushUrl, params.Encode())
chttp.Request("GET", pushUrl, "")
case exitSignal := <-app.ExitSignalChan:
if exitSignal {
app.LogInfof("ExitSignalChan received:%v", exitSignal)
time.Sleep(time.Second * 1)
os.Exit(0)
}
}
}
}()
}