2020-09-07 03:41:46 +00:00
|
|
|
// import cookieParser = require('cookie-parser');
|
2020-09-06 16:15:25 +00:00
|
|
|
import serialize from 'serialize-javascript';
|
|
|
|
import express from 'express';
|
2020-08-23 04:04:58 +00:00
|
|
|
import { StaticRouter } from 'inferno-router';
|
|
|
|
import { renderToString } from 'inferno-server';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { matchPath } from 'inferno-router';
|
2020-09-07 03:41:46 +00:00
|
|
|
import path from 'path';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { App } from '../shared/components/app';
|
2020-09-07 03:41:46 +00:00
|
|
|
import { IsoData } from '../shared/interfaces';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { routes } from '../shared/routes';
|
|
|
|
import IsomorphicCookie from 'isomorphic-cookie';
|
2020-09-07 03:41:46 +00:00
|
|
|
import { lemmyHttp, setAuth } from '../shared/utils';
|
2020-11-10 21:50:23 +00:00
|
|
|
import { GetSiteForm } from 'lemmy-js-client';
|
2020-09-10 16:39:01 +00:00
|
|
|
import process from 'process';
|
2020-09-11 04:03:01 +00:00
|
|
|
import { Helmet } from 'inferno-helmet';
|
2020-10-26 07:09:44 +00:00
|
|
|
import { initializeSite } from '../shared/initialize';
|
2020-09-10 16:39:01 +00:00
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
const server = express();
|
|
|
|
const port = 1234;
|
|
|
|
|
|
|
|
server.use(express.json());
|
|
|
|
server.use(express.urlencoded({ extended: false }));
|
2020-09-07 03:41:46 +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
|
|
|
|
2020-09-07 03:41:46 +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;
|
2020-09-06 16:15:25 +00:00
|
|
|
let auth: string = IsomorphicCookie.load('jwt', req);
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
let getSiteForm: GetSiteForm = {};
|
|
|
|
setAuth(getSiteForm, auth);
|
|
|
|
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
2020-11-10 21:50:23 +00:00
|
|
|
// Get site data first
|
|
|
|
let site = await lemmyHttp.getSite(getSiteForm);
|
|
|
|
initializeSite(site);
|
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
if (activeRoute.fetchInitialData) {
|
|
|
|
promises.push(...activeRoute.fetchInitialData(auth, req.path));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-23 18:46:50 +00:00
|
|
|
let acceptLang = req.headers['accept-language']
|
|
|
|
? req.headers['accept-language'].split(',')[0]
|
|
|
|
: 'en';
|
2020-09-07 15:32:07 +00:00
|
|
|
let lang = !!site.my_user
|
|
|
|
? site.my_user.lang == 'browser'
|
|
|
|
? acceptLang
|
|
|
|
: 'en'
|
|
|
|
: acceptLang;
|
2020-09-07 03:41:46 +00:00
|
|
|
|
|
|
|
let isoData: IsoData = {
|
|
|
|
path: req.path,
|
2020-09-07 15:32:07 +00:00
|
|
|
site,
|
|
|
|
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}>
|
|
|
|
<App site={isoData.site} />
|
2020-08-23 04:04:58 +00:00
|
|
|
</StaticRouter>
|
|
|
|
);
|
|
|
|
if (context.url) {
|
|
|
|
return res.redirect(context.url);
|
|
|
|
}
|
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
const root = renderToString(wrapper);
|
|
|
|
const helmet = Helmet.renderStatic();
|
|
|
|
|
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>
|
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-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
|
|
|
<!-- Icons -->
|
2020-09-11 02:40:41 +00:00
|
|
|
<link rel="shortcut icon" type="image/svg+xml" href="/static/assets/icons/favicon.svg" />
|
|
|
|
<link rel="apple-touch-icon" href="/static/assets/icons/apple-touch-icon.png" />
|
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
|
|
|
`);
|
|
|
|
});
|
|
|
|
let Server = server.listen(port, () => {
|
|
|
|
console.log(`http://localhost:${port}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to restart server by fuseBox
|
|
|
|
*/
|
|
|
|
export async function shutdown() {
|
|
|
|
Server.close();
|
|
|
|
Server = undefined;
|
|
|
|
}
|
2020-09-10 16:39:01 +00:00
|
|
|
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.info('Interrupted');
|
|
|
|
process.exit(0);
|
|
|
|
});
|