forked from iedon/iedon-net-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (73 loc) · 2.93 KB
/
Copy pathapp.js
File metadata and controls
91 lines (73 loc) · 2.93 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
/**
* ===========================
* IEDON-PEERAPI
* Bootstrap
* ===========================
*/
import pkg from './package.json' assert { type: 'json' };
import localSettings from './config.js';
import { useLogger } from './providers/logger/logger.js';
import { useMail } from './providers/mail/mail.js';
import { useWhois } from './providers/whois/whois.js';
import { useFetch } from './providers/fetch/fetch.js';
import { useDbContext } from './db/dbContext.js';
import { useCore } from './providers/core/core.js';
import { useRouter } from './routes.js';
import Koa from 'koa'
import KoaBodyParser from 'koa-bodyparser'
const app = new Koa();
app.settings = localSettings;
app.use(KoaBodyParser());
(async () => {
await useLogger(app, localSettings.loggerSettings);
app.logger.getLogger('app').info(`${pkg.name}/${pkg.version} started.`);
if (localSettings.acorle.enabled) await initAcorle(app);
useDbContext(app, app.settings.dbSettings);
useMail(app, app.settings.mailSettings);
useWhois(app, app.settings.whoisSettings);
useFetch(app, app.settings.fetchSettings);
useCore(app, app.settings.tokenSettings);
useRouter(app);
})();
const initAcorle = async (app) => {
const appLogger = app.logger.getLogger('app');
const acorleLogger = app.logger.getLogger('acorle');
const Acorle = await import('./acorle-sdk/acorleKoa.js');
app.use(Acorle.acorleKoa(app,
localSettings.acorle.centerServerUrl,
localSettings.acorle.zone,
localSettings.acorle.secret,
localSettings.acorle.regIntervalSeconds,
(level, log) => acorleLogger[level](log)
));
if (localSettings.acorle.retriveConfigFromCenterServer) {
appLogger.info(`[acorle.retriveConfigFromCenterServer] is ON. Retriving configuration with key \"${localSettings.acorle.configKey}\" from center server...`);
const config = await app.acorle.getConfig(localSettings.acorle.configKey);
if (config && config.context) {
try {
const newSettings = {
listenPort: localSettings.listenPort,
loggerSettings: localSettings.loggerSettings,
acorle: localSettings.acorle,
handlers: localSettings.handlers
};
Object.assign(newSettings, JSON.parse(config.context));
app.settings = newSettings;
} catch (error) {
appLogger.error('Failed to parse / malformed configuration retrived from center server. Continuing with local file.');
}
} else {
appLogger.error('Failed to retrive configuration from center server. Continuing with local file.');
}
}
if (localSettings.acorle.enabled) {
// Register as microservice to acorle
app.acorle.registerServices([
new Acorle.AcorleService(
localSettings.acorle.serviceKey,
localSettings.acorle.localUrl,
localSettings.acorle.serviceName, false)
]);
}
}
export default app;