slate/server.js

137 lines
3.1 KiB
JavaScript
Raw Normal View History

import * as Environment from "~/node_common/environment";
2020-07-09 06:19:08 +03:00
import * as Constants from "./node_common/constants";
2020-08-07 02:28:54 +03:00
import * as Validations from "~/common/validations";
import * as Data from "~/node_common/data";
import * as ViewerManager from "~/node_common/managers/viewer";
import * as Utilities from "~/node_common/utilities";
import * as Strings from "./common/strings";
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 JWT from "jsonwebtoken";
2020-07-21 21:01:12 +03:00
const app = next({
dev: !Environment.IS_PRODUCTION,
dir: __dirname,
quiet: false,
});
const handler = app.getRequestHandler();
app.prepare().then(async () => {
2020-02-19 09:30:47 +03:00
const server = express();
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-02-19 09:30:47 +03:00
server.get("/_", async (req, res) => {
const id = Utilities.getIdFromCookie(req);
let viewer = null;
if (id) {
viewer = await ViewerManager.getById({
id,
});
}
return app.render(req, res, "/_", {
viewer,
2020-06-17 21:05:13 +03:00
});
2020-02-19 09:30:47 +03:00
});
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: {
username: creator.username,
data: { photo: creator.data.photo },
slates: JSON.parse(JSON.stringify(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,
});
if (!slate) {
return res.redirect("/404");
2020-07-27 12:50:25 +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) {
return res.redirect("/403");
}
return app.render(req, res, "/_/slate", {
2020-08-06 23:59:06 +03:00
slate: JSON.parse(
JSON.stringify({ ...slate, ownername: req.params.username })
),
2020-07-27 12:50:25 +03:00
});
});
server.all("*", async (req, res) => {
return handler(req, res, req.url);
2020-02-19 09:30:47 +03:00
});
2020-07-21 22:53:49 +03:00
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
});
});