2017-10-22 23:33:10 +00:00
|
|
|
import React from 'react';
|
2016-09-12 00:47:27 +00:00
|
|
|
import path from 'path';
|
2017-10-11 07:14:45 +00:00
|
|
|
import fs from 'fs';
|
2016-04-29 05:25:37 +00:00
|
|
|
import httpErrors from 'http-errors';
|
|
|
|
import Koa from 'koa';
|
|
|
|
import Router from 'koa-router';
|
|
|
|
import sendfile from 'koa-sendfile';
|
2017-10-22 23:33:10 +00:00
|
|
|
import ReactDOMServer from 'react-dom/server';
|
2016-05-27 07:06:36 +00:00
|
|
|
import subdomainRedirect from './middlewares/subdomainRedirect';
|
2017-10-22 23:33:10 +00:00
|
|
|
import { Helmet } from 'react-helmet';
|
2016-05-27 07:06:36 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
|
|
|
|
import Layout from './pages/components/Layout';
|
|
|
|
import Home from './pages/Home';
|
|
|
|
|
|
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
2016-04-29 05:25:37 +00:00
|
|
|
const koa = new Koa();
|
|
|
|
const router = new Router();
|
2017-10-22 23:33:10 +00:00
|
|
|
const sheet = new ServerStyleSheet();
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-10-11 07:14:45 +00:00
|
|
|
const readFile = src => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readFile(src, { encoding: 'utf8' }, (err, data) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2016-05-07 16:18:20 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
const renderPage = children => {
|
|
|
|
const html = ReactDOMServer.renderToString(
|
|
|
|
<StyleSheetManager sheet={sheet.instance}>
|
|
|
|
<Layout>
|
|
|
|
{children}
|
|
|
|
</Layout>
|
|
|
|
</StyleSheetManager>
|
|
|
|
);
|
|
|
|
|
|
|
|
// helmet returns an object of meta tags with toString methods, urgh.
|
|
|
|
const helmet = Helmet.renderStatic();
|
|
|
|
let head = '';
|
|
|
|
Object.keys(helmet).forEach(key => (head += helmet[key].toString()));
|
|
|
|
|
|
|
|
return html
|
|
|
|
.replace('{{CSS}}', sheet.getStyleTags())
|
|
|
|
.replace('{{HEAD}}', head);
|
|
|
|
};
|
|
|
|
|
2016-09-05 21:26:06 +00:00
|
|
|
router.get('/_health', ctx => (ctx.body = 'OK'));
|
|
|
|
|
2016-08-19 15:11:21 +00:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2017-05-10 06:14:24 +00:00
|
|
|
router.get('/static/*', async ctx => {
|
2016-06-01 23:48:35 +00:00
|
|
|
ctx.set({
|
2016-09-12 00:47:27 +00:00
|
|
|
'Cache-Control': `max-age=${356 * 24 * 60 * 60}`,
|
2016-06-01 23:48:35 +00:00
|
|
|
});
|
|
|
|
|
2017-05-10 06:14:24 +00:00
|
|
|
await sendfile(
|
|
|
|
ctx,
|
|
|
|
path.join(__dirname, '../dist/', ctx.path.substring(8))
|
|
|
|
);
|
2016-05-07 16:18:20 +00:00
|
|
|
});
|
2017-10-22 23:33:10 +00:00
|
|
|
}
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
router.get('/', async ctx => {
|
|
|
|
if (ctx.cookies.get('loggedIn')) {
|
|
|
|
if (isProduction) {
|
|
|
|
ctx.body = await readFile(path.join(__dirname, '../dist/index.html'));
|
|
|
|
} else {
|
|
|
|
ctx.body = await readFile(path.join(__dirname, './static/dev.html'));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.body = await renderPage(<Home />);
|
|
|
|
}
|
2016-05-27 07:06:36 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
if (!ctx.status) ctx.throw(httpErrors.NotFound());
|
|
|
|
});
|
2017-10-11 07:14:45 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
router.get('*', async ctx => {
|
|
|
|
if (isProduction) {
|
|
|
|
ctx.body = await readFile(path.join(__dirname, '../dist/index.html'));
|
|
|
|
} else {
|
|
|
|
ctx.body = await readFile(path.join(__dirname, './static/dev.html'));
|
|
|
|
}
|
|
|
|
if (!ctx.status) ctx.throw(httpErrors.NotFound());
|
|
|
|
});
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2017-10-22 23:33:10 +00:00
|
|
|
koa.use(subdomainRedirect());
|
2016-04-29 05:25:37 +00:00
|
|
|
koa.use(router.routes());
|
|
|
|
|
|
|
|
// 404 handler
|
|
|
|
koa.use(async () => {
|
|
|
|
throw httpErrors.NotFound();
|
|
|
|
});
|
|
|
|
|
2016-06-01 23:48:35 +00:00
|
|
|
export default koa;
|