Newer
Older
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
package configuration
import (
"errors"
"fmt"
configParser "github.com/caarlos0/env/v9"
)
type Config struct {
Environment string `env:"ENV_NAME" envDefault:"development"`
Host string `env:"HOST" envDefault:"127.0.0.1"`
Port int16 `env:"PORT" envDefault:"3000"`
}
func New() ( *Config, error ){
cfg := Config{}
if err := configParser.Parse( &cfg ); err != nil {
return nil, err
}
possibleEnvValues := map[ string ] bool {
"development": true,
"testing": true,
"production": true,
}
if _, ok := possibleEnvValues[ cfg.Environment ]; !ok {
return nil, errors.New(
fmt.Sprintf( "Invalid environment value: %s", cfg.Environment ),
)
}
return &cfg, nil
}