e5d4f482aa29cca7888f78203528651093d9742e
[icn.git] / cmd / bpa-restapi-agent / internal / config / config.go
1 package config
2
3 import (
4   "encoding/json"
5   "log"
6   "os"
7 )
8
9 type Configuration struct {
10   Password        string  `json:  "password"`
11   DatabaseAddress string  `json:  "database-address"`
12   DatabaseType    string  `json:  "database-type"`
13   ServicePort     string  `json:  "service-port"`
14 }
15
16 var gConfig *Configuration
17
18 func readConfigFile(file string) (*Configuration, error) {
19   f, err := os.Open(file)
20   if err != nil {
21     return defaultConfiguration(), err
22   }
23   defer f.Close()
24
25   conf := defaultConfiguration()
26
27   decoder := json.NewDecoder(f)
28   err = decoder.Decode(conf)
29   if err != nil {
30     return conf, err
31   }
32
33   return conf, nil
34 }
35
36 func defaultConfiguration() *Configuration {
37   return &Configuration {
38     Password:           "",
39     DatabaseAddress:    "127.0.0.1",
40     DatabaseType:       "mongo",
41     ServicePort:        "9015",
42   }
43 }
44
45 func GetConfiguration() *Configuration {
46   if gConfig == nil {
47     conf, err := readConfigFile("ICNconfig.json")
48     if err != nil {
49       log.Println("Error loading config file. Using defaults")
50     }
51
52     gConfig = conf
53   }
54
55   return gConfig
56 }