]> Untitled Git - lemmy-ui.git/blob - src/server/index.tsx
Somewhat working webpack. Sponsors and communities pages done.
[lemmy-ui.git] / src / server / index.tsx
1 // import cookieParser = require('cookie-parser');
2 import serialize from 'serialize-javascript';
3 import express from 'express';
4 import { StaticRouter } from 'inferno-router';
5 import { renderToString } from 'inferno-server';
6 import { matchPath } from 'inferno-router';
7 import path from 'path';
8 import { App } from '../shared/components/app';
9 import { IsoData } from '../shared/interfaces';
10 import { routes } from '../shared/routes';
11 import IsomorphicCookie from 'isomorphic-cookie';
12 import { lemmyHttp, setAuth } from '../shared/utils';
13 import { GetSiteForm } from 'lemmy-js-client';
14 const server = express();
15 const port = 1234;
16
17 server.use(express.json());
18 server.use(express.urlencoded({ extended: false }));
19 server.use('/assets', express.static(path.resolve('./src/assets')));
20 server.use('/static', express.static(path.resolve('./dist')));
21
22 // server.use(cookieParser());
23
24 server.get('/*', async (req, res) => {
25   const activeRoute = routes.find(route => matchPath(req.url, route)) || {};
26   const context = {} as any;
27   let auth: string = IsomorphicCookie.load('jwt', req);
28
29   let getSiteForm: GetSiteForm = {};
30   setAuth(getSiteForm, auth);
31
32   let promises: Promise<any>[] = [];
33
34   let siteData = lemmyHttp.getSite(getSiteForm);
35   promises.push(siteData);
36   if (activeRoute.fetchInitialData) {
37     promises.push(...activeRoute.fetchInitialData(auth, req.path));
38   }
39
40   let resolver = await Promise.all(promises);
41
42   let isoData: IsoData = {
43     path: req.path,
44     site: resolver[0],
45     routeData: resolver.slice(1, resolver.length),
46   };
47
48   console.log(activeRoute.path);
49
50   const wrapper = (
51     <StaticRouter location={req.url} context={isoData}>
52       <App site={isoData.site} />
53     </StaticRouter>
54   );
55   if (context.url) {
56     return res.redirect(context.url);
57   }
58
59   res.send(`
60            <!DOCTYPE html>
61            <html lang="en">
62            <head>
63            <script>window.isoData = ${serialize(isoData)}</script>      
64
65            <!-- Required meta tags -->
66            <meta name="Description" content="Lemmy">
67            <meta charset="utf-8">
68            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
69
70            <!-- Icons -->
71            <link rel="shortcut icon" type="image/svg+xml" href="/assets/favicon.svg" />
72            <!-- <link rel="apple-touch-icon" href="/assets/apple-touch-icon.png" /> -->
73
74            <!-- Styles -->
75            <link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
76            </head>
77
78            <body>
79              <div id='root'>${renderToString(wrapper)}</div>
80              <script src='/static/js/client.js'></script>
81            </body>
82          </html>
83 `);
84 });
85 let Server = server.listen(port, () => {
86   console.log(`http://localhost:${port}`);
87 });
88
89 /**
90  * Used to restart server by fuseBox
91  */
92 export async function shutdown() {
93   Server.close();
94   Server = undefined;
95 }