2020-11-20 22:40:57 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2021-02-01 06:37:56 +00:00
|
|
|
// WebConfig is the structure which supports the configuration of the endpoint
|
2020-11-20 22:40:57 +00:00
|
|
|
// which provides access to the web frontend
|
2021-02-01 06:37:56 +00:00
|
|
|
type WebConfig struct {
|
2020-11-20 22:40:57 +00:00
|
|
|
// Address to listen on (defaults to 0.0.0.0 specified by DefaultAddress)
|
|
|
|
Address string `yaml:"address"`
|
|
|
|
|
|
|
|
// Port to listen on (default to 8080 specified by DefaultPort)
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
}
|
|
|
|
|
2020-11-21 22:35:08 +00:00
|
|
|
// validateAndSetDefaults checks and sets the default values for fields that are not set
|
2021-02-01 06:37:56 +00:00
|
|
|
func (web *WebConfig) validateAndSetDefaults() {
|
2020-11-21 22:35:08 +00:00
|
|
|
// Validate the Address
|
2020-11-20 22:40:57 +00:00
|
|
|
if len(web.Address) == 0 {
|
|
|
|
web.Address = DefaultAddress
|
|
|
|
}
|
2020-11-21 22:35:08 +00:00
|
|
|
// Validate the Port
|
2020-11-20 22:40:57 +00:00
|
|
|
if web.Port == 0 {
|
|
|
|
web.Port = DefaultPort
|
|
|
|
} else if web.Port < 0 || web.Port > math.MaxUint16 {
|
2020-11-21 22:35:08 +00:00
|
|
|
panic(fmt.Sprintf("invalid port: value should be between %d and %d", 0, math.MaxUint16))
|
2020-11-20 22:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SocketAddress returns the combination of the Address and the Port
|
2021-02-01 06:37:56 +00:00
|
|
|
func (web *WebConfig) SocketAddress() string {
|
2020-11-20 22:40:57 +00:00
|
|
|
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
|
|
|
}
|