2020-07-21 14:36:50 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
2020-07-09 06:19:08 +03:00
|
|
|
import * as Constants from "./node_common/constants";
|
2020-07-22 10:41:29 +03:00
|
|
|
import * as Data from "~/node_common/data";
|
2020-07-24 03:22:22 +03:00
|
|
|
import * as ViewerManager from "~/node_common/managers/viewer";
|
2020-07-22 10:41:29 +03:00
|
|
|
import * as Utilities from "~/node_common/utilities";
|
2020-07-24 03:22:22 +03:00
|
|
|
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";
|
2020-07-21 14:36:50 +03:00
|
|
|
import JWT from "jsonwebtoken";
|
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-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-02-19 09:30:47 +03:00
|
|
|
|
2020-07-16 08:48:51 +03:00
|
|
|
server.get("/application", async (req, res) => {
|
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-07-16 08:48:51 +03:00
|
|
|
return app.render(req, res, "/application", {
|
2020-07-17 13:24:20 +03:00
|
|
|
wsPort: null,
|
2020-07-21 14:36:50 +03:00
|
|
|
viewer,
|
2020-06-17 21:05:13 +03:00
|
|
|
});
|
2020-02-19 09:30:47 +03:00
|
|
|
});
|
|
|
|
|
2020-07-22 10:41:29 +03:00
|
|
|
server.get("/@:username", async (req, res) => {
|
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-07-27 12:50:25 +03:00
|
|
|
const slates = await Data.getSlatesByUserId({ userId: creator.id });
|
|
|
|
|
2020-07-22 10:41:29 +03:00
|
|
|
return app.render(req, res, "/profile", {
|
|
|
|
viewer,
|
2020-07-22 14:32:08 +03:00
|
|
|
creator:
|
|
|
|
creator && !creator.error
|
|
|
|
? {
|
|
|
|
username: creator.username,
|
|
|
|
data: { photo: creator.data.photo },
|
2020-07-27 12:50:25 +03:00
|
|
|
slates: JSON.parse(JSON.stringify(slates)),
|
2020-07-22 14:32:08 +03:00
|
|
|
}
|
|
|
|
: null,
|
2020-07-22 10:41:29 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-27 12:50:25 +03:00
|
|
|
server.get("/@:username/:slatename", async (req, res) => {
|
|
|
|
const slate = await Data.getSlateByName({
|
|
|
|
slatename: req.params.slatename,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (slate) {
|
|
|
|
slate.ownername = req.params.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.render(req, res, "/slate", {
|
|
|
|
slate: JSON.parse(JSON.stringify(slate)),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-17 13:24:20 +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) => {
|
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
|
|
|
});
|
|
|
|
});
|