-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
65 lines (53 loc) · 1.24 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
63
64
65
package config
import (
"github.com/spf13/viper"
"log"
"net/url"
)
const (
ISOShortDateFormat = "2006-01-02" // ISO 8601: YYYY-MM-DD
)
func InitConfig() {
viper.AddConfigPath("./config")
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName("default")
err := viper.ReadInConfig()
if err != nil {
log.Panicf("Could not load default config file: %s\n", err)
}
err = viper.BindEnv("GO_ENV")
if err != nil {
log.Panicf("Could not bind GO_ENV variable: %s\n", err)
}
env := viper.GetString("GO_ENV")
if env == "" {
env = "dev"
viper.Set("GO_ENV", env)
}
viper.SetConfigName(env)
err = viper.MergeInConfig()
if err != nil {
log.Panicf("Error in environment [%s] config file: %s\n", env, err)
}
}
// Create a URL to the client/application site
func CreateUrl(path string, queryString string) string {
hostname := viper.GetString("application.applicationDomain")
port := viper.GetString("application.applicationDomainPort")
domainSecure := viper.GetBool("application.domainSecure")
scheme := "https"
if !domainSecure {
scheme = "http"
}
if port != "" {
hostname += ":" + port
}
url := url.URL{
Scheme: scheme,
Host: hostname,
Path: path,
RawQuery: queryString,
}
return url.String()
}