Make WS host configurable (#167)

* Make WS host configurable

* indent

* Type fixes

* Type lemmyConfig

* typo

* Move lemmy config to interfaces.ts
This commit is contained in:
Mischa Spiegelmock 2021-02-12 19:54:35 +02:00 committed by GitHub
parent ad1a350eed
commit 99c7966200
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 22 deletions

View file

@ -5,7 +5,11 @@ import { renderToString } from 'inferno-server';
import { matchPath } from 'inferno-router'; import { matchPath } from 'inferno-router';
import path from 'path'; import path from 'path';
import { App } from '../shared/components/app'; 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 { routes } from '../shared/routes';
import IsomorphicCookie from 'isomorphic-cookie'; import IsomorphicCookie from 'isomorphic-cookie';
import { GetSite, LemmyHttp } from 'lemmy-js-client'; 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 cspStr = process.env.LEMMY_EXTERNAL_HOST ? renderToString(cspHtml) : '';
const helmet = Helmet.renderStatic(); const helmet = Helmet.renderStatic();
const config: ILemmyConfig = { wsHost: process.env.LEMMY_WS_HOST };
res.send(` res.send(`
<!DOCTYPE html> <!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()} lang="en"> <html ${helmet.htmlAttributes.toString()} lang="en">
<head> <head>
<script>window.isoData = ${serialize(isoData)}</script> <script>window.isoData = ${serialize(isoData)}</script>
<script>window.lemmyConfig = ${serialize(config)}</script>
${helmet.title.toString()} ${helmet.title.toString()}
${helmet.meta.toString()} ${helmet.meta.toString()}

View file

@ -2,30 +2,39 @@ import { isBrowser } from './utils';
const testHost = 'localhost:8536'; const testHost = 'localhost:8536';
const internalHost = let internalHost =
(!isBrowser() && process.env.LEMMY_INTERNAL_HOST) || testHost; // used for local dev (!isBrowser() && process.env.LEMMY_INTERNAL_HOST) || testHost; // used for local dev
export const externalHost = isBrowser() export let externalHost: string;
? `${window.location.hostname}${ let host: string;
['1234', '1235'].includes(window.location.port) let wsHost: string;
? ':8536' let secure: string;
: window.location.port == ''
? ''
: `:${window.location.port}`
}`
: process.env.LEMMY_EXTERNAL_HOST || testHost;
const secure = isBrowser() if (isBrowser()) {
? window.location.protocol == 'https:' // browser
? 's' const lemmyConfig =
: '' typeof window.lemmyConfig !== 'undefined' ? window.lemmyConfig : {};
: process.env.LEMMY_HTTPS == 'true'
? 's'
: '';
const host = isBrowser() ? externalHost : internalHost; 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 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 httpUri = `${httpBase}/api/v2`;
export const pictrsUri = `http${secure}://${host}/pictrs/image`; export const pictrsUri = `http${secure}://${host}/pictrs/image`;

View file

@ -14,9 +14,14 @@ export interface IsoData {
// communities?: ListCommunitiesResponse; // communities?: ListCommunitiesResponse;
} }
export interface ILemmyConfig {
wsHost?: string;
}
declare global { declare global {
interface Window { interface Window {
isoData: IsoData; isoData: IsoData;
lemmyConfig?: ILemmyConfig;
} }
} }

View file

@ -54,7 +54,6 @@ import {
CommentNode as CommentNodeI, CommentNode as CommentNodeI,
} from './interfaces'; } from './interfaces';
import { UserService, WebSocketService } from './services'; import { UserService, WebSocketService } from './services';
var Tribute: any; var Tribute: any;
if (isBrowser()) { if (isBrowser()) {
Tribute = require('tributejs'); Tribute = require('tributejs');

View file

@ -22,6 +22,6 @@
}, },
"include": [ "include": [
"src/**/*", "src/**/*",
"node_modules/inferno/dist/index.d.ts" "node_modules/inferno/dist/index.d.ts",
] ]
} }