-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
94 lines (77 loc) · 2.18 KB
/
configuration.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package insapp
import (
"encoding/json"
"log"
"os"
"time"
"gopkg.in/mgo.v2"
)
// Config defines how to model a Config
type Config struct {
Domain string `json:"domain"`
Environment string `json:"env"`
GoogleEmail string `json:"google_email"`
GooglePassword string `json:"google_password"`
FirebaseKey string `json:"firebase_key"`
DatabaseName string `json:"mongo_database_name"`
DatabaseSource string `json:"mongo_database_source"`
DatabaseUsername string `json:"mongo_database_username"`
DatabasePassword string `json:"mongo_database_password"`
PrivateKeyPath string `json:"private_key_path"`
PublicKeyPath string `json:"public_key_path"`
Port string `json:"port"`
}
var mgoSession *mgo.Session
var config *Config
// InitConfig loads the configuration from the filesystem.
func InitConfig() *Config {
file, err1 := os.Open("config.json")
decoder := json.NewDecoder(file)
if err1 != nil {
log.Fatal(err1)
}
err2 := decoder.Decode(&config)
if err2 != nil {
log.Fatal("Error when parsing config file. Make sure the configuration file (config.json) is valid.")
}
return config
}
// GetMongoSession creates a new session.
// If there is an active mongo session it will return a Clone.
func GetMongoSession() *mgo.Session {
if mgoSession == nil {
var err error
mgoSession, err = mgo.DialWithInfo(initMongoConfig())
if err != nil {
log.Fatal(err)
log.Fatal("Failed to start the Mongo session")
}
mgoSession.SetMode(mgo.Monotonic, true)
}
return mgoSession.Clone()
}
// GetCDN returns the CDN address depending the configuration.
func (config Config) GetCDN() string {
var cdn string
switch config.Environment {
case "prod":
cdn = "https://" + config.Domain + "/cdn/"
case "dev":
cdn = "https://" + config.Domain + "/cdn/"
case "local":
cdn = "http://" + config.Domain + "/cdn/"
}
return cdn
}
func initMongoConfig() *mgo.DialInfo {
var address []string
address = append(address, "db")
return &mgo.DialInfo{
Addrs: address,
Database: config.DatabaseName,
Source: config.DatabaseSource,
Username: config.DatabaseUsername,
Password: config.DatabasePassword,
Timeout: time.Second * 10,
}
}