From 5850ed82e44ef445795d4ccdb390e22f4efdf30d Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 21 Nov 2020 17:35:08 -0500 Subject: [PATCH] Refactor code and update comments --- config/config.go | 1 + config/web.go | 45 ++++++++++++++++++++------------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/config/config.go b/config/config.go index be6f7e37..a9c044aa 100644 --- a/config/config.go +++ b/config/config.go @@ -87,6 +87,7 @@ func Get() *Config { } // 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 { log.Printf("[config][Load] Reading configuration from configFile=%s", configFile) cfg, err := readConfigurationFile(configFile) diff --git a/config/web.go b/config/web.go index 892b2a76..342929ff 100644 --- a/config/web.go +++ b/config/web.go @@ -20,38 +20,35 @@ type webConfig struct { ContextRoot string `yaml:"context-root"` } -// validateAndSetDefaults checks and sets missing values based on the defaults -// in given in DefaultWebContext, DefaultAddress and DefaultPort if necessary +// validateAndSetDefaults checks and sets the default values for fields that are not set func (web *webConfig) validateAndSetDefaults() { + // Validate the Address if len(web.Address) == 0 { web.Address = DefaultAddress } + // Validate the Port 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)) + panic(fmt.Sprintf("invalid port: value should be between %d and %d", 0, math.MaxUint16)) } - - 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 + // Validate the ContextRoot + if len(web.ContextRoot) == 0 { + web.ContextRoot = DefaultContextRoot } 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 { - panic(fmt.Sprintf("Invalid context root %s - error: %s.", contextRoot, err)) + panic("invalid context root:" + err.Error()) } - if url.Path != trimedContextRoot { - panic(fmt.Sprintf("Invalid context root %s, simple path required.", contextRoot)) + if rootContextURL.Path != trimmedContextRoot { + panic("invalid context root: too complex") } - - return "/" + strings.Trim(url.Path, "/") + "/" + web.ContextRoot = "/" + strings.Trim(rootContextURL.Path, "/") + "/" } } @@ -60,9 +57,7 @@ func (web *webConfig) SocketAddress() string { return fmt.Sprintf("%s:%d", web.Address, web.Port) } -// PrependWithContextRoot appends the given string to the context root -// PrependWithContextRoot takes care of having only one "/" character at -// the join point and exactly on "/" at the end -func (web *webConfig) PrependWithContextRoot(fragment string) string { - return web.ContextRoot + strings.Trim(fragment, "/") + "/" +// PrependWithContextRoot appends the given path to the ContextRoot +func (web *webConfig) PrependWithContextRoot(path string) string { + return web.ContextRoot + strings.Trim(path, "/") + "/" }