]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/env.ts
Documenting and changing a few env vars. Fixes #661 (#739)
[lemmy-ui.git] / src / shared / env.ts
index 7347a19f0e4b4823c74b73750aafc7994179a855..12260e151106b9c20cc64088612a3c19f5fc9999 100644 (file)
@@ -1,46 +1,51 @@
-import { isBrowser } from './utils';
-
-const testHost = 'localhost:8536';
-
-const internalHost = process.env.LEMMY_INTERNAL_HOST || testHost; // used for local dev
-export const externalHost = isBrowser()
-  ? `${window.location.hostname}:${
-      window.location.port == '1234' || window.location.port == '1235'
-        ? 8536
-        : window.location.port
-    }`
-  : process.env.LEMMY_EXTERNAL_HOST || testHost;
-
-//   ? window.location.port == '1234' || window.location.port == '1235'
-const secure = isBrowser() && window.location.protocol == 'https:' ? 's' : '';
-
-const host = isBrowser() ? externalHost : internalHost;
+import { isBrowser } from "./utils";
+
+const testHost = "0.0.0.0:8536";
+
+let internalHost =
+  (!isBrowser() && process.env.LEMMY_UI_LEMMY_INTERNAL_HOST) || testHost; // used for local dev
+export let externalHost: string;
+let host: string;
+let wsHost: string;
+let secure: string;
+
+if (isBrowser()) {
+  // browser
+  const lemmyConfig =
+    typeof window.lemmyConfig !== "undefined" ? window.lemmyConfig : {};
+
+  externalHost = `${window.location.hostname}${
+    ["1234", "1235"].includes(window.location.port)
+      ? ":8536"
+      : window.location.port == ""
+      ? ""
+      : `:${window.location.port}`
+  }`;
+
+  host = externalHost;
+  wsHost = lemmyConfig.wsHost || host;
+  secure = window.location.protocol == "https:" ? "s" : "";
+} else {
+  // server-side
+  externalHost = process.env.LEMMY_UI_LEMMY_EXTERNAL_HOST || testHost;
+  host = internalHost;
+  wsHost = process.env.LEMMY_UI_LEMMY_WS_HOST || externalHost;
+  secure = process.env.LEMMY_UI_HTTPS == "true" ? "s" : "";
+}
 
-const httpBase = `http${secure}://${host}`;
-export const wsUri = `ws${secure}://${host}/api/v1/ws`;
-export const httpUri = `${httpBase}/api/v1`;
-const httpExternalUri = `http${secure}://${externalHost}`;
+export const httpBaseInternal = `http://${host}`; // Don't use secure here
+export const httpBase = `http${secure}://${host}`;
+export const wsUriBase = `ws${secure}://${wsHost}`;
+export const wsUri = `${wsUriBase}/api/v3/ws`;
 export const pictrsUri = `${httpBase}/pictrs/image`;
+export const isHttps = secure.endsWith("s");
 
-console.log(`Internal host: ${internalHost}`);
-console.log(`External host: ${externalHost}`);
+console.log(`httpbase: ${httpBase}`);
+console.log(`wsUri: ${wsUri}`);
+console.log(`isHttps: ${isHttps}`);
 
+// This is for html tags, don't include port
+const httpExternalUri = `http${secure}://${externalHost.split(":")[0]}`;
 export function httpExternalPath(path: string) {
   return `${httpExternalUri}${path}`;
 }
-
-// export const httpUri = `http${secure}://${endpoint}/api/v1`;
-// export const pictrsUri = `http${secure}://${endpoint}/pictrs/image`;
-
-// const host = isBrowser() ? window.location.hostname : localHostname;
-// const secure = isBrowser() && window.location.protocol == 'https:' ? 's' : '';
-// const port = isBrowser()
-//   ? window.location.port == '1234' || window.location.port == '1235'
-//     ? 8536
-//     : window.location.port
-//   : 8536;
-// const endpoint = `${host}:${port}`;
-//
-// export const wsUri = `ws${secure}://${endpoint}/api/v1/ws`;
-// export const httpUri = `http${secure}://${endpoint}/api/v1`;
-// export const pictrsUri = `http${secure}://${endpoint}/pictrs/image`;