2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-08-07 02:28:54 +03:00
|
|
|
import * as Validations from "~/common/validations";
|
2020-07-22 10:41:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-08-30 22:10:01 +03:00
|
|
|
import * as Serializers from "~/node_common/serializers";
|
2020-08-14 12:17:19 +03:00
|
|
|
|
|
|
|
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";
|
2020-08-18 01:07:31 +03:00
|
|
|
import cors from "cors";
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-21 21:01:12 +03:00
|
|
|
const app = next({
|
|
|
|
dev: !Environment.IS_PRODUCTION,
|
|
|
|
dir: __dirname,
|
|
|
|
quiet: false,
|
|
|
|
});
|
2020-07-22 22:17:08 +03:00
|
|
|
|
2020-07-17 13:24:20 +03:00
|
|
|
const handler = app.getRequestHandler();
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-04-09 00:29:13 +03:00
|
|
|
app.prepare().then(async () => {
|
2020-02-19 09:30:47 +03:00
|
|
|
const server = express();
|
2020-07-22 21:34:24 +03:00
|
|
|
|
2020-08-18 01:11:08 +03:00
|
|
|
server.use(cors());
|
2020-08-18 01:07:31 +03:00
|
|
|
|
2020-07-23 03:07:54 +03:00
|
|
|
if (Environment.IS_PRODUCTION) {
|
2020-02-19 09:30:47 +03:00
|
|
|
server.use(compression());
|
|
|
|
}
|
|
|
|
|
2020-07-09 06:19:08 +03:00
|
|
|
server.use("/public", express.static("public"));
|
2020-08-30 22:10:01 +03:00
|
|
|
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"));
|
2020-09-03 00:04:47 +03:00
|
|
|
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}`)
|
|
|
|
);
|
2020-08-30 22:10:01 +03:00
|
|
|
server.all("/api/:a", async (r, s) => handler(r, s, r.url));
|
|
|
|
server.all("/api/:a/:b", async (r, s) => handler(r, s, r.url));
|
2020-08-20 08:57:46 +03:00
|
|
|
|
|
|
|
server.get("/_", async (req, res) => {
|
|
|
|
const isBucketsAvailable = await Utilities.checkTextile();
|
2020-08-19 21:44:18 +03:00
|
|
|
|
|
|
|
// TODO(jim): Do something more robust here.
|
2020-08-20 08:57:46 +03:00
|
|
|
if (!isBucketsAvailable) {
|
2020-08-19 21:44:18 +03:00
|
|
|
return res.redirect("/maintenance");
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-07-21 14:36:50 +03:00
|
|
|
|
2020-07-22 10:41:29 +03:00
|
|
|
let viewer = null;
|
2020-07-22 13:51:40 +03:00
|
|
|
if (id) {
|
2020-07-24 03:22:22 +03:00
|
|
|
viewer = await ViewerManager.getById({
|
2020-07-22 13:51:40 +03:00
|
|
|
id,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
2020-07-21 14:36:50 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 12:17:19 +03:00
|
|
|
let analytics = await AnalyticsManager.get();
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_", {
|
2020-07-21 14:36:50 +03:00
|
|
|
viewer,
|
2020-08-14 12:17:19 +03:00
|
|
|
analytics,
|
2020-06-17 21:05:13 +03:00
|
|
|
});
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-08-26 03:44:45 +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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-30 22:10:01 +03:00
|
|
|
server.all("/_/:a", async (r, s) => handler(r, s, r.url));
|
|
|
|
server.all("/_/:a/:b", async (r, s) => handler(r, s, r.url));
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
server.get("/:username", async (req, res) => {
|
2020-08-07 02:28:54 +03:00
|
|
|
// TODO(jim): Temporary workaround
|
2020-08-07 02:36:03 +03:00
|
|
|
if (!Validations.userRoute(req.params.username)) {
|
2020-08-07 02:28:54 +03:00
|
|
|
return handler(req, res, req.url);
|
|
|
|
}
|
|
|
|
|
2020-07-22 13:51:40 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
2020-07-22 10:41:29 +03:00
|
|
|
|
|
|
|
let viewer = null;
|
2020-07-22 13:51:40 +03:00
|
|
|
if (id) {
|
2020-07-24 03:22:22 +03:00
|
|
|
viewer = await ViewerManager.getById({
|
2020-07-22 13:51:40 +03:00
|
|
|
id,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const creator = await Data.getUserByUsername({
|
|
|
|
username: req.params.username,
|
|
|
|
});
|
|
|
|
|
2020-08-02 01:46:54 +03:00
|
|
|
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
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_/profile", {
|
2020-07-22 10:41:29 +03:00
|
|
|
viewer,
|
2020-08-30 22:10:01 +03:00
|
|
|
creator: Serializers.user({ ...creator, slates }),
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
server.get("/:username/:slatename", async (req, res) => {
|
2020-08-07 02:28:54 +03:00
|
|
|
// TODO(jim): Temporary workaround
|
2020-08-07 02:36:03 +03:00
|
|
|
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-08-02 01:46:54 +03:00
|
|
|
if (!slate) {
|
|
|
|
return res.redirect("/404");
|
2020-07-27 12:50:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-03 02:29:11 +03:00
|
|
|
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) {
|
2020-08-03 02:29:11 +03:00
|
|
|
return res.redirect("/403");
|
|
|
|
}
|
|
|
|
|
2020-08-25 09:46:02 +03:00
|
|
|
const id = Utilities.getIdFromCookie(req);
|
|
|
|
|
|
|
|
let viewer = null;
|
|
|
|
if (id) {
|
|
|
|
viewer = await ViewerManager.getById({
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:06:54 +03:00
|
|
|
return app.render(req, res, "/_/slate", {
|
2020-08-25 09:46:02 +03:00
|
|
|
viewer,
|
2020-08-30 22:10:01 +03:00
|
|
|
creator: Serializers.user(creator),
|
2020-08-25 09:46:02 +03:00
|
|
|
slate,
|
2020-07-27 12:50:25 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-08-30 22:10:01 +03:00
|
|
|
server.all("*", async (r, s) => handler(r, s, r.url));
|
2020-02-19 09:30:47 +03:00
|
|
|
|
2020-07-21 22:53:49 +03:00
|
|
|
server.listen(Environment.PORT, async (e) => {
|
2020-07-17 13:24:20 +03:00
|
|
|
if (e) throw e;
|
2020-06-09 21:00:36 +03:00
|
|
|
|
2020-07-21 22:53:49 +03:00
|
|
|
console.log(`[ slate ] client: http://localhost:${Environment.PORT}`);
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
});
|