-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
56 lines (45 loc) · 1.32 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
type Config struct {
Token string `json:"bot_token"`
Timeout int `json:"bot_poll_timeout"` // optional
Debug bool `json:"bot_debug"` // optional
Location string `json:"location"`
AllowedUserIds []int `json:"allowed_user_ids"`
MimeWhitelist []string `json:"mime_whitelist"` // optional
SuccessText string `json:"success_text"` // optional
ProxyUrl string `json:"proxy_url"` // optional
}
func ReadConfig(filepath string) Config {
log.Printf(`Reading config from "%s"`, filepath)
fileData, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatalf(`Could not read file "%s": %s`, filepath, err)
}
config := Config{
Timeout: 60,
Debug: false,
AllowedUserIds: []int{},
MimeWhitelist: []string{},
SuccessText: "\u2705 Done.",
}
err = json.Unmarshal(fileData, &config)
if err != nil {
log.Fatalf(`Could not parse config "%s": %s`, fileData, err)
}
if config.Token == "" {
log.Fatal("config.bot_token is required")
}
if config.Location == "" {
log.Fatal("config.location is required")
}
if len(config.AllowedUserIds) == 0 {
log.Fatal("config.allowed_user_ids is missing or empty, all messages will be dropped")
}
log.Printf(`token is "%s"`, config.Token)
return config
}