]> Untitled Git - lemmy-ui.git/blob - src/shared/env.ts
Make comment depth easier to track visually
[lemmy-ui.git] / src / shared / env.ts
1 import { isBrowser } from "./utils";
2
3 const testHost = "0.0.0.0:8536";
4
5 function getInternalHost() {
6   return !isBrowser()
7     ? process.env.LEMMY_UI_LEMMY_INTERNAL_HOST ?? testHost
8     : testHost; // used for local dev
9 }
10
11 export function getExternalHost() {
12   return isBrowser()
13     ? `${window.location.hostname}${
14         ["1234", "1235"].includes(window.location.port)
15           ? ":8536"
16           : window.location.port == ""
17           ? ""
18           : `:${window.location.port}`
19       }`
20     : process.env.LEMMY_UI_LEMMY_EXTERNAL_HOST || testHost;
21 }
22
23 function getSecure() {
24   return (
25     isBrowser()
26       ? window.location.protocol.includes("https")
27       : process.env.LEMMY_UI_HTTPS === "true"
28   )
29     ? "s"
30     : "";
31 }
32
33 function getHost() {
34   return isBrowser() ? getExternalHost() : getInternalHost();
35 }
36
37 function getWsHost() {
38   return isBrowser()
39     ? window.lemmyConfig?.wsHost ?? getHost()
40     : process.env.LEMMY_UI_LEMMY_WS_HOST ?? getExternalHost();
41 }
42
43 function getBaseLocal(s = "") {
44   return `http${s}://${getHost()}`;
45 }
46
47 export function getHttpBaseInternal() {
48   return getBaseLocal(); // Don't use secure here
49 }
50 export function getHttpBase() {
51   return getBaseLocal(getSecure());
52 }
53 export function getWsUri() {
54   return `ws${getSecure()}://${getWsHost()}/api/v3/ws`;
55 }
56 export function isHttps() {
57   return getSecure() === "s";
58 }
59
60 console.log(`httpbase: ${getHttpBase()}`);
61 console.log(`wsUri: ${getWsUri()}`);
62 console.log(`isHttps: ${isHttps()}`);
63
64 // This is for html tags, don't include port
65 export function httpExternalPath(path: string) {
66   return `http${getSecure()}://${getExternalHost().replace(
67     /:\d+/g,
68     ""
69   )}${path}`;
70 }