2021-02-22 02:39:04 +00:00
|
|
|
import serialize from "serialize-javascript";
|
|
|
|
import express from "express";
|
|
|
|
import { StaticRouter } from "inferno-router";
|
|
|
|
import { renderToString } from "inferno-server";
|
|
|
|
import { matchPath } from "inferno-router";
|
|
|
|
import path from "path";
|
|
|
|
import { App } from "../shared/components/app";
|
2021-02-12 17:54:35 +00:00
|
|
|
import {
|
|
|
|
ILemmyConfig,
|
|
|
|
InitialFetchRequest,
|
|
|
|
IsoData,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../shared/interfaces";
|
|
|
|
import { routes } from "../shared/routes";
|
|
|
|
import IsomorphicCookie from "isomorphic-cookie";
|
2021-03-15 20:54:17 +00:00
|
|
|
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
|
2021-02-22 02:39:04 +00:00
|
|
|
import process from "process";
|
|
|
|
import { Helmet } from "inferno-helmet";
|
|
|
|
import { initializeSite } from "../shared/initialize";
|
|
|
|
import { httpUri } from "../shared/env";
|
|
|
|
import { IncomingHttpHeaders } from "http";
|
|
|
|
import { setOptionalAuth } from "../shared/utils";
|
2020-09-10 16:39:01 +00:00
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
const server = express();
|
2021-03-29 15:35:32 +00:00
|
|
|
const hostname = process.env['LEMMY_UI_HOST'] || 'localhost'
|
|
|
|
const port = process.env['LEMMY_UI_PORT'] || 1234;
|
2020-08-23 04:04:58 +00:00
|
|
|
|
|
|
|
server.use(express.json());
|
|
|
|
server.use(express.urlencoded({ extended: false }));
|
2021-02-22 02:39:04 +00:00
|
|
|
server.use("/static", express.static(path.resolve("./dist")));
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
// server.use(cookieParser());
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
server.get("/*", async (req, res) => {
|
2020-09-24 13:42:20 +00:00
|
|
|
const activeRoute = routes.find(route => matchPath(req.path, route)) || {};
|
2020-08-23 04:04:58 +00:00
|
|
|
const context = {} as any;
|
2021-02-22 02:39:04 +00:00
|
|
|
let auth: string = IsomorphicCookie.load("jwt", req);
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
let getSiteForm: GetSite = {};
|
|
|
|
setOptionalAuth(getSiteForm, auth);
|
2020-09-07 03:41:46 +00:00
|
|
|
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
let headers = setForwardedHeaders(req.headers);
|
|
|
|
|
|
|
|
let initialFetchReq: InitialFetchRequest = {
|
|
|
|
client: new LemmyHttp(httpUri, headers),
|
|
|
|
auth,
|
|
|
|
path: req.path,
|
|
|
|
};
|
|
|
|
|
2020-11-10 21:50:23 +00:00
|
|
|
// Get site data first
|
2021-03-15 20:54:17 +00:00
|
|
|
// This bypasses errors, so that the client can hit the error on its own,
|
|
|
|
// in order to remove the jwt on the browser. Necessary for wrong jwts
|
|
|
|
let try_site: any = await initialFetchReq.client.getSite(getSiteForm);
|
|
|
|
if (try_site.error == "not_logged_in") {
|
|
|
|
console.error(
|
|
|
|
"Incorrect JWT token, skipping auth so frontend can remove jwt cookie"
|
|
|
|
);
|
|
|
|
delete getSiteForm.auth;
|
|
|
|
delete initialFetchReq.auth;
|
|
|
|
try_site = await initialFetchReq.client.getSite(getSiteForm);
|
|
|
|
}
|
|
|
|
let site: GetSiteResponse = try_site;
|
2020-11-10 21:50:23 +00:00
|
|
|
initializeSite(site);
|
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
if (activeRoute.fetchInitialData) {
|
2020-11-12 21:56:46 +00:00
|
|
|
promises.push(...activeRoute.fetchInitialData(initialFetchReq));
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 21:50:23 +00:00
|
|
|
let routeData = await Promise.all(promises);
|
2020-09-07 15:32:07 +00:00
|
|
|
|
2020-11-10 18:24:47 +00:00
|
|
|
// Redirect to the 404 if there's an API error
|
|
|
|
if (routeData[0] && routeData[0].error) {
|
2020-11-10 22:45:59 +00:00
|
|
|
let errCode = routeData[0].error;
|
|
|
|
return res.redirect(`/404?err=${errCode}`);
|
2020-11-10 18:24:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
let acceptLang = req.headers["accept-language"]
|
|
|
|
? req.headers["accept-language"].split(",")[0]
|
|
|
|
: "en";
|
2021-02-22 02:24:09 +00:00
|
|
|
let lang = site.my_user
|
2021-03-15 18:09:31 +00:00
|
|
|
? site.my_user.local_user.lang == "browser"
|
2020-09-07 15:32:07 +00:00
|
|
|
? acceptLang
|
2021-02-22 02:39:04 +00:00
|
|
|
: "en"
|
2020-09-07 15:32:07 +00:00
|
|
|
: acceptLang;
|
2020-09-07 03:41:46 +00:00
|
|
|
|
|
|
|
let isoData: IsoData = {
|
|
|
|
path: req.path,
|
2020-12-24 01:58:27 +00:00
|
|
|
site_res: site,
|
2020-09-07 15:32:07 +00:00
|
|
|
routeData,
|
|
|
|
lang,
|
2020-09-07 03:41:46 +00:00
|
|
|
};
|
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
const wrapper = (
|
2020-09-07 03:41:46 +00:00
|
|
|
<StaticRouter location={req.url} context={isoData}>
|
2020-12-24 01:58:27 +00:00
|
|
|
<App siteRes={isoData.site_res} />
|
2020-08-23 04:04:58 +00:00
|
|
|
</StaticRouter>
|
|
|
|
);
|
|
|
|
if (context.url) {
|
|
|
|
return res.redirect(context.url);
|
|
|
|
}
|
|
|
|
|
2020-11-25 20:06:38 +00:00
|
|
|
const cspHtml = (
|
|
|
|
<meta
|
|
|
|
http-equiv="Content-Security-Policy"
|
2021-01-24 18:52:52 +00:00
|
|
|
content="default-src data: 'self'; connect-src * ws: wss:; frame-src *; img-src * data:; script-src 'self'; style-src 'self' 'unsafe-inline'; manifest-src 'self'"
|
2020-11-25 20:06:38 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
const root = renderToString(wrapper);
|
2021-02-22 02:39:04 +00:00
|
|
|
const cspStr = process.env.LEMMY_EXTERNAL_HOST ? renderToString(cspHtml) : "";
|
2020-09-11 04:03:01 +00:00
|
|
|
const helmet = Helmet.renderStatic();
|
|
|
|
|
2021-02-12 17:54:35 +00:00
|
|
|
const config: ILemmyConfig = { wsHost: process.env.LEMMY_WS_HOST };
|
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
res.send(`
|
2020-09-06 16:15:25 +00:00
|
|
|
<!DOCTYPE html>
|
2020-09-11 04:03:01 +00:00
|
|
|
<html ${helmet.htmlAttributes.toString()} lang="en">
|
2020-09-06 16:15:25 +00:00
|
|
|
<head>
|
2020-10-26 14:22:14 +00:00
|
|
|
<script>window.isoData = ${serialize(isoData)}</script>
|
2021-02-12 17:54:35 +00:00
|
|
|
<script>window.lemmyConfig = ${serialize(config)}</script>
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
${helmet.title.toString()}
|
|
|
|
${helmet.meta.toString()}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
<!-- Required meta tags -->
|
|
|
|
<meta name="Description" content="Lemmy">
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
|
|
|
2020-11-23 17:41:57 +00:00
|
|
|
<!-- Content Security Policy -->
|
2020-11-25 20:06:38 +00:00
|
|
|
${cspStr}
|
2020-11-23 17:41:57 +00:00
|
|
|
|
2020-09-10 02:38:57 +00:00
|
|
|
<!-- Web app manifest -->
|
2020-09-10 16:39:01 +00:00
|
|
|
<link rel="manifest" href="/static/assets/manifest.webmanifest">
|
2020-09-10 02:38:57 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
<!-- Styles -->
|
2020-09-07 03:41:46 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
|
2020-10-26 14:28:17 +00:00
|
|
|
|
|
|
|
<!-- Current theme and more -->
|
|
|
|
${helmet.link.toString()}
|
2020-09-06 16:15:25 +00:00
|
|
|
</head>
|
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
<body ${helmet.bodyAttributes.toString()}>
|
2020-09-09 20:33:40 +00:00
|
|
|
<noscript>
|
2020-09-14 15:26:24 +00:00
|
|
|
<div class="alert alert-danger rounded-0" role="alert">
|
2020-09-15 13:20:19 +00:00
|
|
|
<b>Javascript is disabled. Actions will not work.</b>
|
2020-09-14 15:26:24 +00:00
|
|
|
</div>
|
2020-09-09 20:33:40 +00:00
|
|
|
</noscript>
|
2020-10-26 07:09:44 +00:00
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
<div id='root'>${root}</div>
|
2020-09-24 22:03:03 +00:00
|
|
|
<script defer src='/static/js/client.js'></script>
|
2020-09-06 16:15:25 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
2020-08-23 04:04:58 +00:00
|
|
|
`);
|
|
|
|
});
|
2020-11-12 21:56:46 +00:00
|
|
|
|
2021-03-29 15:35:32 +00:00
|
|
|
server.listen(Number(port), hostname, () => {
|
|
|
|
console.log(`http://${hostname}:${port}`);
|
2020-08-23 04:04:58 +00:00
|
|
|
});
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
function setForwardedHeaders(
|
|
|
|
headers: IncomingHttpHeaders
|
|
|
|
): { [key: string]: string } {
|
|
|
|
let out = {
|
|
|
|
host: headers.host,
|
|
|
|
};
|
2021-02-22 02:39:04 +00:00
|
|
|
if (headers["x-real-ip"]) {
|
|
|
|
out["x-real-ip"] = headers["x-real-ip"];
|
2020-11-12 21:56:46 +00:00
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
if (headers["x-forwarded-for"]) {
|
|
|
|
out["x-forwarded-for"] = headers["x-forwarded-for"];
|
2020-11-12 21:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2020-08-23 04:04:58 +00:00
|
|
|
}
|
2020-09-10 16:39:01 +00:00
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
process.on("SIGINT", () => {
|
|
|
|
console.info("Interrupted");
|
2020-09-10 16:39:01 +00:00
|
|
|
process.exit(0);
|
|
|
|
});
|