-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
42 lines (35 loc) · 1006 Bytes
/
configs.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
package configs
import (
"github.com/go-chi/jwtauth"
"github.com/spf13/viper"
)
var cfg *conf
type conf struct {
DBDriver string `mapstructure:"DB_DRIVER"`
DBHost string `mapstructure:"DB_HOST"`
DBPort string `mapstructure:"DB_PORT"`
DBUser string `mapstructure:"DB_USER"`
DBPassword string `mapstructure:"DB_PASSWORD"`
DBName string `mapstructure:"DB_NAME"`
WebServerPort string `mapstructure:"WEB_SERVER_PORT"`
JWTSecretKey string `mapstructure:"JWT_SECRET_KEY"`
JWTExpiresIn int `mapstructure:"JWT_EXPIRES_IN"`
TokenAuth *jwtauth.JWTAuth
}
func InitConfig(path string) (*conf, error) {
viper.SetConfigName("app_config")
viper.SetConfigType("env")
viper.AddConfigPath(path)
viper.SetConfigFile(".env")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
err = viper.Unmarshal(&cfg)
if err != nil {
panic(err)
}
cfg.TokenAuth = jwtauth.New("HS256", []byte(cfg.JWTSecretKey), nil)
return cfg, nil
}