-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
60 lines (54 loc) · 1.35 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
package goapi
import (
"encoding/json"
"log"
"os"
"strings"
)
// Config ...
type Config struct {
Service struct {
Host string `json:"host"`
Port string `json:"port"`
Path string `json:"path"`
Method string `json:"method"`
} `json:"service"`
Cors struct {
AllowOrigins string `json:"allow_origins"`
AllowHeaders string `json:"allow_headers"`
} `json:"cors"`
Sort struct {
DefaultSortOrder string `json:"default_sort_order"`
DefaultSortBy string `json:"default_sort_by"`
ValidSortBy []string `json:"valid_sort_by"`
} `json:"sorting"`
Pagination struct {
DefaultPerPage string `json:"default_per_page"`
MaxPerPage string `json:"max_per_page"`
} `json:"pagination"`
Spanner struct {
Project string `json:"project"`
Instance string `json:"instance"`
Database string `json:"database"`
} `json:"spanner"`
}
// ReadConfig ...
func ReadConfig() (config *Config) {
env := os.Getenv("APP_ENV")
if len(os.Args) > 1 {
env = os.Args[1]
}
if env == "" {
log.Fatal("Missing Envionment!")
}
filePath := "./config/" + env + ".json"
configFile, err := os.Open(filePath)
if err != nil {
panic(err.Error())
}
defer configFile.Close()
json.NewDecoder(configFile).Decode(&config)
config.Service.Path = strings.ToLower(config.Service.Path)
config.Service.Method = strings.ToUpper(config.Service.Method)
return
}