/
internal.go
66 lines (56 loc) · 1.57 KB
/
internal.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
package storage
import (
"fmt"
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v3"
)
var IChatState = "IChatState"
type InternalConfig struct {
TG struct {
BotToken string `yaml:"bot_token"`
BossChatID int64 `yaml:"boss_chat_id"`
} `yaml:"tg"`
JWTExpHours int `yaml:"jwt_exp_hours"`
Log struct {
ClearOnRestart bool `yaml:"clear_on_restart"`
ServiceHostname string `yaml:"service_hostname"`
RetentionHours int `yaml:"retention_hours"`
SimilarityThreshold float64 `yaml:"similarity_threshold"`
} `yaml:"log"`
}
func ReadInternalConfig(path string) (InternalConfig, error) {
var config_path string
if path == "" {
configEnvironment, ok := os.LookupEnv("CONFIG_ENV")
if !ok {
log.Panic("Config environment variable CONFIG_ENV must be specified.")
}
if configEnvironment == "dev" {
config_path = "environment/local.yml"
} else if configEnvironment == "devdocker" {
config_path = "/app/config/local.yml"
} else if configEnvironment == "prod" {
config_path = "/app/config/internal.yml"
} else {
log.Panic("CONFIG_ENV must be either dev or prod.")
}
} else {
config_path = path
}
var config InternalConfig
if _, err := os.Stat(config_path); os.IsNotExist(err) {
return InternalConfig{}, fmt.Errorf("Config must exist.")
}
content, err := ioutil.ReadFile(config_path)
if err != nil {
log.Panic("Failed to read config file.")
}
err = yaml.Unmarshal(content, &config)
if err != nil {
return config, fmt.Errorf("Failed to parse config file. %v", err)
}
log.Println("read internal config")
return config, nil
}