-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
121 lines (94 loc) · 2.48 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/exec"
rice "github.com/GeertJohan/go.rice"
"github.com/getlantern/systray"
"github.com/jangxx/go-poclient"
"github.com/shibukawa/configdir"
)
const APP_NAME string = "SchubsiGo"
const APP_VERSION string = "1.4.0"
var config Config
var server *http.Server
var pushover *poclient.Client
var messages map[int]poclient.Message
var pushover_retry = make(chan bool)
var quit_channel = make(chan bool)
var maxConnectRetries int
func main() {
log.Printf("Starting %s %s\n", APP_NAME, APP_VERSION)
disableTrayIcon := flag.Bool("no-tray", false, "Disables the tray icon")
maxConnectRetriesFlag := flag.Int("max-connect-retries", 5, "Maximum number of retries to connect to the Pushover API")
flag.Parse()
maxConnectRetries = *maxConnectRetriesFlag
log.Printf("Max connect retries: %d\n", maxConnectRetries)
if maxConnectRetries < 0 {
log.Fatalf("Invalid value for max-connect-retries: %d\n", maxConnectRetries)
}
if !*disableTrayIcon {
systray.Run(onReadySystray, onExit)
} else {
onReady(true)
}
}
func onReadySystray() {
onReady(false)
}
func onReady(noTray bool) {
iconBox := rice.MustFindBox("./icon")
icondata := iconBox.MustBytes("icon_64.png")
if !noTray {
systray.SetTitle("SchubsiGo")
systray.SetTooltip("SchubsiGo")
systray.SetTemplateIcon(icondata, icondata)
}
go func() {
configDirs := configdir.New("literalchaos", "schubsigo")
config, _ = loadConfig(configDirs, "settings.json")
if !noTray {
mVersion := systray.AddMenuItem("Version: "+APP_VERSION, "Version of the app")
mVersion.Disable()
mOpenWeb := systray.AddMenuItem("Open Web Interface", "Open the web interface")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
for {
select {
case <-mOpenWeb.ClickedCh:
exec.Command("xdg-open", "http://"+config.Webserver.Addr+":"+config.Webserver.Port).Start()
case <-mQuit.ClickedCh:
quit_channel <- true
}
}
}()
}
messages = make(map[int]poclient.Message)
pushover = initPOClient(config, maxConnectRetries)
initNotifications()
go listenForMessages(pushover)
go listenForNotifications(pushover)
server = initWebserver(config.Webserver)
}()
go func() {
<-quit_channel
if noTray {
onExit()
os.Exit(0)
} else {
systray.Quit()
}
}()
if noTray {
select {} // block forever
}
}
func onExit() {
if server != nil {
server.Shutdown(context.Background())
log.Printf("Server has shut down")
}
}