-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
179 lines (147 loc) · 3.81 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
173
174
175
176
177
178
179
package main
import (
"bytes"
"fmt"
"geoweaver-systray/utils"
"github.com/getlantern/systray"
"image/png"
"log"
"os"
"path/filepath"
"sync"
)
var serverStarted bool = false
func main() {
systray.Run(onReady, onExit)
}
func loadImageAsByteSlice(filePath string) ([]byte, error) {
// Open the file specified by the filePath
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
defer file.Close()
// Decode the PNG image
img, err := png.Decode(file)
if err != nil {
return nil, fmt.Errorf("error decoding PNG: %w", err)
}
// Encode the image to PNG format and store it in a byte buffer
var buffer bytes.Buffer
err = png.Encode(&buffer, img)
if err != nil {
return nil, fmt.Errorf("error encoding PNG to byte slice: %w", err)
}
// Return the bytes of the encoded image
return buffer.Bytes(), nil
}
func onReady() {
// Load Geoweaver logo
geoweaverLogo, err := loadImageAsByteSlice("./icon/geoweaver.png")
if err != nil {
return
}
systray.SetIcon(geoweaverLogo)
systray.SetTooltip("Geoweaver App for Workflow Management")
startServer := systray.AddMenuItem("Start Server", "Start Geoweaver server on the background")
openBrowser := systray.AddMenuItem("Open Browser", "Open geoweaver on your default browser")
restartServer := systray.AddMenuItem("Restart Server", "Restart Geoweaver server on the background")
systray.AddSeparator()
quitMenu := systray.AddMenuItem("Quit", "Quit the whole app")
// Sets the icon of a menu item. Only available on Mac and Windows.
browserIcon, err := loadImageAsByteSlice("./icon/browser.png")
if err != nil {
return
}
openBrowser.SetIcon(browserIcon)
restartServerIcon, err := loadImageAsByteSlice("./icon/restart.png")
if err != nil {
return
}
restartServer.SetIcon(restartServerIcon)
startServerIcon, err := loadImageAsByteSlice("./icon/start-server.png")
if err != nil {
return
}
startServer.SetIcon(startServerIcon)
quitIcon, err := loadImageAsByteSlice("./icon/close.png")
if err != nil {
return
}
quitMenu.SetIcon(quitIcon)
// handle quit
go func() {
<-quitMenu.ClickedCh
fmt.Print("Requesting quit")
_ = utils.KillGeoweaverProcesses()
systray.Quit()
}()
// handle server start and stop
go func() {
var mutex sync.Mutex
for {
<-startServer.ClickedCh
mutex.Lock()
currentState := serverStarted
mutex.Unlock()
if currentState {
startServer.SetTitle("Start server")
err := utils.KillGeoweaverProcesses()
if err != nil {
log.Printf("Failed to stop geoweaver server: %v", err)
continue
}
mutex.Lock()
serverStarted = false
mutex.Unlock()
} else {
startServer.SetTitle("Stop server")
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("Failed to get home directory: %v", err)
continue
}
geoweaverJarPath := filepath.Join(homeDir, "geoweaver.jar")
err = utils.DownloadFile(utils.GEOWEAVER_JAR_URL, geoweaverJarPath)
if err != nil {
log.Printf("Unable to download geoweaver.jar: %v", err)
continue
}
err = utils.RunJavaJar(geoweaverJarPath)
if err != nil {
log.Printf("Failed to start geoweaver jar file: %v", err)
continue
}
mutex.Lock()
serverStarted = true
mutex.Unlock()
}
}
}()
// handle URL open
go func() {
for {
<-openBrowser.ClickedCh
err := utils.OpenURLInBrowser("http://localhost:8070/Geoweaver")
if err != nil {
panic("Failed to open browser")
}
}
}()
// handle restart
go func() {
for {
<-restartServer.ClickedCh
_ = utils.KillGeoweaverProcesses()
homeDir, _ := os.UserHomeDir()
geoweaverJarPath := filepath.Join(homeDir, "geoweaver.jar")
err := utils.RunJavaJar(geoweaverJarPath)
if err != nil {
panic("Unable to start geoweaver")
}
}
}()
}
func onExit() {
// clean up here
}