2020-04-06 22:16:25 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ApiListenUrl() *url.URL {
|
|
|
|
const defaultPort = "4001"
|
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
shouldServeUI := ShouldServeUI()
|
2020-04-06 22:16:25 +02:00
|
|
|
|
|
|
|
apiPrefix := "/"
|
|
|
|
if shouldServeUI {
|
|
|
|
apiPrefix = "/api"
|
|
|
|
}
|
|
|
|
|
|
|
|
var listenAddr string
|
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
listenAddr = EnvListenIP.GetValue()
|
2020-04-06 22:16:25 +02:00
|
|
|
if listenAddr == "" {
|
|
|
|
listenAddr = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
listenPortStr := EnvListenPort.GetValue()
|
2020-04-06 22:16:25 +02:00
|
|
|
if listenPortStr == "" {
|
|
|
|
listenPortStr = defaultPort
|
|
|
|
}
|
|
|
|
|
|
|
|
listenPort, err := strconv.Atoi(listenPortStr)
|
|
|
|
if err != nil {
|
2021-01-17 16:50:48 +01:00
|
|
|
log.Fatalf("%s must be a number: '%s'\n%s", EnvListenPort.GetName(), listenPortStr, err)
|
2020-04-06 22:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiUrl, err := url.Parse(fmt.Sprintf("http://%s:%d", listenAddr, listenPort))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not format api url: %s", err)
|
|
|
|
}
|
|
|
|
apiUrl.Path = apiPrefix
|
|
|
|
|
|
|
|
return apiUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApiEndpointUrl() *url.URL {
|
2021-01-17 16:50:48 +01:00
|
|
|
apiEndpointStr := EnvAPIEndpoint.GetValue()
|
2020-04-06 22:16:25 +02:00
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
shouldServeUI := ShouldServeUI()
|
2020-04-06 22:16:25 +02:00
|
|
|
if shouldServeUI {
|
2021-01-17 16:50:48 +01:00
|
|
|
apiEndpointStr = EnvPublicEndpoint.GetValue()
|
2021-03-01 22:03:57 +01:00
|
|
|
if apiEndpointStr == "" {
|
|
|
|
apiEndpointStr = "/"
|
|
|
|
}
|
2020-04-06 22:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
apiEndpointUrl, err := url.Parse(apiEndpointStr)
|
|
|
|
if err != nil {
|
2021-01-17 16:50:48 +01:00
|
|
|
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvAPIEndpoint.GetName(), EnvAPIEndpoint.GetValue())
|
2020-04-06 22:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if shouldServeUI {
|
|
|
|
apiEndpointUrl.Path = path.Join(apiEndpointUrl.Path, "/api")
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiEndpointUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
func UiEndpointUrl() *url.URL {
|
2021-01-17 16:50:48 +01:00
|
|
|
uiEndpointStr := EnvUIEndpoint.GetValue()
|
2020-04-06 22:16:25 +02:00
|
|
|
|
2021-01-17 16:50:48 +01:00
|
|
|
shouldServeUI := ShouldServeUI()
|
2020-04-06 22:16:25 +02:00
|
|
|
if shouldServeUI {
|
2021-01-17 16:50:48 +01:00
|
|
|
uiEndpointStr = EnvPublicEndpoint.GetValue()
|
2021-03-01 22:03:57 +01:00
|
|
|
if uiEndpointStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-06 22:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uiEndpointUrl, err := url.Parse(uiEndpointStr)
|
|
|
|
if err != nil {
|
2021-01-17 16:50:48 +01:00
|
|
|
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvUIEndpoint.GetName(), EnvUIEndpoint.GetValue())
|
2020-04-06 22:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return uiEndpointUrl
|
|
|
|
}
|