/
config.go
executable file
·73 lines (66 loc) · 1.53 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
package config
import (
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"net/url"
"os"
)
var Config *YamlConfig
var Version string
var BestdoriAPIUrl *url.URL
type YamlConfig struct {
RunAddr string `yaml:"run-addr"`
Debug bool `yaml:"debug"`
API YamlConfigAPI `yaml:"api"`
Database YamlConfigDatabase `yaml:"database"`
}
type YamlConfigAPI struct {
BestdoriAPI string `yaml:"bestdori-api"`
}
type YamlConfigDatabase struct {
Mysql string `yaml:"mysql"`
MeiliSearch string `yaml:"meilisearch"`
MeiliSearchKey string `yaml:"meilisearch-key"`
}
func NewYamlConfig() *YamlConfig {
return &YamlConfig{
RunAddr: "0.0.0.0:8080",
Debug: true,
API: YamlConfigAPI{
BestdoriAPI: "http://127.0.0.1:21104",
},
Database: YamlConfigDatabase{
Mysql: "user:password@/dbname",
MeiliSearch: "http://127.0.0.1:7700",
MeiliSearchKey: "",
},
}
}
func init() {
Version = "2.1.0"
Config = NewYamlConfig()
var err error
if envEnabled() {
initEnv()
} else {
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
yamlConfig, _ := yaml.Marshal(Config)
err = ioutil.WriteFile("conf.yaml", yamlConfig, 0666)
if err != nil {
log.Fatal(err.Error())
}
log.Printf("conf.yaml not found, one is generate!")
os.Exit(0)
}
err = yaml.Unmarshal(yamlFile, Config)
if err != nil {
log.Fatal("Check the conf.yaml, Cannot Read!")
}
}
BestdoriAPIUrl, err = url.Parse(Config.API.BestdoriAPI)
if err != nil {
log.Fatal("Cannot parse BestdoriAPI!")
}
}