2021-07-14 18:43:19 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2021-08-04 06:48:57 +00:00
|
|
|
"strings"
|
2021-07-14 18:43:19 +00:00
|
|
|
)
|
|
|
|
|
2021-12-07 12:20:50 +00:00
|
|
|
// GetHostName function to get hostname
|
2021-08-04 06:48:57 +00:00
|
|
|
func GetHostName(auth_url string) string {
|
|
|
|
u, err := url.Parse(auth_url)
|
|
|
|
if err != nil {
|
|
|
|
return `localhost`
|
|
|
|
}
|
|
|
|
|
|
|
|
host := u.Hostname()
|
|
|
|
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
2021-12-11 01:11:35 +00:00
|
|
|
// GetDomainName function to get domain name
|
2021-07-28 18:23:54 +00:00
|
|
|
func GetDomainName(auth_url string) string {
|
2021-08-04 06:48:57 +00:00
|
|
|
u, err := url.Parse(auth_url)
|
2021-07-14 18:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return `localhost`
|
|
|
|
}
|
|
|
|
|
2021-07-21 23:52:53 +00:00
|
|
|
host := u.Hostname()
|
|
|
|
|
2021-07-28 18:23:54 +00:00
|
|
|
// code to get root domain in case of sub-domains
|
2021-08-04 06:48:57 +00:00
|
|
|
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:], ".")
|
|
|
|
}
|
2021-07-21 23:52:53 +00:00
|
|
|
|
|
|
|
return host
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|