-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
84 lines (71 loc) · 3.25 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
package rmhttp
import (
"fmt"
"dario.cat/mergo"
env "github.com/caarlos0/env/v11"
)
// ------------------------------------------------------------------------------------------------
// TIMEOUT CONFIG
// ------------------------------------------------------------------------------------------------
// The TimeoutConfig contains settings (with defaults) for configuring timeouts in the system.
// These settings mostly correlate to those used by the underlying http.Server
type TimeoutConfig struct {
TCPReadTimeout int `env:"TCP_READ_TIMEOUT" envDefault:"2"`
TCPReadHeaderTimeout int `env:"TCP_READ_HEADER_TIMEOUT" envDefault:"1"`
TCPIdleTimeout int `env:"TCP_IDLE_TIMEOUT" envDefault:"120"`
TCPWriteTimeout int `env:"TCP_WRITE_TIMEOUT" envDefault:"5"`
TCPWriteTimeoutPadding int `env:"TCP_WRITE_TIMEOUT_BUFFER" envDefault:"1"`
RequestTimeout int `env:"HTTP_REQUEST_TIMEOUT" envDefault:"7"`
TimeoutMessage string `env:"HTTP_TIMEOUT_MESSAGE" envDefault:"Request Timeout"`
}
// ------------------------------------------------------------------------------------------------
// SSL CONFIG
// ------------------------------------------------------------------------------------------------
// The SSLConfig contains settings (with defaults) for configuring SSL in the server.
type SSLConfig struct {
Enable bool `env:"ENABLE_SSL"`
Cert string `env:"SSL_CERT"`
Key string `env:"SSL_KEY"`
}
// ------------------------------------------------------------------------------------------------
// CONFIG
// ------------------------------------------------------------------------------------------------
// The Config contains settings (with defaults) for configuring the app, server and router.
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT" envDefault:"8080"`
Debug bool `env:"DEBUG"`
DisableGeneralOptionsHandler bool
Logger Logger
SSL SSLConfig
Timeout TimeoutConfig
}
// loadConfig parses the environment variables defined in the Config objects (with defaults), then merges those
// with the config that the user may have supplied. This function only gets called during app initialisation.
//
// This function will return a completed config, or error if the environment variables cannot be parsed.
func LoadConfig(cfg Config) (Config, error) {
config := Config{}
if err := env.Parse(&config); err != nil {
return config, fmt.Errorf("failed to load environment variables: %v", err)
}
// Merge the main config
err := mergo.Merge(&config, cfg, mergo.WithOverride)
if err != nil {
return config, fmt.Errorf("failed to merge user supplied and default configs: %v", err)
}
// Merge the SSL config
err = mergo.Merge(&config.SSL, cfg.SSL, mergo.WithOverride)
if err != nil {
return config, fmt.Errorf("failed to merge user supplied and default SSL configs: %v", err)
}
// Merge the Timeout config
err = mergo.Merge(&config.Timeout, cfg.Timeout, mergo.WithOverride)
if err != nil {
return config, fmt.Errorf(
"failed to merge user supplied and default Timeout configs: %v",
err,
)
}
return config, nil
}