-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
45 lines (38 loc) · 1.04 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
package config
import (
"fmt"
"github.com/ilyakaznacheev/cleanenv"
)
type (
Config struct {
Http `yaml:"http"`
Redis `yaml:"redis"`
Postgres `yaml:"postgres"`
}
Http struct {
Port string `env-required:"true" yaml:"port" env:"HTTP_PORT"`
}
Redis struct {
Host string `env-required:"true" yaml:"host" env:"REDIS_HOST"`
Port string `env-required:"true" yaml:"port" env:"REDIS_PORT"`
}
Postgres struct {
Host string `env-required:"true" yaml:"host" env:"POSTGRES_HOST"`
Port string `env-required:"true" yaml:"port" env:"POSTGRES_PORT"`
Db string `env-required:"true" yaml:"db" env:"POSTGRES_DB"`
User string `env-required:"true" yaml:"user" env:"POSTGRES_USER"`
Password string `env-required:"true" yaml:"password" env:"POSTGRES_PASSWORD"`
}
)
func NewConfig() (*Config, error) {
cfg := &Config{}
err := cleanenv.ReadConfig("./config/config.yml", cfg)
if err != nil {
return nil, fmt.Errorf("config error: %w", err)
}
err = cleanenv.ReadEnv(cfg)
if err != nil {
return nil, err
}
return cfg, nil
}