1
package config
2

3
import (
4
    "os"
5
    "strings"
6
    "encoding/json"
7
    "io/ioutil"
8
    "log"
9

10
    "github.com/hashicorp/vault/api"
11
)
12

13
const (
14
    localConfigurationFile = ".config.local.json"
15
    configurationStoreFile = ".config.yml"
16
)
17

18
// Config struct with configuration data for the application
19
type Config struct {
20
    ApplicationKey string `json:"application_key"`
21
    DatastoreHost string `json:"datastore_host"`
22
    DatastorePort string `json:"datastore_port"`
23
    DatastoreNamePrefix string `json:"datastore_name_prefix"`
24
    DatastorePassword string `json:"datastore_password"`
25
    DatastoreSslMode string `json:"datastore_ssl_mode"`
26
    DatastoreUser string `json:"datastore_user"`
27
    MailFrom string `json:"mail_from"`
28
    MailerAccess string `json:"mailer_access"`
29
    MemorystoreHost string `json:"memory_store_host"`
30
    MemorystoreIndex int `json:"memory_store_index"`
31
    MemorystorePassword string `json:"memory_store_password"`
32
    MemorystorePort int `json:"memory_store_port"`
33
    SessionSecret string `json:"session_secret"`
34
    SessionSecure bool `json:"session_secure"`
35
    StorageSecret string `json:"storage_secret"`
36
}
37

38
var globalConfig Config
39
var environment string
40

41 1
func init() {
42 1
    environment = strings.ToLower(os.Getenv("SPACE_ENV"))
43 1
    if environment == "" {
44 0
        environment = "development"
45
    }
46
}
47

48
// Environment returns the current environment for the application;
49
//      it could be: development, testing, production
50 1
func Environment() string {
51 1
    return environment
52
}
53

54
// IsEnvironment checks if the current environment for the application
55
//      is the same as defined in `env`
56 1
func IsEnvironment(env string) bool {
57 1
    return strings.ToLower(env) == Environment()
58
}
59

60
// GetEnvVar gets a `key` from the environment variables
61 1
func GetEnvVar(key string) string {
62 1
    return os.Getenv(key)
63
}
64

65
// GetGlobalConfig returns the global configuration struct for the application
66 0
func GetGlobalConfig() Config {
67 0
    return globalConfig
68
}
69

70
// SetConfig sets the global configuration struct for the application
71 0
func SetConfig(config Config) {
72 0
    globalConfig = config
73
}
74

75
// LoadConfig loads the globalConfig structure from a JSON-based stream:
76
//   1. it attempts to load it from the .config.local.json file;
77
//   2. it checks for the .config.yml file and loads it from Vault; or
78
//   3. it fails without any configuration option available
79 0
func LoadConfig() {
80 0
    var globalService Service
81 0
    var dataStream []byte
82 0
    var err error
83 0
    var client *api.Client
84 0
    var secret *api.Secret
85

86 0
    if _, jErr := os.Stat(localConfigurationFile); jErr == nil {
87
        // .config.local.json exists
88 0
        dataStream, err = ioutil.ReadFile(localConfigurationFile)
89 0
        if err != nil {
90 0
            panic(err)
91
        }
92 0
    } else if _, yErr := os.Stat(configurationStoreFile); yErr == nil && os.IsNotExist(jErr) {
93
        // .config.yml exists
94 0
        globalService.LoadService(configurationStoreFile)
95 0
        client, err = api.NewClient(&api.Config{
96 0
            Address: globalService.Space.ConfigurationStore.Addr,
97 0
        })
98 0
        if err != nil {
99 0
            panic(err)
100
        }
101 0
        client.SetToken(globalService.Space.ConfigurationStore.Token)
102 0
        secret, err = client.Logical().Read(globalService.Space.ConfigurationStore.Path)
103 0
        if err != nil {
104 0
            panic(err)
105
        }
106

107 0
        dataStream, _ = json.Marshal(secret.Data)
108 0
    } else {
109
        // no configuration option available
110 0
        log.Fatal("> No configuration option is available; fatal")
111
    }
112

113 0
    err = json.Unmarshal([]byte(dataStream), &globalConfig)
114 0
    if err != nil {
115 0
        panic(err)
116
    }
117
}

Read our documentation on viewing source code .

Loading