This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/routes.js
Tom Moor 802ce10f14 POC
2017-10-22 17:16:57 -07:00

69 lines
1.6 KiB
JavaScript

// @flow
import React from 'react';
import path from 'path';
import httpErrors from 'http-errors';
import Koa from 'koa';
import Router from 'koa-router';
import sendfile from 'koa-sendfile';
import subdomainRedirect from './middlewares/subdomainRedirect';
import renderpage from './utils/renderpage';
import Home from './pages/Home';
import About from './pages/About';
import Pricing from './pages/Pricing';
const isProduction = process.env.NODE_ENV === 'production';
const koa = new Koa();
const router = new Router();
const renderapp = async ctx => {
if (isProduction) {
await sendfile(ctx, path.join(__dirname, '../dist/index.html'));
} else {
await sendfile(ctx, path.join(__dirname, './static/dev.html'));
}
};
router.get('/_health', ctx => (ctx.body = 'OK'));
if (process.env.NODE_ENV === 'production') {
router.get('/static/*', async ctx => {
ctx.set({
'Cache-Control': `max-age=${356 * 24 * 60 * 60}`,
});
await sendfile(
ctx,
path.join(__dirname, '../dist/', ctx.path.substring(8))
);
});
}
// static pages
router.get('/about', ctx => renderpage(ctx, <About />));
router.get('/pricing', ctx => renderpage(ctx, <Pricing />));
// home page
router.get('/', async ctx => {
if (ctx.cookies.get('loggedIn')) {
await renderapp(ctx);
} else {
await renderpage(ctx, <Home />);
}
});
// catch all for react app
router.get('*', async ctx => {
await renderapp(ctx);
if (!ctx.status) ctx.throw(httpErrors.NotFound());
});
// middleware
koa.use(subdomainRedirect());
koa.use(router.routes());
koa.use(async () => {
throw httpErrors.NotFound();
});
export default koa;