-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
114 lines (97 loc) · 2.98 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
package main
import (
"strings"
"time"
"github.com/cafebazaar/go-boilerplate/pkg/errors"
"github.com/cafebazaar/go-boilerplate/pkg/sql"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
// Config the application's configuration structure
type Config struct {
Logging LoggingConfig
ConfigFile string
ListenPort int
MetricListenPort int
Database sql.PostgresConfig
Cache CacheConfig
}
type CacheConfig struct {
Redis RedisConfig
BigCache BigCacheConfig
}
type RedisConfig struct {
Enabled bool
Host string
Port int
DB int
Prefix string
ExpirationTime time.Duration
}
type BigCacheConfig struct {
Enabled bool
ExpirationTime time.Duration
MaxSpace int
Shards int
LifeWindow time.Duration
MaxEntriesInWindow int
MaxEntrySize int
Verbose bool
HardMaxCacheSize int
}
// LoadConfig loads the config from a file if specified, otherwise from the environment
func LoadConfig(cmd *cobra.Command) (*Config, error) {
// Setting defaults for this application
viper.SetDefault("logging.SentryEnabled", false)
viper.SetDefault("logging.level", "error")
viper.SetDefault("listenPort", 8080)
viper.SetDefault("metricListenPort", 8081)
viper.SetDefault("database.host", "127.0.0.1")
viper.SetDefault("database.port", 5432)
viper.SetDefault("database.username", "postgres")
viper.SetDefault("database.password", "")
viper.SetDefault("database.database", "postview")
viper.SetDefault("database.ssl", false)
viper.SetDefault("database.maxIdleConnection", 0)
viper.SetDefault("database.maxOpenConnection", 0)
viper.SetDefault("cache.redis.host", "127.0.0.1")
viper.SetDefault("cache.redis.port", 6379)
viper.SetDefault("cache.redis.db", 0)
viper.SetDefault("cache.redis.expirationTime", 3*time.Hour)
viper.SetDefault("cache.redis.prefix", "POST_VIEW")
viper.SetDefault("cache.bigCache.shards", 1024)
viper.SetDefault("cache.bigCache.maxEntriesInWindow", 1100*10*60)
viper.SetDefault("cache.bigCache.maxEntrySize", 500)
viper.SetDefault("cache.bigCache.verbose", true)
viper.SetDefault("cache.bigCache.hardMaxCacheSize", 125)
// Read Config from ENV
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("POSTVIEW")
viper.AutomaticEnv()
// Read Config from Flags
err := viper.BindPFlags(cmd.Flags())
if err != nil {
return nil, err
}
// Read Config from file
if configFile, err := cmd.Flags().GetString("config-file"); err == nil && configFile != "" {
viper.SetConfigFile(configFile)
if err := viper.ReadInConfig(); err != nil {
return nil, err
}
}
var config Config
err = viper.Unmarshal(&config)
if err != nil {
return nil, err
}
return &config, nil
}
func provideConfig(cmd *cobra.Command) (*Config, error) {
config, err := LoadConfig(cmd)
if err != nil {
return nil, errors.Wrap(err, "Failed to load configurations.")
}
return config, nil
}