-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
71 lines (60 loc) · 2.53 KB
/
server.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
'use strict';
require('./processRequire.js');
const path = require('path');
const nopt = require('nopt');
const openVeoApi = require('@openveo/api');
const conf = process.require('app/server/conf.js');
const Server = process.require('app/server/Server.js');
const context = process.require('app/server/context.js');
const OpenVeoProvider = process.require('app/server/providers/OpenVeoProvider.js');
const OpenVeoStorage = process.require('app/server/storages/OpenVeoStorage.js');
const configurationDirectoryPath = path.join(openVeoApi.fileSystem.getConfDir(), 'portal');
const loggerConfPath = path.join(configurationDirectoryPath, 'loggerConf.json');
const serverConfPath = path.join(configurationDirectoryPath, 'serverConf.json');
const databaseConfPath = path.join(configurationDirectoryPath, 'databaseConf.json');
const webservicesConfPath = path.join(configurationDirectoryPath, 'webservicesConf.json');
const confPath = path.join(configurationDirectoryPath, 'conf.json');
// Process list of arguments
const knownProcessOptions = {
serverConf: [String, null],
databaseConf: [String, null],
loggerConf: [String, null],
conf: [String, null],
webservicesConf: [String, null]
};
// Parse process arguments
const processOptions = nopt(knownProcessOptions, null, process.argv);
// Load configuration files
try {
conf.loggerConf = require(processOptions.loggerConf || loggerConfPath);
conf.serverConf = require(processOptions.serverConf || serverConfPath);
conf.databaseConf = require(processOptions.databaseConf || databaseConfPath);
conf.webservicesConf = require(processOptions.webservicesConf || webservicesConfPath);
conf.conf = require(processOptions.conf || confPath);
conf.serverConf.webServiceBasePath = '/ws/';
} catch (error) {
throw new Error(`Invalid configuration file : ${error.message}`);
}
// Create logger
process.logger = openVeoApi.logger.add('portal', conf.loggerConf);
// Instanciate a Server
const server = new Server(conf.serverConf);
// Get a Database instance
const db = openVeoApi.storages.factory.get(conf.databaseConf.type, conf.databaseConf);
// Create OpenVeo provider
context.openVeoProvider = new OpenVeoProvider(new OpenVeoStorage(conf.webservicesConf));
// Establish connection to the database
db.connect((error) => {
if (error) {
process.logger.error(error && error.message);
throw new Error(error);
}
context.database = db;
server.onDatabaseAvailable(db, (error) => {
if (error) {
process.logger.error(error && error.message);
throw new Error(error);
}
server.start();
});
});