2020-11-20 23:40:57 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-11-21 01:44:05 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-11-20 23:40:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// webConfig is the structure which supports the configuration of the endpoint
|
|
|
|
// which provides access to the web frontend
|
|
|
|
type webConfig struct {
|
|
|
|
// 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 01:44:05 +01:00
|
|
|
|
2020-11-21 03:42:42 +01:00
|
|
|
// ContextRoot set the root context for the web application
|
2020-11-21 01:44:05 +01:00
|
|
|
ContextRoot string `yaml:"context-root"`
|
2020-11-20 23:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateAndSetDefaults checks and sets missing values based on the defaults
|
2020-11-21 13:11:37 +01:00
|
|
|
// in given in DefaultWebContext, DefaultAddress and DefaultPort if necessary
|
2020-11-20 23:40:57 +01:00
|
|
|
func (web *webConfig) validateAndSetDefaults() {
|
|
|
|
if len(web.Address) == 0 {
|
|
|
|
web.Address = DefaultAddress
|
|
|
|
}
|
|
|
|
if web.Port == 0 {
|
|
|
|
web.Port = DefaultPort
|
|
|
|
} else if web.Port < 0 || web.Port > math.MaxUint16 {
|
|
|
|
panic(fmt.Sprintf("port has an invalid: value should be between %d and %d", 0, math.MaxUint16))
|
|
|
|
}
|
2020-11-21 13:11:37 +01:00
|
|
|
|
|
|
|
web.ContextRoot = validateAndBuild(web.ContextRoot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateAndBuild validates and builds a checked
|
|
|
|
// path for the context root
|
|
|
|
func validateAndBuild(contextRoot string) string {
|
|
|
|
trimedContextRoot := strings.Trim(contextRoot, "/")
|
|
|
|
|
|
|
|
if len(trimedContextRoot) == 0 {
|
|
|
|
return DefaultContextRoot
|
2020-11-21 01:44:05 +01:00
|
|
|
} else {
|
2020-11-21 13:11:37 +01:00
|
|
|
url, err := url.Parse(trimedContextRoot)
|
2020-11-21 01:44:05 +01:00
|
|
|
if err != nil {
|
2020-11-21 13:11:37 +01:00
|
|
|
panic(fmt.Sprintf("Invalid context root %s - error: %s.", contextRoot, err))
|
2020-11-21 01:44:05 +01:00
|
|
|
}
|
2020-11-21 13:11:37 +01:00
|
|
|
if url.Path != trimedContextRoot {
|
|
|
|
panic(fmt.Sprintf("Invalid context root %s, simple path required.", contextRoot))
|
2020-11-21 03:42:42 +01:00
|
|
|
}
|
2020-11-21 13:11:37 +01:00
|
|
|
|
|
|
|
return "/" + strings.Trim(url.Path, "/") + "/"
|
2020-11-21 01:44:05 +01:00
|
|
|
}
|
2020-11-20 23:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SocketAddress returns the combination of the Address and the Port
|
|
|
|
func (web *webConfig) SocketAddress() string {
|
|
|
|
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
|
|
|
}
|
2020-11-21 01:44:05 +01:00
|
|
|
|
2020-11-21 21:38:45 +01:00
|
|
|
// PrependWithContextRoot appends the given string to the context root
|
|
|
|
// PrependWithContextRoot takes care of having only one "/" character at
|
2020-11-21 01:44:05 +01:00
|
|
|
// the join point and exactly on "/" at the end
|
2020-11-21 21:38:45 +01:00
|
|
|
func (web *webConfig) PrependWithContextRoot(fragment string) string {
|
2020-11-21 03:42:42 +01:00
|
|
|
return web.ContextRoot + strings.Trim(fragment, "/") + "/"
|
2020-11-21 01:44:05 +01:00
|
|
|
}
|