2017-10-23 00:16:57 +00:00
|
|
|
|
// @flow
|
2018-05-05 23:16:08 +00:00
|
|
|
|
import * as React from 'react';
|
2016-09-12 00:47:27 +00:00
|
|
|
|
import path from 'path';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
import Koa from 'koa';
|
|
|
|
|
import Router from 'koa-router';
|
|
|
|
|
import sendfile from 'koa-sendfile';
|
2017-10-29 22:02:24 +00:00
|
|
|
|
import serve from 'koa-static';
|
2018-11-04 03:27:57 +00:00
|
|
|
|
import parseDomain from 'parse-domain';
|
|
|
|
|
import apexRedirect from './middlewares/apexRedirect';
|
2017-10-23 00:16:57 +00:00
|
|
|
|
import renderpage from './utils/renderpage';
|
2018-11-18 20:07:11 +00:00
|
|
|
|
import { isCustomSubdomain } from '../shared/utils/domains';
|
2018-01-22 02:47:43 +00:00
|
|
|
|
import { robotsResponse } from './utils/robots';
|
2018-02-25 04:52:56 +00:00
|
|
|
|
import { NotFoundError } from './errors';
|
2018-11-04 03:27:57 +00:00
|
|
|
|
import { Team } from './models';
|
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';
|
2017-11-29 06:46:26 +00:00
|
|
|
|
import Changelog from './pages/Changelog';
|
2017-12-18 01:14:16 +00:00
|
|
|
|
import Privacy from './pages/Privacy';
|
2017-10-23 00:16:57 +00:00
|
|
|
|
import Pricing from './pages/Pricing';
|
2018-12-20 15:19:05 +00:00
|
|
|
|
import Integrations from './pages/Integrations';
|
|
|
|
|
import Integration from './pages/Integration';
|
2017-11-14 06:59:45 +00:00
|
|
|
|
import Api from './pages/Api';
|
2018-11-04 03:27:57 +00:00
|
|
|
|
import SubdomainSignin from './pages/SubdomainSignin';
|
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
|
|
|
|
};
|
|
|
|
|
|
2017-10-29 22:02:24 +00:00
|
|
|
|
// serve static assets
|
|
|
|
|
koa.use(serve(path.resolve(__dirname, '../public')));
|
|
|
|
|
|
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 />));
|
2017-11-14 06:59:45 +00:00
|
|
|
|
router.get('/developers', ctx => renderpage(ctx, <Api />));
|
2017-12-18 01:14:16 +00:00
|
|
|
|
router.get('/privacy', ctx => renderpage(ctx, <Privacy />));
|
2018-12-20 15:19:05 +00:00
|
|
|
|
router.get('/integrations/:slug', ctx =>
|
|
|
|
|
renderpage(ctx, <Integration slug={ctx.params.slug} />)
|
|
|
|
|
);
|
|
|
|
|
router.get('/integrations', ctx => renderpage(ctx, <Integrations />));
|
2017-11-29 06:46:26 +00:00
|
|
|
|
router.get('/changelog', async ctx => {
|
2018-05-13 02:56:44 +00:00
|
|
|
|
const data = await fetch(
|
|
|
|
|
'https://api.github.com/repos/outline/outline/releases'
|
|
|
|
|
);
|
|
|
|
|
const releases = await data.json();
|
|
|
|
|
return renderpage(ctx, <Changelog releases={releases} />);
|
2017-11-29 06:46:26 +00:00
|
|
|
|
});
|
2017-10-23 00:16:57 +00:00
|
|
|
|
|
|
|
|
|
// home page
|
2017-10-22 23:33:10 +00:00
|
|
|
|
router.get('/', async ctx => {
|
2018-05-29 04:14:43 +00:00
|
|
|
|
const lastSignedIn = ctx.cookies.get('lastSignedIn');
|
2018-11-13 07:17:50 +00:00
|
|
|
|
const accessToken = ctx.cookies.get('accessToken');
|
2018-11-10 07:40:33 +00:00
|
|
|
|
|
2018-11-13 06:45:51 +00:00
|
|
|
|
// Because we render both the signed in and signed out views depending
|
|
|
|
|
// on a cookie it's important that the browser does not render from cache.
|
2018-11-12 00:24:05 +00:00
|
|
|
|
ctx.set('Cache-Control', 'no-cache');
|
2018-05-28 18:36:37 +00:00
|
|
|
|
|
2018-11-13 06:45:51 +00:00
|
|
|
|
// If we have an accessToken we can just go ahead and render the app – if
|
|
|
|
|
// the accessToken turns out to be invalid the user will be redirected.
|
2018-05-28 18:36:37 +00:00
|
|
|
|
if (accessToken) {
|
2018-11-04 03:27:57 +00:00
|
|
|
|
return renderapp(ctx);
|
2017-10-22 23:33:10 +00:00
|
|
|
|
}
|
2018-11-04 03:27:57 +00:00
|
|
|
|
|
2018-11-13 06:45:51 +00:00
|
|
|
|
// If we're on a custom subdomain then we display a slightly different signed
|
|
|
|
|
// out view that includes the teams basic information.
|
2018-11-18 20:07:11 +00:00
|
|
|
|
if (
|
|
|
|
|
process.env.SUBDOMAINS_ENABLED === 'true' &&
|
|
|
|
|
isCustomSubdomain(ctx.request.hostname)
|
|
|
|
|
) {
|
|
|
|
|
const domain = parseDomain(ctx.request.hostname);
|
|
|
|
|
const subdomain = domain ? domain.subdomain : undefined;
|
2018-11-04 03:27:57 +00:00
|
|
|
|
const team = await Team.find({
|
|
|
|
|
where: { subdomain },
|
|
|
|
|
});
|
2018-11-18 20:07:11 +00:00
|
|
|
|
if (team) {
|
2018-11-04 03:27:57 +00:00
|
|
|
|
return renderpage(
|
|
|
|
|
ctx,
|
|
|
|
|
<SubdomainSignin
|
|
|
|
|
team={team}
|
|
|
|
|
notice={ctx.request.query.notice}
|
|
|
|
|
lastSignedIn={lastSignedIn}
|
|
|
|
|
googleSigninEnabled={!!process.env.GOOGLE_CLIENT_ID}
|
|
|
|
|
slackSigninEnabled={!!process.env.SLACK_KEY}
|
|
|
|
|
hostname={ctx.request.hostname}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-04 07:59:52 +00:00
|
|
|
|
ctx.redirect(`${process.env.URL}?notice=invalid-auth`);
|
2018-11-04 03:27:57 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 06:45:51 +00:00
|
|
|
|
// Otherwise, go ahead and render the homepage
|
2018-11-04 03:27:57 +00:00
|
|
|
|
return renderpage(
|
|
|
|
|
ctx,
|
|
|
|
|
<Home
|
|
|
|
|
notice={ctx.request.query.notice}
|
|
|
|
|
lastSignedIn={lastSignedIn}
|
|
|
|
|
googleSigninEnabled={!!process.env.GOOGLE_CLIENT_ID}
|
|
|
|
|
slackSigninEnabled={!!process.env.SLACK_KEY}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2017-10-22 23:33:10 +00:00
|
|
|
|
});
|
2017-10-11 07:14:45 +00:00
|
|
|
|
|
2018-01-22 02:47:43 +00:00
|
|
|
|
// Other
|
|
|
|
|
router.get('/robots.txt', ctx => (ctx.body = robotsResponse(ctx)));
|
|
|
|
|
|
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);
|
2018-02-25 04:52:56 +00:00
|
|
|
|
if (!ctx.status) ctx.throw(new NotFoundError());
|
2017-10-22 23:33:10 +00:00
|
|
|
|
});
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
2017-10-23 00:16:57 +00:00
|
|
|
|
// middleware
|
2018-11-04 03:27:57 +00:00
|
|
|
|
koa.use(apexRedirect());
|
2016-04-29 05:25:37 +00:00
|
|
|
|
koa.use(router.routes());
|
|
|
|
|
|
2016-06-01 23:48:35 +00:00
|
|
|
|
export default koa;
|