-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.js
More file actions
96 lines (84 loc) · 2.3 KB
/
service.js
File metadata and controls
96 lines (84 loc) · 2.3 KB
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
const { Cache, RedisStore, Events} = require("@drumee/server-essentials");
const { Session, Input, Output } = require("@drumee/server-core");
const { ERROR, START } = Events;
const configs = require("./configs");
const env = configs.env();
configs.load();
const HttpServer = require("http");
const Acl = require("./router/rest");
console.log(`Starting service server with verbosity = ${global.verbosity}`);
/**
*
* @param {*} request
* @param {*} response
*/
function handler (request, response) {
const input = new Input({ request, sourceName: "service" });
const output = new Output({ response });
let session = new Session({ input, output, env});
session.once(ERROR, function (e) {
console.error("SERVER_FAULT[47]", e);
if (session.exception)
session.exception.server("SESSION_FAILED");
session.stop();
});
session.once(START, function () {
try {
Acl.run(session);
} catch (e) {
console.error("Failed to run service", e);
if (session.exception)
session.exception.server("SERVICE_FAILED");
session.stop();
}
});
};
/**
*
*/
function fatalError(args) {
let { status, error, response } = args;
status = status || 500;
error = error || "SERVICE_RUNNER_ERROR";
const output = new Output({ response });
let data = {
error_code: status,
status,
error,
}
output.add_data(data);
output.flush();
}
let res = new RedisStore();
res
.init()
.then(async () => {
global.SharedRedisStore = RedisStore;
new Acl();
new Cache();
Cache.setEnv(env);
await Cache.load();
console.log("Cache loaded ", Cache.message("_domain_name"));
await Acl.loadModules(__dirname);
await Acl.loadPlugins();
const http = HttpServer.createServer((request, response) => {
try {
handler(request, response);
} catch (e) {
const error = "SERVICE_ERROR";
console.error(`ERR[95]:${error}`, e);
fatalError({ response, error })
}
});
http.listen(env.restPort);
})
.catch((e) => {
console.error("EEE:69 --- Failed to start Drumee server", e);
const error = "SERVER_PANIC";
console.error(`ERR[104]:${error}`, e);
fatalError({ response, error })
});
configs.handleSignals(async () => {
console.log("Reloading plugin");
await Acl.loadPlugins(true);
});