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 Strings from "./common/strings";
|
|
|
|
import * as Constants from "./node_common/constants";
|
2020-07-21 14:36:50 +03:00
|
|
|
import * as Models from "./node_common/models";
|
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-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-21 21:01:12 +03:00
|
|
|
if (Environment.IS_PRODUCTION_WEB) {
|
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-21 14:36:50 +03:00
|
|
|
let viewer = null;
|
|
|
|
|
|
|
|
if (!Strings.isEmpty(req.headers.cookie)) {
|
|
|
|
const token = req.headers.cookie.replace(
|
|
|
|
/(?:(?:^|.*;\s*)WEB_SERVICE_SESSION_KEY\s*\=\s*([^;]*).*$)|^.*$/,
|
|
|
|
"$1"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!Strings.isEmpty(token)) {
|
|
|
|
try {
|
|
|
|
const decoded = JWT.verify(token, Environment.JWT_SECRET);
|
|
|
|
|
|
|
|
if (decoded.username) {
|
|
|
|
viewer = await Models.getViewer({ username: decoded.username });
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 21:01:12 +03:00
|
|
|
production: Environment.IS_PRODUCTION_WEB,
|
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-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
|
|
|
});
|
|
|
|
});
|