This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
76 lines (62 loc) · 2.75 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
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const config = require('./config');
const cluster = require('cluster');
const numCPU = require('os').cpus().length;
if (cluster.isMaster && config.cluster_enabled) {
for (let i = 0; i < numCPU; i++) {
const worker = cluster.fork();
const pid = worker.process.pid;
console.log("[Cluster]", `worker ${pid} born`);
}
cluster.on('exit', function (deadWorker, code, signal) {
const worker = cluster.fork();
const newPID = worker.process.pid;
const oldPID = deadWorker.process.pid;
console.log("[Cluster]", `worker ${oldPID} died with code ${code} and signal ${signal}.`);
console.log("[Cluster]", `worker ${newPID} born.`);
});
} else {
const app = express();
let localDB;
MongoClient.connect(config.mongodb_url).then(db => {
console.log("[MongoDB]", "Connected to MongoDB");
localDB = db;
}, err => {
console.error("[MongoDB]", "Connection to MongoDB failed")
});
app.use(function (req, res, next) {
req.db = localDB;
next();
});
/*
* Touches
*/
const touchController = require('./controllers/touchController');
app.put("/touches", touchController.putTouch);
app.get("/touches/scenes", touchController.getScenes);
app.get("/touches/scene/:scene", (req, res, next) => touchController.getTouch(req, res, next, req.params["scene"]));
/*
* Images
*/
const imageController = require('./controllers/imageController');
app.get("/images/check/:scene", (req, res, next) => imageController.imageNeeded(req, res, next, req.params["scene"]));
app.post("/images/:scene", (req, res, next) => imageController.addImage(req, res, next, req.params["scene"]));
app.get("/images/:scene", (req, res, next) => imageController.getImage(req, res, next, req.params["scene"]));
/*
* Public
*/
app.use(express.static("./public/build/"));
app.use(function (req, res, next) {
const responseView = require('./views/response');
responseView.catch(req, res, next,
Promise.resolve().then(() => {
res.status(404);
throw responseView.create(null, null, "404 - Not found");
})
);
});
app.listen(config.app_listen_port, () => {
console.log("[App]", `App listening in ${config.app_listen_port}`);
});
}