authorizer/server/utils/urls.go

38 lines
558 B
Go
Raw Normal View History

package utils
import (
"net/url"
2021-07-21 23:52:53 +00:00
"strings"
"github.com/yauthdev/yauth/server/constants"
)
func GetFrontendHost() string {
u, err := url.Parse(constants.FRONTEND_URL)
if err != nil {
return `localhost`
}
2021-07-21 23:52:53 +00:00
host := u.Hostname()
hostParts := strings.Split(host, ".")
hostPartsLen := len(hostParts)
2021-07-21 15:57:12 +00:00
2021-07-21 23:52:53 +00:00
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
}