-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (66 loc) · 1.66 KB
/
config.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
package main
import (
"encoding/json"
"log"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
defaultServerInfoPath = "ocs/v2.php/apps/serverinfo/api/v1/info"
)
type internalConfig struct {
TimeoutSeconds float64 `json:"timeout"`
NextcloudURL string `json:"nextcloud_url"`
AppendDefaultServerInfo bool `json:"append_default_serverinfo_path"`
SkipApps *bool `json:"skip_apps"`
SkipUpdate *bool `json:"skip_update"`
Listen string `json:"listen"`
}
type Config struct {
Timeout time.Duration
InfoURL *url.URL
Listen string
}
func NewConfig(filename string) *Config {
fd, err := os.Open(filename)
if err != nil {
log.Fatalln(err)
}
defer fd.Close()
config := &internalConfig{
TimeoutSeconds: 10,
Listen: ":9091",
AppendDefaultServerInfo: true,
}
if err := json.NewDecoder(fd).Decode(config); err != nil {
log.Fatalln(err)
}
infoURL, err := url.Parse(config.NextcloudURL)
if err != nil {
log.Fatalf("Unable to parse info_url: %s", err)
}
if config.AppendDefaultServerInfo {
if !strings.HasSuffix(infoURL.Path, "/") {
infoURL.Path += "/"
}
infoURL.Path += defaultServerInfoPath
}
if config.SkipApps != nil || config.SkipUpdate != nil {
query := infoURL.Query()
if config.SkipApps != nil {
query.Set("skipApps", strconv.FormatBool(*config.SkipApps))
}
if config.SkipUpdate != nil {
query.Set("skipUpdate", strconv.FormatBool(*config.SkipUpdate))
}
infoURL.RawQuery = query.Encode()
}
return &Config{
Timeout: time.Duration(config.TimeoutSeconds * float64(time.Second)),
InfoURL: infoURL,
Listen: config.Listen,
}
}