-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (49 loc) · 1.53 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
require("make-promises-safe");
require("dotenv").config();
// Require Node.js Dependencies
const { readFileSync, mkdirSync } = require("fs");
const { join } = require("path");
const { createServer } = require("https");
// Require Internal Dependencies
const httpServer = require("./src/httpServer");
const getSession = require("./src/session");
const { qWrap } = require("./src/utils");
// CONSTANTS
const SSL_DIR = join(process.cwd(), "ssl");
const PORT = process.env.api_http_port || 1337;
const ACTIVE_SSL = process.env.api_http_ssl === "true";
function httpStarted() {
const kw = ACTIVE_SSL ? "https" : "http";
console.log(`${kw} server started at: ${`${kw}://localhost:${PORT}`}`);
}
async function main() {
if (ACTIVE_SSL) {
try {
mkdirSync(SSL_DIR);
}
catch (err) {
// Skip
}
const options = {
key: readFileSync(join(SSL_DIR, "key.pem")),
cert: readFileSync(join(SSL_DIR, "cert.pem"))
};
createServer(options, httpServer.handler).listen(httpStarted);
}
else {
httpServer.listen(PORT, httpStarted);
}
const sess = await getSession();
const tOrders = sess.getTable("cmdb_order");
const [row = null] = await qWrap(
tOrders.select(["number"]).orderBy("number desc").limit(1)
);
if (row === null) {
throw new Error("Unable to found last number for orders!");
}
httpServer.use((req, res, next) => {
req.lastId = row;
next();
});
}
main().catch(console.error);