Refactor code and update comments

This commit is contained in:
TwinProduction 2020-11-21 17:35:08 -05:00
parent 53e1012ca1
commit 5850ed82e4
2 changed files with 21 additions and 25 deletions

View File

@ -87,6 +87,7 @@ func Get() *Config {
} }
// Load loads a custom configuration file // Load loads a custom configuration file
// Note that the misconfiguration of some fields may lead to panics. This is on purpose.
func Load(configFile string) error { func Load(configFile string) error {
log.Printf("[config][Load] Reading configuration from configFile=%s", configFile) log.Printf("[config][Load] Reading configuration from configFile=%s", configFile)
cfg, err := readConfigurationFile(configFile) cfg, err := readConfigurationFile(configFile)

View File

@ -20,38 +20,35 @@ type webConfig struct {
ContextRoot string `yaml:"context-root"` ContextRoot string `yaml:"context-root"`
} }
// validateAndSetDefaults checks and sets missing values based on the defaults // validateAndSetDefaults checks and sets the default values for fields that are not set
// in given in DefaultWebContext, DefaultAddress and DefaultPort if necessary
func (web *webConfig) validateAndSetDefaults() { func (web *webConfig) validateAndSetDefaults() {
// Validate the Address
if len(web.Address) == 0 { if len(web.Address) == 0 {
web.Address = DefaultAddress web.Address = DefaultAddress
} }
// Validate the Port
if web.Port == 0 { if web.Port == 0 {
web.Port = DefaultPort web.Port = DefaultPort
} else if web.Port < 0 || web.Port > math.MaxUint16 { } 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)) panic(fmt.Sprintf("invalid port: value should be between %d and %d", 0, math.MaxUint16))
} }
// Validate the ContextRoot
web.ContextRoot = validateAndBuild(web.ContextRoot) if len(web.ContextRoot) == 0 {
} web.ContextRoot = DefaultContextRoot
// 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
} else { } else {
url, err := url.Parse(trimedContextRoot) trimmedContextRoot := strings.Trim(web.ContextRoot, "/")
if len(trimmedContextRoot) == 0 {
web.ContextRoot = DefaultContextRoot
return
}
rootContextURL, err := url.Parse(trimmedContextRoot)
if err != nil { if err != nil {
panic(fmt.Sprintf("Invalid context root %s - error: %s.", contextRoot, err)) panic("invalid context root:" + err.Error())
} }
if url.Path != trimedContextRoot { if rootContextURL.Path != trimmedContextRoot {
panic(fmt.Sprintf("Invalid context root %s, simple path required.", contextRoot)) panic("invalid context root: too complex")
} }
web.ContextRoot = "/" + strings.Trim(rootContextURL.Path, "/") + "/"
return "/" + strings.Trim(url.Path, "/") + "/"
} }
} }
@ -60,9 +57,7 @@ func (web *webConfig) SocketAddress() string {
return fmt.Sprintf("%s:%d", web.Address, web.Port) return fmt.Sprintf("%s:%d", web.Address, web.Port)
} }
// PrependWithContextRoot appends the given string to the context root // PrependWithContextRoot appends the given path to the ContextRoot
// PrependWithContextRoot takes care of having only one "/" character at func (web *webConfig) PrependWithContextRoot(path string) string {
// the join point and exactly on "/" at the end return web.ContextRoot + strings.Trim(path, "/") + "/"
func (web *webConfig) PrependWithContextRoot(fragment string) string {
return web.ContextRoot + strings.Trim(fragment, "/") + "/"
} }