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 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(`
<!DOCTYPE html>
<html ${helmet.htmlAttributes.toString()} lang="en">
<head>
<script>window.isoData = ${serialize(isoData)}</script>
<script>window.lemmyConfig = ${serialize(config)}</script>
${helmet.title.toString()}
${helmet.meta.toString()}

View file

@ -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;
export let externalHost: string;
let host: string;
let wsHost: string;
let secure: string;
const secure = isBrowser()
? window.location.protocol == 'https:'
? 's'
: ''
: process.env.LEMMY_HTTPS == 'true'
? 's'
: '';
if (isBrowser()) {
// browser
const lemmyConfig =
typeof window.lemmyConfig !== 'undefined' ? window.lemmyConfig : {};
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
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`;

View file

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

View file

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

View file

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