]> Untitled Git - lemmy-ui.git/blob - src/shared/env.ts
Make WS host configurable (#167)
[lemmy-ui.git] / src / shared / env.ts
1 import { isBrowser } from './utils';
2
3 const testHost = 'localhost:8536';
4
5 let internalHost =
6   (!isBrowser() && process.env.LEMMY_INTERNAL_HOST) || testHost; // used for local dev
7 export let externalHost: string;
8 let host: string;
9 let wsHost: string;
10 let secure: string;
11
12 if (isBrowser()) {
13   // browser
14   const lemmyConfig =
15     typeof window.lemmyConfig !== 'undefined' ? window.lemmyConfig : {};
16
17   externalHost = `${window.location.hostname}${
18     ['1234', '1235'].includes(window.location.port)
19       ? ':8536'
20       : window.location.port == ''
21       ? ''
22       : `:${window.location.port}`
23   }`;
24
25   host = externalHost;
26   wsHost = lemmyConfig.wsHost || host;
27   secure = window.location.protocol == 'https:' ? 's' : '';
28 } else {
29   // server-side
30   externalHost = process.env.LEMMY_EXTERNAL_HOST || testHost;
31   host = internalHost;
32   wsHost = process.env.LEMMY_WS_HOST || host;
33   secure = process.env.LEMMY_HTTPS == 'true' ? 's' : '';
34 }
35
36 const httpBase = `http://${host}`; // Don't use secure here
37 export const wsUri = `ws${secure}://${wsHost}/api/v2/ws`;
38 export const httpUri = `${httpBase}/api/v2`;
39 export const pictrsUri = `http${secure}://${host}/pictrs/image`;
40
41 console.log(`httpbase: ${httpBase}`);
42 console.log(`wsUri: ${wsUri}`);
43
44 // This is for html tags, don't include port
45 const httpExternalUri = `http${secure}://${externalHost.split(':')[0]}`;
46 export function httpExternalPath(path: string) {
47   return `${httpExternalUri}${path}`;
48 }