-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (37 loc) · 791 Bytes
/
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
package config
import (
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
const (
appName = "golang_template"
)
type (
Config struct {
timezone string `envconfig:"app_timezone" default:"UTC"` // String timezone format
Timezone *time.Location
}
)
func NewConfig() (*Config, error) {
var cfg Config
godotenv.Load()
// Parse variables from environment or return err
err := envconfig.Process(appName, &cfg)
if err != nil {
return nil, err
}
// Parse timezone from cfg.tz or return err
cfg.Timezone, err = time.LoadLocation(cfg.timezone)
if err != nil {
return nil, err
}
if err := cfg.validate(); err != nil {
return nil, err
}
return &cfg, nil
}
func (cfg *Config) validate() error {
// pass some validations here
return nil
}