-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
main.go
172 lines (149 loc) · 5.37 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"fmt"
"golang.design/x/hotkey/mainthread"
"os"
"runtime"
"strconv"
"strings"
"time"
"wox/i18n"
"wox/plugin"
"wox/resource"
"wox/setting"
"wox/ui"
"wox/util"
"wox/util/hotkey"
)
import _ "wox/plugin/host" // import all hosts
// import all system plugins
import _ "wox/plugin/system"
import _ "wox/plugin/system/app"
import _ "wox/plugin/system/calculator"
import _ "wox/plugin/system/llm"
func main() {
// logger depends on location, so location must be initialized first
locationErr := util.GetLocation().Init()
if locationErr != nil {
panic(locationErr)
}
ctx := util.NewTraceContext()
util.GetLogger().Info(ctx, "------------------------------")
util.GetLogger().Info(ctx, "Wox starting")
util.GetLogger().Info(ctx, fmt.Sprintf("golang version: %s", strings.ReplaceAll(runtime.Version(), "go", "")))
util.GetLogger().Info(ctx, fmt.Sprintf("wox data location: %s", util.GetLocation().GetWoxDataDirectory()))
util.GetLogger().Info(ctx, fmt.Sprintf("user data location: %s", util.GetLocation().GetUserDataDirectory()))
serverPort := 34987
if util.IsProd() {
availablePort, portErr := util.GetAvailableTcpPort(ctx)
if portErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to get server port: %s", portErr.Error()))
return
}
serverPort = availablePort
}
util.GetLogger().Info(ctx, fmt.Sprintf("server port: %d", serverPort))
// check if there is existing instance running
if existingPort := getExistingInstancePort(ctx); existingPort > 0 {
util.GetLogger().Error(ctx, fmt.Sprintf("there is existing instance running, port: %d", existingPort))
_, postShowErr := util.HttpPost(ctx, fmt.Sprintf("http://localhost:%d/show", existingPort), "")
if postShowErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to show existing instance: %s", postShowErr.Error()))
} else {
util.GetLogger().Info(ctx, "show existing instance successfully, bye~")
}
return
} else {
util.GetLogger().Info(ctx, "no existing instance found")
writeErr := os.WriteFile(util.GetLocation().GetAppLockPath(), []byte(fmt.Sprintf("%d", serverPort)), 0644)
if writeErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to write lock file: %s", writeErr.Error()))
}
}
extractErr := resource.Extract(ctx)
if extractErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to extract embed file: %s", extractErr.Error()))
return
}
settingErr := setting.GetSettingManager().Init(ctx)
if settingErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to initialize settings: %s", settingErr.Error()))
return
}
woxSetting := setting.GetSettingManager().GetWoxSetting(ctx)
langErr := i18n.GetI18nManager().UpdateLang(ctx, woxSetting.LangCode)
if langErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to initialize lang(%s): %s", woxSetting.LangCode, langErr.Error()))
return
}
themeErr := ui.GetUIManager().Start(ctx)
if themeErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to initialize themes: %s", themeErr.Error()))
return
}
if woxSetting.ShowTray {
ui.GetUIManager().ShowTray()
}
shareUI := ui.GetUIManager().GetUI(ctx)
plugin.GetPluginManager().Start(ctx, shareUI)
util.InitSelection()
// hotkey must be registered in main thread
mainthread.Init(func() {
registerMainHotkeyErr := ui.GetUIManager().RegisterMainHotkey(ctx, woxSetting.MainHotkey.Get())
if registerMainHotkeyErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to register main hotkey: %s", registerMainHotkeyErr.Error()))
}
registerSelectionHotkeyErr := ui.GetUIManager().RegisterSelectionHotkey(ctx, woxSetting.SelectionHotkey.Get())
if registerSelectionHotkeyErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to register selection hotkey: %s", registerSelectionHotkeyErr.Error()))
}
for _, queryHotkey := range woxSetting.QueryHotkeys.Get() {
registerQueryHotkeyErr := ui.GetUIManager().RegisterQueryHotkey(ctx, queryHotkey)
if registerQueryHotkeyErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to register query hotkey: %s", registerQueryHotkeyErr.Error()))
}
}
t := hotkey.Hotkey{}
t.Register(ctx, "ctrl+ctrl", func() {
//files := plugin.GetPluginManager().GetUI().PickFiles(ctx, share.PickFilesParams{IsDirectory: true})
//ui.GetUIManager().GetUI(ctx).Notify(ctx, "Picked files", fmt.Sprintf("%v", files))
})
if util.IsProd() {
util.Go(ctx, "start ui", func() {
time.Sleep(time.Millisecond * 200) // wait websocket server start
appErr := ui.GetUIManager().StartUIApp(ctx, serverPort)
if appErr != nil {
util.GetLogger().Error(ctx, fmt.Sprintf("failed to start ui app: %s", appErr.Error()))
return
}
})
}
ui.GetUIManager().StartWebsocketAndWait(ctx, serverPort)
})
}
// retrieves the instance port from the existing instance lock file.
// It returns 0 if the lock file doesn't exist or fails to read the file.
func getExistingInstancePort(ctx context.Context) int {
filePath := util.GetLocation().GetAppLockPath()
if !util.IsFileExists(filePath) {
return 0
}
file, err := os.ReadFile(filePath)
if err != nil {
return 0
}
port, err := strconv.Atoi(string(file))
if err != nil {
return 0
}
//check if the port is valid
response, err := util.HttpGet(ctx, fmt.Sprintf("http://localhost:%d/ping", port))
if err != nil {
return 0
}
if !strings.Contains(string(response), "pong") {
return 0
}
return port
}