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

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-10-23 00:16:57 +00:00
// @flow
2017-10-22 23:33:10 +00:00
import React from 'react';
2016-09-12 00:47:27 +00:00
import path from 'path';
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';
2016-05-27 07:06:36 +00:00
import subdomainRedirect from './middlewares/subdomainRedirect';
2017-10-23 00:16:57 +00:00
import renderpage from './utils/renderpage';
2016-05-27 07:06:36 +00:00
2017-10-22 23:33:10 +00:00
import Home from './pages/Home';
2017-10-23 00:16:57 +00:00
import About from './pages/About';
import Pricing from './pages/Pricing';
2017-10-22 23:33:10 +00:00
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-23 00:16:57 +00:00
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'));
}
2017-10-22 23:33:10 +00:00
};
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-23 00:16:57 +00:00
// static pages
router.get('/about', ctx => renderpage(ctx, <About />));
router.get('/pricing', ctx => renderpage(ctx, <Pricing />));
// home page
2017-10-22 23:33:10 +00:00
router.get('/', async ctx => {
if (ctx.cookies.get('loggedIn')) {
2017-10-23 00:16:57 +00:00
await renderapp(ctx);
2017-10-22 23:33:10 +00:00
} else {
2017-10-23 00:16:57 +00:00
await renderpage(ctx, <Home />);
2017-10-22 23:33:10 +00:00
}
});
2017-10-23 00:16:57 +00:00
// catch all for react app
2017-10-22 23:33:10 +00:00
router.get('*', async ctx => {
2017-10-23 00:16:57 +00:00
await renderapp(ctx);
2017-10-22 23:33:10 +00:00
if (!ctx.status) ctx.throw(httpErrors.NotFound());
});
2016-04-29 05:25:37 +00:00
2017-10-23 00:16:57 +00:00
// middleware
2017-10-22 23:33:10 +00:00
koa.use(subdomainRedirect());
2016-04-29 05:25:37 +00:00
koa.use(router.routes());
koa.use(async () => {
throw httpErrors.NotFound();
});
2016-06-01 23:48:35 +00:00
export default koa;