-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
112 lines (95 loc) · 2.78 KB
/
Copy pathconfig.go
File metadata and controls
112 lines (95 loc) · 2.78 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package xginx
import (
"os"
"github.com/xSaCh/xginx/pkg/schedulers"
"gopkg.in/yaml.v3"
)
type Config struct {
LoadBalancer LoadBalancerConfig `yaml:"xginx"`
}
type LoadBalancerConfig struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Scheduler schedulers.SchedulerAlgorithm `yaml:"scheduler"`
HealthCheck HealthCheckConfig `yaml:"health_check"`
BackendServers []BackendServer `yaml:"backend_servers"`
Security SecurityConfig `yaml:"security"`
Logging LoggingConfig `yaml:"logging"`
StickySessions StickySessionsConfig `yaml:"sticky_sessions"`
}
type HealthCheckConfig struct {
Enabled bool `yaml:"enabled"`
Interval int `yaml:"interval"`
Timeout int `yaml:"timeout"`
Retries int `yaml:"retries"`
}
type BackendServer struct {
Name string `yaml:"name"`
Address string `yaml:"address"`
Port int `yaml:"port"`
Weight int `yaml:"weight"`
}
type SecurityConfig struct {
TLS TLSConfig `yaml:"tls"`
RateLimiting RateLimitingConfig `yaml:"rate_limiting"`
}
type TLSConfig struct {
Enabled bool `yaml:"enabled"`
Certificate string `yaml:"certificate"`
PrivateKey string `yaml:"private_key"`
}
type RateLimitingConfig struct {
Enabled bool `yaml:"enabled"`
RequestsPerSecond int `yaml:"requests_per_second"`
}
type LoggingConfig struct {
Level string `yaml:"level"`
File string `yaml:"file"`
}
type StickySessionsConfig struct {
Enabled bool `yaml:"enabled"`
Type string `yaml:"type"`
}
func LoadConfig(filename string) (*Config, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
setDefaults(&config)
return &config, nil
}
func setDefaults(config *Config) {
if config.LoadBalancer.Name == "" {
config.LoadBalancer.Name = "xginx"
}
if config.LoadBalancer.Host == "" {
config.LoadBalancer.Host = "127.0.0.1"
}
if config.LoadBalancer.Port == 0 {
config.LoadBalancer.Port = 5000
}
if config.LoadBalancer.Scheduler == "" {
config.LoadBalancer.Scheduler = schedulers.SCHEDULER_ROUND_ROBIN
}
if config.LoadBalancer.HealthCheck.Interval == 0 {
config.LoadBalancer.HealthCheck.Interval = 5
}
if config.LoadBalancer.HealthCheck.Timeout == 0 {
config.LoadBalancer.HealthCheck.Timeout = 2
}
if config.LoadBalancer.HealthCheck.Retries == 0 {
config.LoadBalancer.HealthCheck.Retries = 3
}
if config.LoadBalancer.Logging.Level == "" {
config.LoadBalancer.Logging.Level = "debug"
}
if config.LoadBalancer.Logging.File == "" {
config.LoadBalancer.Logging.File = "xginx.log"
}
}