-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
46 lines (39 loc) · 1.03 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
)
//ConfigFile struct for reading the configuration json file
type ConfigFile struct {
BaseCurrency string `json:"baseCurrency"`
URL string `json:"urlAPI"`
Assets Assets `json:"assets"`
}
//Config struct
var Config ConfigFile
//ReadConfig data
func ReadConfig() (configFile ConfigFile) {
//get binary dir
//os.Args doesn't work the way we want with "go run". You can use next line
//for local dev, but use the original for production.
//dir, err := filepath.Abs("./") //local dev
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //production-binary
if err != nil {
log.Fatal(err)
}
log.Println("Reading config file")
file := dir + "/config.json"
configFileData, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal("Can't read config.json, because: ", err)
}
log.Println("Parsing config file")
err = json.Unmarshal(configFileData, &configFile)
if err != nil {
log.Fatal("Cant parse json, because: ", err)
}
return
}