-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathconfig.go
155 lines (129 loc) · 3.34 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package config
import (
"os"
"strings"
"time"
"github.com/spf13/viper"
)
const (
// StaticDir stores the name of the directory that will serve static files.
StaticDir = "static"
// StaticPrefix stores the URL prefix used when serving static files.
StaticPrefix = "files"
)
type environment string
const (
// EnvLocal represents the local environment.
EnvLocal environment = "local"
// EnvTest represents the test environment.
EnvTest environment = "test"
// EnvDevelop represents the development environment.
EnvDevelop environment = "dev"
// EnvStaging represents the staging environment.
EnvStaging environment = "staging"
// EnvQA represents the qa environment.
EnvQA environment = "qa"
// EnvProduction represents the production environment.
EnvProduction environment = "prod"
)
// SwitchEnvironment sets the environment variable used to dictate which environment the application is
// currently running in.
// This must be called prior to loading the configuration in order for it to take effect.
func SwitchEnvironment(env environment) {
if err := os.Setenv("PAGODA_APP_ENVIRONMENT", string(env)); err != nil {
panic(err)
}
}
type (
// Config stores complete configuration.
Config struct {
HTTP HTTPConfig
App AppConfig
Cache CacheConfig
Database DatabaseConfig
Files FilesConfig
Tasks TasksConfig
Mail MailConfig
}
// HTTPConfig stores HTTP configuration.
HTTPConfig struct {
Hostname string
Port uint16
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ShutdownTimeout time.Duration
TLS struct {
Enabled bool
Certificate string
Key string
}
}
// AppConfig stores application configuration.
AppConfig struct {
Name string
Host string
Environment environment
EncryptionKey string
Timeout time.Duration
PasswordToken struct {
Expiration time.Duration
Length int
}
EmailVerificationTokenExpiration time.Duration
}
// CacheConfig stores the cache configuration.
CacheConfig struct {
Capacity int
Expiration struct {
StaticFile time.Duration
}
}
// DatabaseConfig stores the database configuration.
DatabaseConfig struct {
Driver string
Connection string
TestConnection string
}
// FilesConfig stores the file system configuration.
FilesConfig struct {
Directory string
}
// TasksConfig stores the tasks configuration.
TasksConfig struct {
Goroutines int
ReleaseAfter time.Duration
CleanupInterval time.Duration
ShutdownTimeout time.Duration
}
// MailConfig stores the mail configuration.
MailConfig struct {
Hostname string
Port uint16
User string
Password string
FromAddress string
}
)
// GetConfig loads and returns configuration.
func GetConfig() (Config, error) {
var c Config
// Load the config file.
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("config")
viper.AddConfigPath("../config")
viper.AddConfigPath("../../config")
// Load env variables.
viper.SetEnvPrefix("pagoda")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.ReadInConfig(); err != nil {
return c, err
}
if err := viper.Unmarshal(&c); err != nil {
return c, err
}
return c, nil
}