forked from Davenchy/live-torrent-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (69 loc) · 2.36 KB
/
server.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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const morgan = require("morgan");
const path = require("path");
const chalk = require("chalk");
const cluster = require("cluster");
const os = require("os");
const historyApi = require("connect-history-api-fallback");
const servers = require("./create-server");
const app = servers.app;
function main(disableMiddleWares = false, logs = true) {
// middle wares
if (!disableMiddleWares) {
app.use(cors());
if (logs) app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
}
// routes
app.use("/torrent", require("./routes/torrent"));
app.use("/search", require("./routes/search"));
app.use("/captions", require("./routes/captions"));
app.use("/yts", require("./routes/yts"));
app.use(historyApi());
app.use(express.static(path.resolve("docs", ".vuepress", "dist")));
}
// server listener
// @ts-ignore
if (!module.parent) {
if (cluster.isMaster) {
const cpus = parseInt(process.env.CLUSTERS) || os.cpus().length;
const { ENV, PORT, SSL_PORT } = servers;
for (let i = 0; i < cpus; i++) cluster.fork();
cluster.on("exit", () => cluster.fork());
console.log(
chalk`Main process is running on process {red ${process.pid}} in {blue ${ENV}} mode`
);
console.log(chalk`HTTP Server is running on port {green ${PORT}}`);
if (servers.isSSLSupported)
console.log(chalk`HTTPS Server is running on port {green ${SSL_PORT}}`);
console.log("");
} else {
main();
const { PORT, SSL_PORT } = servers;
servers.httpServer.listen(PORT, () =>
console.log(
chalk`{blue HTTP} server is running on process {red ${process.pid}}`
)
);
if (servers.isSSLSupported)
servers.httpsServer.listen(SSL_PORT, () => {
console.log(
chalk`{yellow HTTPS} server is running on process {red ${process.pid}}`
);
});
}
} else {
/**
* live torrent backend express.js middle ware
* @param {boolean} [disableMiddleWares=false] - disable the default middle wares used by the backend
* @param {boolean} [logs=true] - show logs in the console
* @return {object} express.js app object
*/
module.exports = (disableMiddleWares = false, logs = true) => {
main(!!disableMiddleWares, !!logs);
return servers;
};
}