-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (67 loc) · 1.94 KB
/
app.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
// requires the multiple libraries
const express = require("express");
const process = require("process");
const util = require("hive-js-util");
const info = require("./package");
const lib = require("./lib");
// builds the initial application object to be used
// by the application for serving
const app = express();
process.on("SIGINT", function() {
process.exit();
});
process.on("SIGTERM", function() {
process.exit();
});
process.on("exit", () => {
util.Logging.info("Exiting on user's request");
lib.destroy();
});
app.get("/", (req, res, next) => {
async function clojure() {
lib.verifyKey(req);
const engine = req.query.engine || "qrimage";
const engineModule = lib.ENGINES[engine];
const engineInstance = engineModule.singleton();
await engineInstance.render(req, res, next);
}
clojure().catch(next);
});
app.get("/info", (req, res, next) => {
res.json({
name: info.name,
version: info.version,
node: process.version
});
});
app.all("*", (req, res, next) => {
res.status(404);
res.json({ error: "Route not found", code: 404 });
});
app.use((err, req, res, next) => {
if (res.headersSent) {
return next(err);
}
const code = err.code || 500;
const result = { error: err.message, code: code };
if (process.env.NODE_ENV !== "production") {
result.stack = err.stack ? err.stack.split("\n") : [];
}
res.status(code);
res.json(result);
});
(async () => {
await lib.start();
try {
app.listen(lib.conf.PORT, lib.conf.HOST, () => {
try {
util.Logging.info("Listening on " + lib.conf.HOST + ":" + String(lib.conf.PORT));
lib.init();
} catch (err) {
util.Logging.error(err);
}
});
} finally {
await lib.stop();
}
})();