-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
224 lines (181 loc) · 5.23 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
"github.com/getlantern/systray"
flag "github.com/spf13/pflag"
"github.com/varnamproject/desktop/icon"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/stuffbin"
)
type userConfig map[string]string
const (
downloadPageSize = 100
)
var (
kf = koanf.New(".")
appAddress string
varnamdConfig *config // config instance used across the application
syncDispatcherRunning bool
startedAt time.Time
buildVersion string
buildDate string
maxHandleCount int
authEnabled bool
// User accounts are stored here.
users map[string]userConfig
)
type appConfig struct {
Address string
EnableInternalApis bool `koanf:"enable-internal-api"` // internal APIs are not exposed to public
EnableSSL bool `koanf:"enable-ssl"`
CertFilePath string `koanf:"cert-path"`
KeyFilePath string `koanf:"key-file-path"`
DownloadEnabledSchemes string `koanf:"download-enabled-schemes"`
SyncInterval time.Duration `koanf:"sync-interval"`
UpstreamURL string `koanf:"upstream-url"`
// Web bool `koanf:"web"`
VstDir string `koanf:"vst-dir"`
}
// App is a singleton to share across handlers.
type App struct {
cache Cache
log *log.Logger
fs stuffbin.FileSystem
}
// varnamd configurations
// this is populated from various command line flags
type config struct {
upstream string
schemesToDownload map[string]bool
syncInterval time.Duration
}
func init() {
// Set max processors to number of CPUs to maximize performance.
runtime.GOMAXPROCS(runtime.NumCPU())
// Setup flags to read from user to start the application.
// Initialize 'config' flagset.
flagSet := flag.NewFlagSet("config", flag.ContinueOnError)
flagSet.Usage = func() {
log.Fatal(flagSet.FlagUsages())
}
// Create config flag to read 'config.toml' from user.
flagSet.String("config", "config.toml", "Path to the TOML configuration file")
flagSet.Int("p", 8080, "Run daemon in specified port")
flagSet.String("host", "", "Host for the varnam daemon server")
// Create flag for version check.
flagSet.Bool("version", false, "Current version of the build")
err := flagSet.Parse(os.Args[1:])
if err != nil {
log.Fatalf("error parsing flags: %v", err)
}
// Load commandline params user given.
if err = kf.Load(posflag.Provider(flagSet, ".", kf), nil); err != nil {
log.Fatal(err.Error())
}
// Handle --version flag. Print build version, build date and die.
if kf.Bool("version") {
fmt.Printf("Commit: %v\nBuild: %v\n", buildVersion, buildDate)
os.Exit(0)
}
// Load the config file.
log.Printf("reading config: %s", kf.String("config"))
if err = kf.Load(file.Provider(kf.String("config")), toml.Parser()); err != nil {
log.Printf("error reading config: %v", err)
}
}
func syncRequired() bool {
return len(varnamdConfig.schemesToDownload) > 0
}
// Starts the sync process only if it is not running
func startSyncDispatcher() {
if syncRequired() && !syncDispatcherRunning {
sync := newSyncDispatcher(varnamdConfig.syncInterval / time.Second)
sync.start()
sync.runNow() // run one round of sync immediatly rather than waiting for the next interval to occur
syncDispatcherRunning = true
}
}
func openInBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}
func onTrayReady() {
// systray.SetIcon(icon.Data)
systray.SetTitle("Varnam Desktop")
systray.SetTemplateIcon(icon.Data, icon.Data)
mOpen := systray.AddMenuItem("Open", "Open in browser")
go func() {
<-mOpen.ClickedCh
openInBrowser(appAddress)
}()
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
go func() {
<-mQuit.ClickedCh
fmt.Println("Requesting quit")
systray.Quit()
fmt.Println("Finished quitting")
}()
}
func main() {
config, err := initAppConfig()
if err != nil {
log.Fatal(err.Error())
}
if config.VstDir != "" {
vstDir, err := filepath.Abs(config.VstDir)
if err != nil {
log.Fatal(err.Error())
}
os.Setenv("VARNAM_VST_DIR", vstDir)
}
appAddress = "http://" + config.Address
maxHandleCount = kf.Int("app.max-handles")
if maxHandleCount <= 0 {
maxHandleCount = 10
}
authEnabled = kf.Bool("app.accounts-enabled")
if authEnabled {
if err = kf.Unmarshal("users", &users); err != nil {
log.Fatal(err.Error())
}
}
varnamdConfig = initConfig(config)
startedAt = time.Now()
log.Printf("varnamd %s-%s", buildVersion, buildDate)
fs, err := initVFS()
if err != nil {
log.Fatal(err.Error())
}
app := &App{
cache: NewCache(),
log: log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile),
fs: fs,
}
startSyncDispatcher()
go startDaemon(app, config)
// Wait a bit for daemon to start
time.Sleep(2 * time.Second)
openInBrowser(appAddress)
systray.Run(onTrayReady, func() {})
}