/
http_service_configuration.go
104 lines (90 loc) · 3.39 KB
/
http_service_configuration.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
package service
import (
"fmt"
"github.com/borderzero/border0-go/lib/types/nilcheck"
)
const (
// HttpServiceTypeStandard is the http
// service type for standard http services.
HttpServiceTypeStandard = "standard"
// HttpServiceTypeConnectorFileServer is the http service
// type for the connector's built-in file webserver.
HttpServiceTypeConnectorFileServer = "connector_file_server"
)
// HttpServiceConfiguration represents service
// configuration for http services (fka sockets).
type HttpServiceConfiguration struct {
HttpServiceType string `json:"http_service_type"`
// mutually exclusive fields below
StandardHttpServiceConfiguration *StandardHttpServiceConfiguration `json:"standard_http_service_configuration,omitempty"`
FileServerHttpServiceConfiguration *FileServerHttpServiceConfiguration `json:"fileserver_http_service_configuration,omitempty"`
}
// StandardHttpServiceConfiguration represents service
// configuration for standard http services (fka sockets).
type StandardHttpServiceConfiguration struct {
HostnameAndPort // inherited
HostHeader string `json:"host_header"`
}
// FileServerHttpServiceConfiguration represents service
// configuration for the connector built-in file webserver.
type FileServerHttpServiceConfiguration struct {
TopLevelDirectory string `json:"top_level_directory,omitempty"`
}
// Validate validates the HttpServiceConfiguration.
func (c *HttpServiceConfiguration) Validate() error {
switch c.HttpServiceType {
case HttpServiceTypeStandard:
if nilcheck.AnyNotNil(c.FileServerHttpServiceConfiguration) {
return fmt.Errorf(
"http service type \"%s\" can only have standard http service configuration defined",
HttpServiceTypeStandard,
)
}
if c.StandardHttpServiceConfiguration == nil {
return fmt.Errorf(
"http service configuration for http service type \"%s\" must have standard http service configuration defined",
HttpServiceTypeStandard,
)
}
if err := c.StandardHttpServiceConfiguration.Validate(); err != nil {
return fmt.Errorf("invalid standard http service configuration: %v", err)
}
return nil
case HttpServiceTypeConnectorFileServer:
if nilcheck.AnyNotNil(c.StandardHttpServiceConfiguration) {
return fmt.Errorf(
"http service type \"%s\" can only have file server http service configuration defined",
HttpServiceTypeConnectorFileServer,
)
}
if c.FileServerHttpServiceConfiguration == nil {
return fmt.Errorf(
"http service configuration for http service type \"%s\" must have file server http service configuration defined",
HttpServiceTypeConnectorFileServer,
)
}
if err := c.FileServerHttpServiceConfiguration.Validate(); err != nil {
return fmt.Errorf("invalid file server http service configuration: %v", err)
}
return nil
default:
return fmt.Errorf("http service configuration has invalid http service type \"%s\"", c.HttpServiceType)
}
}
// Validate validates the StandardHttpServiceConfiguration.
func (c *StandardHttpServiceConfiguration) Validate() error {
if c.HostHeader == "" {
return fmt.Errorf("host_header is a required field")
}
if err := c.HostnameAndPort.Validate(); err != nil {
return err
}
return nil
}
// Validate validates the FileServerHttpServiceConfiguration.
func (c *FileServerHttpServiceConfiguration) Validate() error {
if c.TopLevelDirectory == "" {
return fmt.Errorf("top_level_directory is a required field")
}
return nil
}