This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
145 lines (122 loc) · 3.26 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"time"
)
// Config structure used to configure Helios
type Config struct {
Server Server `yaml:"server"`
Upstreams []Upstream `yaml:"upstreams"`
Routes []Route `yaml:"routes"`
Identity Identity `yaml:"identity"`
JWT JWT `yaml:"jwt"`
}
// Server structure is used to configure the HTTP(S) server
type Server struct {
ListenIP string `yaml:"listen_ip"`
ListenPort int `yaml:"listen_port"`
Timeout time.Duration `yaml:"timeout"`
IdleTimeout time.Duration `yaml:"idle_timeout"`
TLSContext TLSContext `yaml:"tls_context"`
}
// TLSContext structure is used to configure TLS for the server
type TLSContext struct {
CertificatePath string `yaml:"certificate_path"`
PrivateKeyPath string `yaml:"private_key_path"`
}
// Route represents a route configuration
type Route struct {
Host string
Rules []string
HTTP struct {
Paths []struct {
Path string
Upstream string
Authentication bool `yaml:"authentication"`
}
}
}
// Upstream represents a single proxy upstream
type Upstream struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
ConnectTimeout time.Duration
}
// Identity provider configuration
type Identity struct {
Provider string `yaml:"provider"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
OAuth2 struct {
AuthURL string `yaml:"auth_url"`
TokenURL string `yaml:"token_url"`
ProfileURL string `yaml:"profile_url"`
}
}
// JWT token configuration
type JWT struct {
Secret string
Expires time.Duration
}
// UnmarshalYAML parses upstream configuration from a YAML file
func (c *Upstream) UnmarshalYAML(unmarshal func(v interface{}) error) error {
buf := struct {
ConnectTimeout string `yaml:"connect_timeout"`
Name string `yaml:"name"`
URL string `yaml:"url"`
}{}
if err := unmarshal(&buf); err != nil {
return err
}
timeout, err := time.ParseDuration(buf.ConnectTimeout)
if err != nil {
return err
}
c.ConnectTimeout = timeout
c.URL = buf.URL
c.Name = buf.Name
return nil
}
// UnmarshalYAML parses JWT configuration from a YAML file
func (c *JWT) UnmarshalYAML(unmarshal func(v interface{}) error) error {
buf := struct {
Secret string `yaml:"secret"`
Expires string `yaml:"expires"`
}{}
if err := unmarshal(&buf); err != nil {
return err
}
expires, err := time.ParseDuration(buf.Expires)
if err != nil {
return err
}
c.Expires = expires
c.Secret = buf.Secret
return nil
}
// UnmarshalYAML parses server configuration from a YAML file
func (c *Server) UnmarshalYAML(unmarshal func(v interface{}) error) error {
var buf struct {
ListenIP string `yaml:"listen_ip"`
ListenPort int `yaml:"listen_port"`
Timeout string `yaml:"timeout"`
IdleTimeout string `yaml:"idle_timeout"`
TLSContext TLSContext `yaml:"tls_context"`
}
if err := unmarshal(&buf); err != nil {
return err
}
timeout, err := time.ParseDuration(buf.Timeout)
if err != nil {
return err
}
idleTimeout, err := time.ParseDuration(buf.IdleTimeout)
if err != nil {
return err
}
c.Timeout = timeout
c.IdleTimeout = idleTimeout
c.TLSContext = buf.TLSContext
c.ListenIP = buf.ListenIP
c.ListenPort = buf.ListenPort
return nil
}