-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
59 lines (51 loc) · 1.75 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
package gondola
import (
"io"
"os"
"gopkg.in/yaml.v3"
)
// Proxy is a struct that represents the proxy server.
// Port is the port that the proxy server will listen on.
// ShutdownTimeout is the timeout in milliseconds for the proxy server to shutdown.
type Proxy struct {
Port string `yaml:"port"`
ReadHeaderTimeout int `yaml:"read_header_timeout"`
ShutdownTimeout int `yaml:"shutdown_timeout"`
TLSCertPath string `yaml:"tls_cert_path"`
TLSKeyPath string `yaml:"tls_key_path"`
StaticFiles []StaticFile `yaml:"static_files"`
}
// StaticFile is a struct that represents a static file configuration.
type StaticFile struct {
Path string `yaml:"path"`
Dir string `yaml:"dir"`
}
// IsEnableTLS returns true if the proxy server is configured to use TLS.
func (p *Proxy) IsEnableTLS() bool {
return p.TLSCertPath != "" && p.TLSKeyPath != ""
}
// Upstream is a struct that represents a backend server.
// HostName is the hostname that the proxy will listen for.
// Target is the target URL that the proxy will forward requests to.
type Upstream struct {
HostName string `yaml:"host_name"`
Target string `yaml:"target"`
}
// Config is a struct that represents the configuration of the proxy.
type Config struct {
Proxy Proxy `yaml:"proxy"`
Upstreams []Upstream `yaml:"upstreams"`
LogLevel int `yaml:"log_level"` // Debug:-4 Info:0 Warn:4 Error:8
}
// Load reads the configuration from a reader and returns a Config struct.
func (c *Config) Load(reader io.Reader) (*Config, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
data = []byte(os.ExpandEnv(string(data)))
if err := yaml.Unmarshal(data, &c); err != nil {
return nil, err
}
return c, nil
}