2017-10-23 00:16:57 +00:00
|
|
|
// @flow
|
2020-08-09 05:53:59 +00:00
|
|
|
import fs from "fs";
|
2020-06-20 20:59:15 +00:00
|
|
|
import path from "path";
|
2020-08-09 05:53:59 +00:00
|
|
|
import util from "util";
|
2020-06-20 20:59:15 +00:00
|
|
|
import Koa from "koa";
|
|
|
|
import Router from "koa-router";
|
|
|
|
import sendfile from "koa-sendfile";
|
|
|
|
import serve from "koa-static";
|
2021-06-10 00:41:39 +00:00
|
|
|
import isUUID from "validator/lib/isUUID";
|
2020-11-30 04:04:58 +00:00
|
|
|
import { languages } from "../shared/i18n";
|
2021-02-02 05:13:44 +00:00
|
|
|
import env from "./env";
|
2020-06-20 20:59:15 +00:00
|
|
|
import apexRedirect from "./middlewares/apexRedirect";
|
2021-06-10 00:41:39 +00:00
|
|
|
import Share from "./models/Share";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { opensearchResponse } from "./utils/opensearch";
|
2021-01-18 23:48:46 +00:00
|
|
|
import prefetchTags from "./utils/prefetchTags";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { robotsResponse } from "./utils/robots";
|
2020-06-20 20:59:15 +00:00
|
|
|
|
|
|
|
const isProduction = process.env.NODE_ENV === "production";
|
2021-06-10 00:41:39 +00:00
|
|
|
const isTest = process.env.NODE_ENV === "test";
|
2016-04-29 05:25:37 +00:00
|
|
|
const koa = new Koa();
|
|
|
|
const router = new Router();
|
2020-07-18 18:02:40 +00:00
|
|
|
const readFile = util.promisify(fs.readFile);
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
const readIndexFile = async (ctx) => {
|
2020-07-18 18:02:40 +00:00
|
|
|
if (isProduction) {
|
2020-08-24 03:35:59 +00:00
|
|
|
return readFile(path.join(__dirname, "../app/index.html"));
|
2020-07-18 18:02:40 +00:00
|
|
|
}
|
2021-06-10 00:41:39 +00:00
|
|
|
if (isTest) {
|
|
|
|
return readFile(path.join(__dirname, "/static/index.html"));
|
|
|
|
}
|
2020-07-18 18:02:40 +00:00
|
|
|
|
|
|
|
const middleware = ctx.devMiddleware;
|
2020-08-09 01:53:11 +00:00
|
|
|
await new Promise((resolve) => middleware.waitUntilValid(resolve));
|
2020-07-18 18:02:40 +00:00
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
middleware.fileSystem.readFile(
|
|
|
|
`${ctx.webpackConfig.output.path}/index.html`,
|
|
|
|
(err, result) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2021-06-10 00:41:39 +00:00
|
|
|
const renderApp = async (ctx, next, title = "Outline") => {
|
2020-09-24 02:26:18 +00:00
|
|
|
if (ctx.request.path === "/realtime/") {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const page = await readIndexFile(ctx);
|
2021-02-02 05:13:44 +00:00
|
|
|
const environment = `
|
|
|
|
window.env = ${JSON.stringify(env)};
|
2020-09-24 02:26:18 +00:00
|
|
|
`;
|
|
|
|
ctx.body = page
|
|
|
|
.toString()
|
2021-02-02 05:13:44 +00:00
|
|
|
.replace(/\/\/inject-env\/\//g, environment)
|
2021-06-10 00:41:39 +00:00
|
|
|
.replace(/\/\/inject-title\/\//g, title)
|
2021-01-18 23:48:46 +00:00
|
|
|
.replace(/\/\/inject-prefetch\/\//g, prefetchTags)
|
2020-09-24 02:26:18 +00:00
|
|
|
.replace(/\/\/inject-slack-app-id\/\//g, process.env.SLACK_APP_ID || "");
|
|
|
|
};
|
|
|
|
|
2021-06-10 00:41:39 +00:00
|
|
|
const renderShare = async (ctx, next) => {
|
|
|
|
const { shareId } = ctx.params;
|
|
|
|
|
|
|
|
// Find the share record if publicly published so that the document title
|
|
|
|
// can be be returned in the server-rendered HTML. This allows it to appear in
|
|
|
|
// unfurls with more reliablity
|
|
|
|
let share;
|
|
|
|
|
|
|
|
if (isUUID(shareId)) {
|
|
|
|
share = await Share.findOne({
|
|
|
|
where: {
|
|
|
|
id: shareId,
|
|
|
|
published: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow shares to be embedded in iframes on other websites
|
|
|
|
ctx.remove("X-Frame-Options");
|
|
|
|
|
|
|
|
return renderApp(ctx, next, share ? share.document.title : undefined);
|
|
|
|
};
|
|
|
|
|
2017-10-29 22:02:24 +00:00
|
|
|
// serve static assets
|
2018-12-21 04:00:58 +00:00
|
|
|
koa.use(
|
2020-08-24 03:44:44 +00:00
|
|
|
serve(path.resolve(__dirname, "../../public"), {
|
2018-12-21 04:00:58 +00:00
|
|
|
maxage: 60 * 60 * 24 * 30 * 1000,
|
|
|
|
})
|
|
|
|
);
|
2017-10-29 22:02:24 +00:00
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.get("/_health", (ctx) => (ctx.body = "OK"));
|
2016-09-05 21:26:06 +00:00
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
if (process.env.NODE_ENV === "production") {
|
2020-08-09 01:53:11 +00:00
|
|
|
router.get("/static/*", async (ctx) => {
|
2016-06-01 23:48:35 +00:00
|
|
|
ctx.set({
|
2021-02-15 23:19:51 +00:00
|
|
|
"Service-Worker-Allowed": "/",
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
2020-06-20 20:59:15 +00:00
|
|
|
"Cache-Control": `max-age=${356 * 24 * 60 * 60}`,
|
2016-06-01 23:48:35 +00:00
|
|
|
});
|
|
|
|
|
2020-08-24 03:35:59 +00:00
|
|
|
await sendfile(ctx, path.join(__dirname, "../app/", 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
|
|
|
|
2020-11-30 04:04:58 +00:00
|
|
|
router.get("/locales/:lng.json", async (ctx) => {
|
|
|
|
let { lng } = ctx.params;
|
|
|
|
|
|
|
|
if (!languages.includes(lng)) {
|
2020-12-20 19:53:09 +00:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
2020-11-30 04:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
|
ctx.set({
|
|
|
|
"Cache-Control": `max-age=${7 * 24 * 60 * 60}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await sendfile(
|
|
|
|
ctx,
|
|
|
|
path.join(__dirname, "../shared/i18n/locales", lng, "translation.json")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.get("/robots.txt", (ctx) => {
|
2019-08-09 02:52:29 +00:00
|
|
|
ctx.body = robotsResponse(ctx);
|
|
|
|
});
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
router.get("/opensearch.xml", (ctx) => {
|
2020-06-20 20:59:15 +00:00
|
|
|
ctx.type = "text/xml";
|
2019-08-09 02:52:29 +00:00
|
|
|
ctx.body = opensearchResponse();
|
|
|
|
});
|
2018-01-22 02:47:43 +00:00
|
|
|
|
2021-06-10 00:41:39 +00:00
|
|
|
router.get("/share/:shareId", renderShare);
|
|
|
|
router.get("/share/:shareId/*", renderShare);
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2020-09-24 02:26:18 +00:00
|
|
|
// catch all for application
|
|
|
|
router.get("*", renderApp);
|
|
|
|
|
2021-02-02 05:13:44 +00:00
|
|
|
// In order to report all possible performance metrics to Sentry this header
|
|
|
|
// must be provided when serving the application, see:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin
|
|
|
|
const timingOrigins = [env.URL];
|
|
|
|
if (env.SENTRY_DSN) {
|
|
|
|
timingOrigins.push("https://sentry.io");
|
|
|
|
}
|
|
|
|
|
|
|
|
koa.use(async (ctx, next) => {
|
|
|
|
ctx.set("Timing-Allow-Origin", timingOrigins.join(", "));
|
|
|
|
await next();
|
|
|
|
});
|
|
|
|
|
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;
|