-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
199 lines (157 loc) · 4.83 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"use strict";
// System libraries
const fs = require("fs");
// NPM libraries
const yargs = require("yargs");
const { Rpc, SessionManager, Webserver } = require("nicolai-jsonrpc");
const Configuration = require("nicolai-configuration");
// Project specific libraries
const Mqttclient = require("./lib/mqtt.js");
const Database = require("./lib/db.js");
// Project specific modules
const Files = require("./modules/files.js");
const Users = require("./modules/users.js");
const Persons = require("./modules/persons.js");
const Products = require("./modules/products.js");
const Invoices = require("./modules/invoices.js");
const Reports = require("./modules/reports.js");
// Project specific verification modules
const VerifyBalance = require("./verification/balance.js");
// Argument parser
const argv = yargs
.option("config", {
alias: "c",
description: "Configuration file path (use empty string to disable loading and only use CONFIG_... env variables)",
type: "string",
})
.help()
.alias("help", "h")
.argv;
var configFile = "configuration.json";
if (argv.config !== undefined) {
if (argv.config === "")
configFile = null
else
configFile = argv.config;
}
// Configuration
var configuration = new Configuration(configFile);
// Logfile
var logEnable = configuration.get("log", "enable");
var logFile = null;
if (logEnable) {
var logDir = configuration.get("log", "directory");
if (logDir === null) logDir = "log/";
logFile = fs.createWriteStream(logDir+(new Date()).getTime()+".txt");
}
else
{
//log to stdout instead
logFile = process.stdout;
}
// Error handlers
process.on("unhandledRejection", (err) => {
if (logFile) logFile.write("Unhandled rejection: "+err+"\n");
console.error("Unhandled rejection:", err);
process.exit(1);
});
process.on("uncaughtException", (err) => {
if (logFile) logFile.write("Uncaught exception: "+err+"\n");
console.error("Uncaught exception:", err);
process.exit(1);
});
process.on("SIGINT", () => {
if (logFile) logFile.write("Application interrupted.\n");
console.error("Application interrupted");
process.exit(0);
});
process.on("exit", (code) => {
if (logFile) logFile.write("Application terminated with code "+code+"\n");
if (logFile) logFile.end();
});
// Database
var database = new Database({
host: configuration.get("database", "host"),
user: configuration.get("database", "user"),
password: configuration.get("database", "password"),
database: configuration.get("database", "name"),
onConnect: start,
logFile: logFile
});
// Application elements not requiring database availability
var sessionManager = new SessionManager({
timeout: configuration.get("sessions","timeout"),
userSchema: {}
});
var rpc = new Rpc({
title: configuration.get("rpc","identity"),
version: ""
}, sessionManager);
if (configuration.get("rpc", "webserver", "enabled")) {
new Webserver({
port: configuration.get("rpc","webserver","port"),
host: configuration.get("rpc","webserver","listen"),
queue: configuration.get("rpc","webserver","queue"),
application: rpc,
});
}
var mqttclient = null;
if (configuration.get("mqtt", "enable")) {
mqttclient = new Mqttclient({
port: configuration.get("mqtt", "port"),
host: configuration.get("mqtt", "host"),
topic: configuration.get("mqtt", "topic"),
rpc: {
handle: async (request) => {
console.log("Something did a request over mqtt, which is disabled.", request);
return "RPC over MQTT is disabled!";
}
}
});
}
/* Application elements depending on database availability */
function start() {
var files = new Files({
database: database
});
files.registerRpcMethods(rpc);
var users = new Users({
database: database,
files: files
});
users.registerRpcMethods(rpc);
var products = new Products({
database: database,
files: files
});
products.registerRpcMethods(rpc);
var persons = new Persons({
database: database,
files: files,
products: products
});
persons.registerRpcMethods(rpc);
var invoices = new Invoices({
database: database,
persons: persons,
products: products,
mqtt: mqttclient,
mqtt_topic: "tkkrlab/spacecore/transaction"
});
invoices.registerRpcMethods(rpc);
var reports = new Reports({
database: database,
persons: persons,
products: products,
invoices: invoices
});
reports.registerRpcMethods(rpc);
var verifications = [];
verifications.push(new VerifyBalance({
persons: persons,
transactions: invoices
}));
for (var i in verifications) {
verifications[i].verify();
}
}