-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
85 lines (66 loc) · 1.94 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
package config
import (
"errors"
"fmt"
"github.com/caarlos0/env/v7"
"github.com/joho/godotenv"
)
// ParameterStoreHandler is used to get parameter from another service and must
// be expose all parameters to the env.
type ParameterStoreHandler func(stage, appName string) error
// Config represents the config of application.
type Config struct {
Debug bool `env:"DEBUG"`
AppDomain string `env:"APP_DOMAIN"`
ServerHost string `env:"SERVER_HOST"`
ServerPort int `env:"SERVER_PORT"`
ServerReadTimeout int `env:"SERVER_READ_TIMEOUT"`
ServerWriteTimeout int `env:"SERVER_WRITE_TIMEOUT"`
ServerAllowOrigins []string `env:"SERVER_ALLOW_ORIGINS"`
ServerAllowHeaders []string `env:"SERVER_ALLOW_HEADERS"`
ServerAllowMethods []string `env:"SERVER_ALLOW_METHODS"`
}
// Validate the config.
func (c *Config) Validate() error {
return nil
}
// LoadOptions is options for function load.
type LoadOptions struct {
Stage string
AppName string
CustomHandler ParameterStoreHandler
}
func (o *LoadOptions) Validate() error {
if o.AppName == "" {
return errors.New("`App Name` must be defined for configuration.")
}
return nil
}
// Load will loads all variables from .env file & .env.{stage} file and the
// custom parameter store's variables to env process. After that, It will parse
// to a Config.
//
// Stage must be expose to env process if application not runs in the local.
func Load(opts *LoadOptions) (*Config, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
stage := opts.Stage
if stage == "" {
stage = "local"
}
supportEnvFiles := []string{".env", fmt.Sprintf(".env.%s", stage)}
if err := godotenv.Load(supportEnvFiles...); err != nil {
return nil, err
}
if opts.CustomHandler != nil {
if err := opts.CustomHandler(stage, opts.AppName); err != nil {
return nil, err
}
}
cfg := &Config{}
if err := env.Parse(cfg); err != nil {
return nil, err
}
return cfg, nil
}