slate/server.js

287 lines
6.5 KiB
JavaScript
Raw Normal View History

import * as Environment from "~/node_common/environment";
2020-08-07 02:28:54 +03:00
import * as Validations from "~/common/validations";
import * as Data from "~/node_common/data";
import * as Utilities from "~/node_common/utilities";
import * as Serializers from "~/node_common/serializers";
import * as ViewerManager from "~/node_common/managers/viewer";
import * as AnalyticsManager from "~/node_common/managers/analytics";
2020-06-17 21:05:13 +03:00
2020-07-09 06:19:08 +03:00
import express from "express";
import next from "next";
import compression from "compression";
import cors from "cors";
2020-07-21 21:01:12 +03:00
const app = next({
dev: !Environment.IS_PRODUCTION,
dir: __dirname,
quiet: false,
});
2020-10-05 05:17:08 +03:00
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const handler = app.getRequestHandler();
app.prepare().then(async () => {
2020-02-19 09:30:47 +03:00
const server = express();
2020-08-18 01:11:08 +03:00
server.use(cors());
if (Environment.IS_PRODUCTION) {
2020-02-19 09:30:47 +03:00
server.use(compression());
}
server.keepAliveTimeout = 15 * 60 * 1000;
server.headersTimeout = 16 * 60 * 1000;
2020-07-09 06:19:08 +03:00
server.use("/public", express.static("public"));
2020-10-05 05:17:08 +03:00
server.get("/please-dont-use-timeout", async (r, s) => {
console.log("[ forbidden ] someone is using your testing timeout");
await sleep(15 * 60 * 1000);
return res
.status(200)
.send({ decorator: "15_MINUTE_TEST", data: { success: true } });
});
server.get("/system", async (r, s) => s.redirect("/_/system"));
server.get("/experiences", async (r, s) => s.redirect("/_/system"));
server.get("/_/experiences", async (r, s) => s.redirect("/_/system"));
server.get("/system/:c", async (r, s) =>
s.redirect(`/_/system/${r.params.c}`)
);
server.get("/experiences/:m", async (r, s) =>
s.redirect(`/_/experiences/${r.params.m}`)
);
server.all("/api/:a", async (r, s, next) => {
r.setTimeout(15 * 60 * 1000, function() {
console.log("REQUEST TIMED OUT");
});
s.setTimeout(15 * 60 * 1000, function() {
console.log("RESPONSE TIMED OUT");
s.send(408);
});
return handler(r, s, r.url);
});
server.all("/api/:a/:b", async (r, s, next) => {
r.setTimeout(15 * 60 * 1000, function() {
console.log("REQUEST TIMED OUT");
});
s.setTimeout(15 * 60 * 1000, function() {
console.log("RESPONSE TIMED OUT");
s.send(408);
});
return handler(r, s, r.url);
});
2020-09-14 10:28:09 +03:00
server.get("/", async (req, res) => {
return app.render(req, res, "/", {});
});
server.get("/_", async (req, res) => {
const isBucketsAvailable = await Utilities.checkTextile();
2020-09-17 23:00:07 +03:00
if (!isBucketsAvailable) {
return res.redirect("/maintenance");
}
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
let analytics = await AnalyticsManager.get();
return app.render(req, res, "/_", {
viewer,
analytics,
2020-06-17 21:05:13 +03:00
});
2020-02-19 09:30:47 +03:00
});
server.get("/_/integration-page", async (req, res) => {
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
return app.render(req, res, "/_/integration-page", {
viewer,
});
});
server.all("/_/:a", async (r, s) => handler(r, s, r.url));
server.all("/_/:a/:b", async (r, s) => handler(r, s, r.url));
server.get("/:username", async (req, res) => {
2020-08-07 02:28:54 +03:00
// TODO(jim): Temporary workaround
if (!Validations.userRoute(req.params.username)) {
2020-08-07 02:28:54 +03:00
return handler(req, res, req.url);
}
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
const creator = await Data.getUserByUsername({
username: req.params.username,
});
if (!creator) {
return res.redirect("/404");
}
if (creator.error) {
return res.redirect("/404");
}
2020-08-06 23:59:06 +03:00
const slates = await Data.getSlatesByUserId({
userId: creator.id,
publicOnly: true,
});
2020-07-27 12:50:25 +03:00
return app.render(req, res, "/_/profile", {
viewer,
creator: Serializers.user({ ...creator, slates }),
});
});
server.get("/:username/:slatename", async (req, res) => {
2020-08-07 02:28:54 +03:00
// TODO(jim): Temporary workaround
if (!Validations.userRoute(req.params.username)) {
2020-08-07 02:28:54 +03:00
return handler(req, res, req.url);
}
2020-07-27 12:50:25 +03:00
const slate = await Data.getSlateByName({
slatename: req.params.slatename,
2020-09-29 07:50:11 +03:00
username: req.params.username,
2020-07-27 12:50:25 +03:00
});
if (!slate) {
return res.redirect("/404");
2020-07-27 12:50:25 +03:00
}
if (slate.error) {
return res.redirect("/404");
}
if (!slate.data.public) {
return res.redirect("/403");
}
const creator = await Data.getUserById({ id: slate.data.ownerId });
if (!creator) {
return res.redirect("/404");
}
if (creator.error) {
return res.redirect("/404");
}
if (req.params.username !== creator.username) {
return res.redirect("/403");
}
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
return app.render(req, res, "/_/slate", {
viewer,
creator: Serializers.user(creator),
slate,
});
});
server.get("/:username/:slatename/cid::cid", async (req, res) => {
// TODO(jim): Temporary workaround
if (!Validations.userRoute(req.params.username)) {
return handler(req, res, req.url);
}
const slate = await Data.getSlateByName({
slatename: req.params.slatename,
2020-09-29 07:50:11 +03:00
username: req.params.username,
});
if (!slate) {
return res.redirect("/404");
}
if (slate.error) {
return res.redirect("/404");
}
if (!slate.data.public) {
2020-08-06 23:59:06 +03:00
return res.redirect("/403");
}
const creator = await Data.getUserById({ id: slate.data.ownerId });
if (!creator) {
return res.redirect("/404");
}
if (creator.error) {
return res.redirect("/404");
}
if (req.params.username !== creator.username) {
return res.redirect("/403");
}
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
return app.render(req, res, "/_/slate", {
viewer,
creator: Serializers.user(creator),
slate,
cid: req.params.cid,
2020-07-27 12:50:25 +03:00
});
});
server.all("*", async (r, s) => handler(r, s, r.url));
2020-02-19 09:30:47 +03:00
const listenServer = server.listen(Environment.PORT, async (e) => {
if (e) throw e;
2020-07-21 22:53:49 +03:00
console.log(`[ slate ] client: http://localhost:${Environment.PORT}`);
2020-02-19 09:30:47 +03:00
});
listenServer.setTimeout(15 * 60 * 1000);
2020-02-19 09:30:47 +03:00
});