-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.go
110 lines (86 loc) · 2.39 KB
/
init.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
package main
import (
"fmt"
"log"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/knadh/stuffbin"
)
func initConfig(cfg appConfig) *config {
toDownload := make(map[string]bool)
schemes := strings.Split(cfg.DownloadEnabledSchemes, ",")
for _, scheme := range schemes {
s := strings.TrimSpace(scheme)
if s != "" {
if !isValidSchemeIdentifier(s) {
panic(fmt.Sprintf("%s is not a valid libvarnam supported scheme", s))
}
toDownload[s] = true
}
}
return &config{upstream: cfg.UpstreamURL, schemesToDownload: toDownload,
syncInterval: time.Duration(cfg.SyncInterval)}
}
func (c *config) setDownloadStatus(langCode string, status bool) error {
if !isValidSchemeIdentifier(langCode) {
return fmt.Errorf("%s is not a valid libvarnam supported scheme", langCode)
}
c.schemesToDownload[langCode] = status
if status {
// when varnamd was started without any langcodes to sync, the dispatcher won't be running
// in that case, we need to start the dispatcher since we have a new lang code to download now
startSyncDispatcher()
}
return nil
}
func getConfigDir() string {
if runtime.GOOS == "windows" {
return path.Join(os.Getenv("localappdata"), ".varnamd")
}
return path.Join(os.Getenv("HOME"), ".varnamd")
}
// initVFS initializes the stuffbin virtual FileSystem to provide
// access to bunded static assets to the app.
func initVFS() (stuffbin.FileSystem, error) {
files := []string{
"ui:/",
}
binPath, err := os.Executable()
if err != nil {
return nil, err
}
fs, err := stuffbin.UnStuff(binPath)
if err != nil {
log.Printf("unable to initialize embedded filesystem: %v", err)
log.Printf("using local filesystem")
fs, err = stuffbin.NewLocalFS("/", files...)
if err != nil {
return nil, err
}
}
return fs, nil
}
func initAppConfig() (appConfig, error) {
var config appConfig
// Read configuration using Koanf.
if err := kf.Unmarshal("app", &config); err != nil {
return config, err
}
// If address is empty run on localhost port 8080.
if config.Address == "" {
config.Address = fmt.Sprintf("%s:%d", kf.String("host"), kf.Int("p"))
}
if config.EnableSSL && (config.CertFilePath == "" || config.KeyFilePath == "") {
config.EnableSSL = false
}
if config.SyncInterval < 1*time.Second {
config.SyncInterval = 30 * time.Second
}
if config.UpstreamURL == "" {
config.UpstreamURL = "https://api.varnamproject.com"
}
return config, nil
}