-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (122 loc) · 3.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import morgan from "morgan";
import compression from "compression";
import * as ip from "neoip";
import * as fs from "node:fs";
import * as path from "path";
import express from "express";
import paginate from "express-paginate";
import actuator from "express-actuator";
import minifyHTML from "express-minify-html-2";
import {minify as minifyJS} from "uglify-js";
import bodyParser from "body-parser";
import {config} from "dotenv";
import {router} from "express-file-routing";
import {fileURLToPath} from "url";
import {parse as parseUserAgent} from "useragent";
import {createStream as createRotatingFileStream} from "rotating-file-stream";
import {Server as IO} from "socket.io";
import HTTPStatus from "./util/http-status.js";
config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __logs = path.join(__dirname, "logs");
if (!fs.existsSync(__logs)) fs.mkdirSync(__logs, {recursive: true});
const nodeEnv = process.env.NODE_ENV || "development";
const port = process.env.PORT || 3000;
const app = express();
app.use(morgan(nodeEnv === "development" ? "dev" : "common"));
app.use(morgan(
"combined",
{
stream: createRotatingFileStream(
(time, i) => time ? `server.${time.toISOString().split("T")[0]}.${i}.log.gz` : "server.log",
{
path: __logs,
size: "100M",
interval: "1d",
compress: "gzip",
},
),
skip: (req, _) => {
switch (req.connection.remoteAddress) {
case "::1":
case "::ffff:127.0.0.1":
return true;
default:
return false;
}
},
}
));
app.set("views", "./views");
app.set("view engine", "ejs");
app.use("/", express.static(path.join(__dirname, "public")));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(paginate.middleware(10, 50));
app.use((req, res, next) => {
res.locals.path = req.baseUrl + req.path;
const user_agent = req.headers["user-agent"];
res.locals.os = parseUserAgent(user_agent).os.family;
next();
});
app.use(
minifyHTML({
override: true,
exceptionUrls: false,
htmlMinifier: {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true,
}
})
);
app.use((req, res, next) => {
const originalSend = res.send;
res.send = function (body) {
if (typeof body === "string") {
const minified = body.replace(
/<script>([\s\S]*?)<\/script>/gi,
(match, content) => `<script>${(minifyJS(content).code)}</script>`
);
originalSend.call(this, minified);
} else {
originalSend.call(this, body);
}
};
next();
});
app.use(compression());
app.use(actuator({ basePath: "/actuator" }));
app.use("/", await router());
app.use(async (err, _req, res, _next) => {
const message = nodeEnv === "production"
? err.message
: err.stack.split("\n")
.map(line => line.trimStart())
.join("\n");
res.status(HTTPStatus.INTERNAL_SERVER_ERROR).render("error", { message });
});
const server = app.listen(port, () => {
if (nodeEnv === "development") {
console.debug(`
App listening on:
* http://localhost:${port}
* http://${ip.address()}:${port}
`);
}
});
const io = new IO(server);
io.on("connection", (socket) => {
socket.on("label_added", (label) => {
socket.broadcast.emit("label_append", label);
});
socket.on("label_removed", (label) => {
socket.broadcast.emit("label_detach", label);
});
socket.on("label_renamed", (data) => {
socket.broadcast.emit("label_replace", data);
});
});