-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
87 lines (70 loc) · 2.17 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
package config
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/caarlos0/env/v8"
)
type Server struct {
AppAddress string `env:"APP_PORT" envDefault:"7000"`
AppReadTimeout time.Duration `env:"APP_READ_TIMEOUT" envDefault:"60s"`
AppWriteTimeout time.Duration `env:"APP_WRITE_TIMEOUT" envDefault:"60s"`
AppIdleTimeout time.Duration `env:"APP_IDLE_TIMEOUT" envDefault:"60s"`
}
type Postgres struct {
Host string `env:"DB_HOST,notEmpty"`
Port string `env:"DB_PORT" envDefault:"5432"`
Database string `env:"DB_NAME,notEmpty"`
User string `env:"DB_USER,notEmpty"`
Password string `env:"DB_PASSWORD,notEmpty"`
SSLmode string `env:"DB_SSL_MODE" envDefault:"disable"`
}
type SMTP struct {
MailAccount string `env:"SMTP_ACCOUNT"`
AccountPassword string `env:"SMTP_PASSWORD"`
SMTPServerAddress string `env:"SMTP_ADDRESS"`
SMTPPort string `env:"SMTP_PORT"`
CaptchaKey string `env:"CAPTCHA_KEY"`
}
type Config struct {
Server Server
DB Postgres
Auth AuthConfig
SMTP SMTP
LogLevel string `env:"LOG_LEVEL" envDefault:"INFO"`
}
type AuthConfig struct {
Salt string `env:"APP_SALT,notEmpty"`
SigningKey string `env:"SIGNING_KEY,notEmpty"`
AccessTokenTTL time.Duration `env:"ACCESS_TOKEN_TTL" envDefault:"15m"`
RefreshTokenTTL time.Duration `env:"REFRESH_TOKEN_TTL" envDefault:"24h"`
}
// Allowed logger levels & config key.
const (
DebugLogLvl = "DEBUG"
InfoLogLvl = "INFO"
ErrorLogLvl = "ERROR"
)
var errNotAllowedLoggelLevel = errors.New("not allowed logger level")
func InitConfig() (Config, error) {
fmt.Println(os.Getenv("APP_PORT"))
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
return Config{}, fmt.Errorf("error while parsing .env: %w", err)
}
if err := validate(cfg.LogLevel); err != nil {
return Config{}, fmt.Errorf("wrong loglevel value: %w", err)
}
cfg.Server.AppAddress = ":" + cfg.Server.AppAddress
return cfg, nil
}
func validate(logLevel string) error {
if strings.ToUpper(logLevel) != DebugLogLvl &&
strings.ToUpper(logLevel) != ErrorLogLvl &&
strings.ToUpper(logLevel) != InfoLogLvl {
return errNotAllowedLoggelLevel
}
return nil
}