This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
config.go
81 lines (72 loc) · 2.33 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
// Copyright 2022 <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
var (
ascii = `
___ _ _ _ _ _ ___ ____ _ ____ _ _ ____ ____ ____
| | |\ | \_/ |__] |__| | |__| |\ | | |___ |__/
| | | \| | |__] | | |___ | | | \| |___ |___ | \
`
)
// Config configuration details of balancer
type Config struct {
SSLCertificateKey string `yaml:"ssl_certificate_key"`
Location []*Location `yaml:"location"`
Schema string `yaml:"schema"`
Port int `yaml:"port"`
SSLCertificate string `yaml:"ssl_certificate"`
HealthCheck bool `yaml:"tcp_health_check"`
HealthCheckInterval uint `yaml:"health_check_interval"`
MaxAllowed uint `yaml:"max_allowed"`
}
// Location routing details of balancer
type Location struct {
Pattern string `yaml:"pattern"`
ProxyPass []string `yaml:"proxy_pass"`
BalanceMode string `yaml:"balance_mode"`
}
// ReadConfig read configuration from `fileName` file
func ReadConfig(fileName string) (*Config, error) {
in, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(in, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// Print print config details
func (c *Config) Print() {
fmt.Printf("%s\nSchema: %s\nPort: %d\nHealth Check: %v\nLocation:\n",
ascii, c.Schema, c.Port, c.HealthCheck)
for _, l := range c.Location {
fmt.Printf("\tRoute: %s\n\tProxy Pass: %s\n\tMode: %s\n\n",
l.Pattern, l.ProxyPass, l.BalanceMode)
}
}
// Validation verify the configuration details of the balancer
func (c *Config) Validation() error {
if c.Schema != "http" && c.Schema != "https" {
return fmt.Errorf("the schema \"%s\" not supported", c.Schema)
}
if len(c.Location) == 0 {
return errors.New("the details of location cannot be null")
}
if c.Schema == "https" && (len(c.SSLCertificate) == 0 || len(c.SSLCertificateKey) == 0) {
return errors.New("the https proxy requires ssl_certificate_key and ssl_certificate")
}
if c.HealthCheckInterval < 1 {
return errors.New("health_check_interval must be greater than 0")
}
return nil
}