-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (63 loc) · 1.9 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
package exo
import (
"errors"
"github.com/hashicorp/hcl2/gohcl"
"github.com/hashicorp/hcl2/hclparse"
)
type Config struct {
Servers []*ServerConfig `hcl:"server,block"`
}
type ServerConfig struct {
Name string `hcl:"name,label"`
ListenAddr string `hcl:"listen_addr"`
Routes Routes `hcl:"routes,block"`
// TLSOn bool `hcl:"tls_on,optional"`
TLSCertificateFile *string `hcl:"tls_cert,optional"`
TLSPrivateLKeyFile *string `hcl:"tls_key,optional"`
TLSCertificate *string `hcl:"tls_cert_bytes,optional"`
TLSPrivateLKey *string `hcl:"tls_key_bytes,optional"`
}
type Routes struct {
Routes []RouteConfig `hcl:"path,block"`
}
type RouteConfig struct {
Match string `hcl:"match,label"`
FileServer *FileServerConfig `hcl:"file_server,block"`
ReverseProxy *ReverseProxyConfig `hcl:"reverse_proxy,block"`
Headers *Headers `hcl:"headers,optional"`
}
type FileServerConfig struct {
Root string `hcl:"root"`
Bundle bool `hcl:"bundle,optional"`
Headers *Headers `hcl:"headers,optional"`
}
type ReverseProxyConfig struct {
Upstreams []string `hcl:"upstreams"`
LoadBalancing string `hcl:"load_balancing,optional"`
Headers *Headers `hcl:"headers,optional"`
}
type Headers map[string]string
func LoadConfig(config string, cfg *Config) error {
parser := hclparse.NewParser()
file, diags := parser.ParseHCLFile(config)
if diags.HasErrors() {
return errors.New(diags.Error())
}
diags = gohcl.DecodeBody(file.Body, nil, cfg)
if diags.HasErrors() {
return errors.New(diags.Error())
}
return nil
}
func LoadConfigFromBytes(config []byte, cfg *Config) error {
parser := hclparse.NewParser()
file, diags := parser.ParseHCL(config, "byte.hcl")
if diags.HasErrors() {
return errors.New(diags.Error())
}
diags = gohcl.DecodeBody(file.Body, nil, cfg)
if diags.HasErrors() {
return errors.New(diags.Error())
}
return nil
}