-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
88 lines (76 loc) · 2.58 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
package config
import (
"errors"
"fmt"
"github.com/spf13/viper"
)
const DefaultRequestTimeoutSeconds = 60
type Configuration struct {
Server ServerConfigurations
Database DatabaseConfigurations
JwtSigningKey string `yaml:"jwtSigningKey"`
Production bool `yaml:"production"`
}
type ServerConfigurations struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
RequestTimeoutSeconds int `yaml:"requestTimeoutSeconds"`
DisableCORS bool `yaml:"disableCORS"`
}
type DatabaseConfigurations struct {
DBName string `yaml:"dbname"`
DBUser string `yaml:"dbuser"`
DBPassword string `yaml:"dbpassword"`
DBConnection string `yaml:"dbconnection"`
DBSslMode string `yaml:"dbsslmode"`
DataSeeding bool `yaml:"dbseeding"`
}
func LoadConfigurations() (*Configuration, error) {
// Set the file name of the configurations file
viper.SetConfigName("config")
// Set the path to look for the configurations file
viper.AddConfigPath("./config")
viper.SetConfigType("yml")
// Enable VIPER to read Environment Variables
viper.AutomaticEnv()
viper.SetEnvPrefix("TRACKER")
_ = viper.BindEnv("production", "TRACKER_PRODUCTION")
_ = viper.BindEnv("server.port", "TRACKER_SERVER_PORT")
_ = viper.BindEnv("server.host", "TRACKER_SERVER_HOST")
_ = viper.BindEnv("server.requestTimeoutSeconds", "TRACKER_SERVER_TIMEOUT")
_ = viper.BindEnv("server.disableCORS", "TRACKER_SERVER_DISABLE_CORS")
_ = viper.BindEnv("database.dbname", "TRACKER_DATABASE_NAME")
_ = viper.BindEnv("database.dbuser", "TRACKER_DATABASE_USER")
_ = viper.BindEnv("database.dbpassword", "TRACKER_DATABASE_PASSWORD")
_ = viper.BindEnv("database.dbconnection", "TRACKER_DATABASE_CONNECTION")
_ = viper.BindEnv("database.dbsslmode", "TRACKER_DATABASE_SSL")
_ = viper.BindEnv("database.dbseeding", "TRACKER_DATABASE_SEEDING")
configuration := &Configuration{
Server: ServerConfigurations{
RequestTimeoutSeconds: DefaultRequestTimeoutSeconds,
},
Database: DatabaseConfigurations{
DataSeeding: false,
DBSslMode: "disable",
},
}
var err error
if err = viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("error reading config file, %s", err)
}
err = viper.Unmarshal(configuration)
if err != nil {
return nil, fmt.Errorf("unable to decode into struct, %v", err)
}
validationError := configuration.validate()
if validationError != nil {
return nil, validationError
}
return configuration, nil
}
func (c *Configuration) validate() error {
if c.JwtSigningKey == "" {
return errors.New("JwtSigningKey null or empty")
}
return nil
}