-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathservers_config_hydrator.go
60 lines (50 loc) · 1.67 KB
/
servers_config_hydrator.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
package config
import "net/url"
// HydrateServers populates the servers fields of the RelayMinerConfig.
func (relayMinerConfig *RelayMinerConfig) HydrateServers(
yamlSupplierConfigs []YAMLRelayMinerSupplierConfig,
) error {
// At least one server is required
if len(yamlSupplierConfigs) == 0 {
return ErrRelayMinerConfigInvalidSupplier.Wrap("no suppliers provided")
}
relayMinerConfig.Servers = make(map[string]*RelayMinerServerConfig)
for _, yamlSupplierConfig := range yamlSupplierConfigs {
listenUrl, err := url.Parse(yamlSupplierConfig.ListenUrl)
if err != nil {
return ErrRelayMinerConfigInvalidServer.Wrapf(
"invalid listen url %q",
yamlSupplierConfig.ListenUrl,
)
}
if listenUrl.Scheme == "" {
return ErrRelayMinerConfigInvalidServer.Wrapf(
"missing scheme in listen url %q",
yamlSupplierConfig.ListenUrl,
)
}
if _, ok := relayMinerConfig.Servers[yamlSupplierConfig.ListenUrl]; ok {
continue
}
serverConfig := &RelayMinerServerConfig{
XForwardedHostLookup: yamlSupplierConfig.XForwardedHostLookup,
SupplierConfigsMap: make(map[string]*RelayMinerSupplierConfig),
}
// Populate the server fields that are relevant to each supported server type
switch listenUrl.Scheme {
case "http", "ws":
if err := serverConfig.parseHTTPServerConfig(yamlSupplierConfig); err != nil {
return err
}
serverConfig.ServerType = RelayMinerServerTypeHTTP
default:
// Fail if the relay miner server type is not supported
return ErrRelayMinerConfigInvalidServer.Wrapf(
"invalid relay miner server type %q",
listenUrl.Scheme,
)
}
relayMinerConfig.Servers[yamlSupplierConfig.ListenUrl] = serverConfig
}
return nil
}