start working on context root configuration
This commit is contained in:
@ -3,6 +3,8 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// webConfig is the structure which supports the configuration of the endpoint
|
||||
@ -13,6 +15,10 @@ type webConfig struct {
|
||||
|
||||
// Port to listen on (default to 8080 specified by DefaultPort)
|
||||
Port int `yaml:"port"`
|
||||
|
||||
ContextRoot string `yaml:"context-root"`
|
||||
|
||||
safeContextRoot string
|
||||
}
|
||||
|
||||
// validateAndSetDefaults checks and sets missing values based on the defaults
|
||||
@ -26,9 +32,43 @@ func (web *webConfig) validateAndSetDefaults() {
|
||||
} 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))
|
||||
}
|
||||
if len(web.ContextRoot) == 0 {
|
||||
web.safeContextRoot = DefaultContextRoot
|
||||
} else {
|
||||
// url.PathEscape escapes all "/", in order to build a secure path
|
||||
// (1) split into path fragements using "/" as delimiter
|
||||
// (2) use url.PathEscape() on each fragment
|
||||
// (3) re-concatinate the path using "/" as join character
|
||||
const splitJoinChar = "/"
|
||||
pathes := strings.Split(web.ContextRoot, splitJoinChar)
|
||||
escapedPathes := make([]string, len(pathes))
|
||||
for i, path := range pathes {
|
||||
escapedPathes[i] = url.PathEscape(path)
|
||||
}
|
||||
|
||||
web.safeContextRoot = strings.Join(escapedPathes, splitJoinChar)
|
||||
|
||||
// assure that we have still a valid url
|
||||
_, err := url.Parse(web.safeContextRoot)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Invalid context root %s - Error %s", web.ContextRoot, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SocketAddress returns the combination of the Address and the Port
|
||||
func (web *webConfig) SocketAddress() string {
|
||||
return fmt.Sprintf("%s:%d", web.Address, web.Port)
|
||||
}
|
||||
|
||||
// CtxRoot returns the context root
|
||||
func (web *webConfig) CtxRoot() string {
|
||||
return web.safeContextRoot
|
||||
}
|
||||
|
||||
// AppendToCtxRoot appends the given string to the context root
|
||||
// AppendToCtxRoot takes care of having only one "/" character at
|
||||
// the join point and exactly on "/" at the end
|
||||
func (web *webConfig) AppendToCtxRoot(fragment string) string {
|
||||
return strings.TrimSuffix(web.safeContextRoot, "/") + "/" + strings.Trim(fragment, "/") + "/"
|
||||
}
|
||||
|
Reference in New Issue
Block a user