From: Mischa Spiegelmock Date: Fri, 12 Feb 2021 17:54:35 +0000 (+0200) Subject: Make WS host configurable (#167) X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/readmes/%7B%7D/%7Bthis.state.thumbnail%7D?a=commitdiff_plain;h=99c7966200371da7e4f3615412cf18103dee44b7;p=lemmy-ui.git Make WS host configurable (#167) * Make WS host configurable * indent * Type fixes * Type lemmyConfig * typo * Move lemmy config to interfaces.ts --- diff --git a/src/server/index.tsx b/src/server/index.tsx index 21b3d05..666140f 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -5,7 +5,11 @@ import { renderToString } from 'inferno-server'; import { matchPath } from 'inferno-router'; import path from 'path'; import { App } from '../shared/components/app'; -import { InitialFetchRequest, IsoData } from '../shared/interfaces'; +import { + ILemmyConfig, + InitialFetchRequest, + IsoData, +} from '../shared/interfaces'; import { routes } from '../shared/routes'; import IsomorphicCookie from 'isomorphic-cookie'; import { GetSite, LemmyHttp } from 'lemmy-js-client'; @@ -95,11 +99,14 @@ server.get('/*', async (req, res) => { const cspStr = process.env.LEMMY_EXTERNAL_HOST ? renderToString(cspHtml) : ''; const helmet = Helmet.renderStatic(); + const config: ILemmyConfig = { wsHost: process.env.LEMMY_WS_HOST }; + res.send(` + ${helmet.title.toString()} ${helmet.meta.toString()} diff --git a/src/shared/env.ts b/src/shared/env.ts index 02d66e8..9fd5039 100644 --- a/src/shared/env.ts +++ b/src/shared/env.ts @@ -2,30 +2,39 @@ import { isBrowser } from './utils'; const testHost = 'localhost:8536'; -const internalHost = +let internalHost = (!isBrowser() && process.env.LEMMY_INTERNAL_HOST) || testHost; // used for local dev -export const externalHost = isBrowser() - ? `${window.location.hostname}${ - ['1234', '1235'].includes(window.location.port) - ? ':8536' - : window.location.port == '' - ? '' - : `:${window.location.port}` - }` - : process.env.LEMMY_EXTERNAL_HOST || testHost; - -const secure = isBrowser() - ? window.location.protocol == 'https:' - ? 's' - : '' - : process.env.LEMMY_HTTPS == 'true' - ? 's' - : ''; - -const host = isBrowser() ? externalHost : internalHost; +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_EXTERNAL_HOST || testHost; + host = internalHost; + wsHost = process.env.LEMMY_WS_HOST || host; + secure = process.env.LEMMY_HTTPS == 'true' ? 's' : ''; +} const httpBase = `http://${host}`; // Don't use secure here -export const wsUri = `ws${secure}://${host}/api/v2/ws`; +export const wsUri = `ws${secure}://${wsHost}/api/v2/ws`; export const httpUri = `${httpBase}/api/v2`; export const pictrsUri = `http${secure}://${host}/pictrs/image`; diff --git a/src/shared/interfaces.ts b/src/shared/interfaces.ts index 116f5d1..cc4c18a 100644 --- a/src/shared/interfaces.ts +++ b/src/shared/interfaces.ts @@ -14,9 +14,14 @@ export interface IsoData { // communities?: ListCommunitiesResponse; } +export interface ILemmyConfig { + wsHost?: string; +} + declare global { interface Window { isoData: IsoData; + lemmyConfig?: ILemmyConfig; } } diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 2f44142..016862f 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -54,7 +54,6 @@ import { CommentNode as CommentNodeI, } from './interfaces'; import { UserService, WebSocketService } from './services'; - var Tribute: any; if (isBrowser()) { Tribute = require('tributejs'); diff --git a/tsconfig.json b/tsconfig.json index cd9bc8d..aae5ae7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,6 @@ }, "include": [ "src/**/*", - "node_modules/inferno/dist/index.d.ts" + "node_modules/inferno/dist/index.d.ts", ] }