-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
62 lines (49 loc) · 1.6 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
package main
// Credits for this file: https://www.thepolyglotdeveloper.com/2017/04/load-json-configuration-file-golang-application/
import (
"encoding/json"
"log"
"os"
)
// Configuration describes the fields of JSON configuration
type Configuration struct {
DBTYPE string `json:"DB_TYPE"`
DatabaseURL string `json:"DATABASE_URL"`
LinksColl string `json:"LINKS_COLL,omitempty"`
CounterColl string `json:"COUNTER_COLL,omitempty"`
Port string `json:"PORT"`
RedirectMethod string `json:"REDIRECT_METHOD"`
AuthToken string `json:"AUTH_TOKEN"`
}
// ReadConfig reads config from config file
func ReadConfig() *Configuration {
var (
config Configuration
configFilename string
)
configFilename = os.Getenv("CONFIG_FILE")
//configFilename = ".env.postgresql.json"
//configFilename = ".env.mongodb.json"
if len(configFilename) == 0 {
log.Println("Config file not provided. Reading environment variables.")
config.DBTYPE = os.Getenv("DB_TYPE")
config.DatabaseURL = os.Getenv("DATABASE_URL")
config.LinksColl = os.Getenv("LINKS_COLL")
config.CounterColl = os.Getenv("COUNTER_COLL")
config.Port = os.Getenv("PORT")
config.RedirectMethod = os.Getenv("REDIRECT_METHOD")
config.AuthToken = os.Getenv("AUTH_TOKEN")
if len(config.DatabaseURL) == 0 {
log.Fatalf("Missing config , Config : %+v", config)
}
return &config
}
configFile, err := os.Open(configFilename)
defer configFile.Close()
if configFile == nil || err != nil {
log.Fatal("Failed to load config file", err)
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return &config
}