authorizer/server/utils/urls.go

62 lines
1.1 KiB
Go

package utils
import (
"net/url"
"strings"
)
// GetHostName function returns hostname and port
func GetHostParts(uri string) (string, string) {
tempURI := uri
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
tempURI = "https://" + tempURI
}
u, err := url.Parse(tempURI)
if err != nil {
return "localhost", "8080"
}
host := u.Hostname()
port := u.Port()
return host, port
}
// GetDomainName function to get domain name
func GetDomainName(uri string) string {
tempURI := uri
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
tempURI = "https://" + tempURI
}
u, err := url.Parse(tempURI)
if err != nil {
return `localhost`
}
host := u.Hostname()
// code to get root domain in case of sub-domains
hostParts := strings.Split(host, ".")
hostPartsLen := len(hostParts)
if hostPartsLen == 1 {
return host
}
if hostPartsLen == 2 {
if hostParts[0] == "www" {
return hostParts[1]
} else {
return host
}
}
if hostPartsLen > 2 {
return strings.Join(hostParts[hostPartsLen-2:], ".")
}
return host
}