slate/server.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

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";
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";
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();
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) => {
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", {
wsPort: null,
2020-07-21 21:01:12 +03:00
production: Environment.IS_PRODUCTION_WEB,
viewer,
2020-06-17 21:05:13 +03:00
});
2020-02-19 09:30:47 +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
});
});