-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (51 loc) · 1.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
package config
import "github.com/spf13/viper"
// CasbinConfig holds casbin-related config
type CasbinConfig struct {
Model string `mapstructure:"model"`
Policy string `mapstructure:"policy"`
}
// SecurityConfig holds security-related config
type SecurityConfig struct {
PasswordSalt string `mapstructure:"password_salt"`
StoreSecret string `mapstructure:"store_secret"`
SessionKey string `mapstructure:"session_key"`
CSRFSecret string `mapstructure:"csrf_secret"`
Casbin CasbinConfig `mapstructure:"casbin"`
}
// DBConfig holds db-related config
type DBConfig struct {
Name string
URI string
}
// CORSConfig holds cors-related config
type CORSConfig struct {
AllowedOrigins []string `mapstructure:"allowed_origins"`
}
// HTTPConfig holds http-related config
type HTTPConfig struct {
Host string
Port string
CORS CORSConfig `mapstructure:"cors"`
}
// Config holds app-wide config
type Config struct {
Prod bool
HTTP HTTPConfig `mapstructure:"http"`
DB DBConfig `mapstructure:"db"`
Security SecurityConfig `mapstructure:"security"`
}
// Load loads the config
func Load() (*Config, error) {
v := viper.New()
v.SetConfigName("config")
v.AddConfigPath(".")
if err := v.ReadInConfig(); err != nil {
return nil, err
}
var c Config
if err := v.Unmarshal(&c); err != nil {
return nil, err
}
return &c, nil
}