-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (123 loc) · 2.89 KB
/
Copy pathmain.go
File metadata and controls
145 lines (123 loc) · 2.89 KB
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
package main
import (
"embed"
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
//go:embed frontend/dist
var assets embed.FS
//go:embed assets/tray-idle.png
var trayIdle []byte
//go:embed assets/tray-pending.png
var trayPending []byte
func main() {
logger, closeLog := initLogging()
defer closeLog()
unlock, err := acquireLock()
if err != nil {
logger.Warn("failed to acquire lock", "error", err)
os.Exit(1)
}
defer unlock()
state := NewAppState(logger)
app := application.New(application.Options{
Name: "askd",
Description: "MCP server with GUI for asking user questions",
Icon: trayPending,
Logger: logger,
Services: []application.Service{
application.NewService(state),
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: false,
},
Windows: application.WindowsOptions{
DisableQuitOnLastWindowClosed: true,
},
})
state.wailsApp = app
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "askd",
Width: 550,
Height: 600,
Hidden: true,
URL: "/",
})
state.window = window
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
e.Cancel()
window.Hide()
})
systray := app.SystemTray.New()
systray.SetLabel("askd")
systray.SetIcon(trayIdle)
systray.OnClick(func() {
if window.IsVisible() {
window.Hide()
} else {
window.Center()
window.Show()
window.Focus()
}
})
systray.OnRightClick(func() {
systray.OpenMenu()
})
state.systray = systray
menu := app.NewMenu()
menu.Add("Open").OnClick(func(ctx *application.Context) {
window.Center()
window.Show()
window.Focus()
})
menu.AddSeparator()
menu.Add("Quit").OnClick(func(ctx *application.Context) {
app.Quit()
})
systray.SetMenu(menu)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigCh
app.Quit()
}()
if err := app.Run(); err != nil {
logger.Error("application error", "error", err)
os.Exit(1)
}
}
func acquireLock() (func(), error) {
home, err := os.UserHomeDir()
if err != nil {
home = "."
}
lockDir := filepath.Join(home, ".askd")
if err := os.MkdirAll(lockDir, 0755); err != nil {
return nil, fmt.Errorf("create lock dir: %w", err)
}
lockPath := filepath.Join(lockDir, "lock")
data, readErr := os.ReadFile(lockPath)
if readErr == nil {
var pid int
fmt.Sscanf(string(data), "%d", &pid)
if pid > 0 && isProcessAlive(pid) {
killProcess(pid)
}
os.Remove(lockPath)
}
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("acquire lock: %w", err)
}
fmt.Fprintf(f, "%d", os.Getpid())
f.Close()
return func() { os.Remove(lockPath) }, nil
}