2021-07-14 18:43:19 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2021-08-04 06:48:57 +00:00
|
|
|
"strings"
|
2022-01-31 06:05:24 +00:00
|
|
|
|
2022-03-15 03:23:48 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2022-01-31 06:05:24 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-14 18:43:19 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 06:05:24 +00:00
|
|
|
// GetHost returns hostname from request context
|
|
|
|
func GetHost(c *gin.Context) string {
|
2022-01-31 09:00:13 +00:00
|
|
|
scheme := c.Request.Header.Get("X-Forwarded-Proto")
|
|
|
|
if scheme != "https" {
|
|
|
|
scheme = "http"
|
2022-01-31 06:05:24 +00:00
|
|
|
}
|
2022-01-31 09:00:13 +00:00
|
|
|
host := c.Request.Host
|
|
|
|
return scheme + "://" + host
|
2022-01-31 06:05:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:16:54 +00:00
|
|
|
// GetHostName function returns hostname and port
|
|
|
|
func GetHostParts(uri string) (string, string) {
|
|
|
|
tempURI := uri
|
2022-01-22 19:54:41 +00:00
|
|
|
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
|
2021-12-21 13:16:54 +00:00
|
|
|
tempURI = "https://" + tempURI
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(tempURI)
|
2021-08-04 06:48:57 +00:00
|
|
|
if err != nil {
|
2021-12-21 13:16:54 +00:00
|
|
|
return "localhost", "8080"
|
2021-08-04 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
host := u.Hostname()
|
2021-12-21 13:16:54 +00:00
|
|
|
port := u.Port()
|
2021-08-04 06:48:57 +00:00
|
|
|
|
2021-12-21 13:16:54 +00:00
|
|
|
return host, port
|
2021-08-04 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 01:11:35 +00:00
|
|
|
// GetDomainName function to get domain name
|
2021-12-21 13:16:54 +00:00
|
|
|
func GetDomainName(uri string) string {
|
|
|
|
tempURI := uri
|
2022-01-22 19:54:41 +00:00
|
|
|
if !strings.HasPrefix(tempURI, "http://") && !strings.HasPrefix(tempURI, "https://") {
|
2021-12-21 13:16:54 +00:00
|
|
|
tempURI = "https://" + tempURI
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(tempURI)
|
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
|
|
|
}
|
2022-03-15 03:23:48 +00:00
|
|
|
|
|
|
|
// GetAppURL to get /app/ url if not configured by user
|
|
|
|
func GetAppURL(gc *gin.Context) string {
|
|
|
|
envAppURL := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAppURL)
|
|
|
|
if envAppURL == "" {
|
2022-03-15 04:27:09 +00:00
|
|
|
envAppURL = GetHost(gc) + "/app"
|
2022-03-15 03:23:48 +00:00
|
|
|
}
|
|
|
|
return envAppURL
|
|
|
|
}
|