-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
217 lines (182 loc) · 6.24 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
require('./processRequire.js');
var path = require('path');
var async = require('async');
var nopt = require('nopt');
var openVeoApi = require('@openveo/api');
var CorePlugin = process.require('app/server/plugin/CorePlugin.js');
var storage = process.require('app/server/storage.js');
var supportedContentLanguages = process.require('supportedContentLanguages.json');
var configurationDirectoryPath = path.join(openVeoApi.fileSystem.getConfDir(), 'core');
var coreConfPath = path.join(configurationDirectoryPath, 'conf.json');
var loggerConfPath = path.join(configurationDirectoryPath, 'loggerConf.json');
var serverConfPath = path.join(configurationDirectoryPath, 'serverConf.json');
var databaseConfPath = path.join(configurationDirectoryPath, 'databaseConf.json');
var loggerConf;
var serverConf;
var databaseConf;
var coreConf;
var corePlugin;
// Process arguments
var knownProcessOptions = {
ws: [Boolean],
conf: [String, null],
serverConf: [String, null],
databaseConf: [String, null],
loggerConf: [String, null]
};
// Parse process arguments
var processOptions = nopt(knownProcessOptions, null, process.argv);
// Load configuration files
try {
coreConf = require(processOptions.conf || coreConfPath);
loggerConf = require(processOptions.loggerConf || loggerConfPath);
serverConf = require(processOptions.serverConf || serverConfPath);
databaseConf = require(processOptions.databaseConf || databaseConfPath);
} catch (error) {
throw new Error('Invalid configuration file : ' + error.message);
}
var server;
if (processOptions['ws']) {
process.isWebService = true;
process.logger = openVeoApi.logger.add('openveo', loggerConf.ws);
var WebServiceServer = process.require('app/server/servers/WebServiceServer.js');
server = new WebServiceServer(serverConf.ws);
} else {
process.logger = openVeoApi.logger.add('openveo', loggerConf.app);
var ApplicationServer = process.require('app/server/servers/ApplicationServer.js');
server = new ApplicationServer(serverConf.app);
}
// Loaders
var pluginLoader = process.require('app/server/loaders/pluginLoader.js');
var entityLoader = process.require('app/server/loaders/entityLoader.js');
var permissionLoader = process.require('app/server/loaders/permissionLoader.js');
var migrationProcess = process.require('app/server/migration/migrationProcess.js');
// Store configuration
storage.setConfiguration({
superAdminId: '0',
anonymousId: '1',
cdn: coreConf.cdn,
auth: serverConf.app.auth,
contentLanguage: supportedContentLanguages.indexOf(coreConf.contentLanguage) !== -1 ? coreConf.contentLanguage : 'en'
});
/**
* Executes a plugin function on all plugins in parallel.
*
* @param {String} functionToExecute The name of the function to execute on each plugin
* @param {Function} callback Function to call when it's done with :
* - **Error** An error if something went wrong, null otherwise
*/
function executePluginFunction(functionToExecute, callback) {
var plugins = process.api.getPlugins();
var asyncFunctions = [];
plugins.forEach(function(plugin) {
if (plugin[functionToExecute] && typeof plugin[functionToExecute] === 'function')
asyncFunctions.push(function(callback) {
plugin[functionToExecute](callback);
});
});
if (asyncFunctions.length)
async.parallel(asyncFunctions, callback);
else
callback();
}
async.series([
// Establish a connection to the database
function(callback) {
// Get a Database instance
var db = openVeoApi.storages.factory.get(databaseConf.type, databaseConf);
// Establish connection to the database
db.connect(function(error) {
if (error) {
process.logger.error(error && error.message);
throw new Error(error);
}
storage.setDatabase(db);
callback();
});
},
// Inform server that database is loaded and available
function(callback) {
server.onDatabaseAvailable(storage.getDatabase(), function() {
callback();
});
},
// Load Core plugin
function(callback) {
corePlugin = new CorePlugin();
pluginLoader.loadPluginMetadata(corePlugin, function(error) {
if (error)
return callback(error);
// Add core plugin and exposes its APIs before loading other plugins
// as some plugins could use its APIs
process.api.addPlugin(corePlugin);
callback();
});
},
// Load openveo plugins
function(callback) {
pluginLoader.loadPlugins(process.root, function(error, plugins) {
// An error occurred when loading plugins
// The server must not be launched, exit process
if (error) {
process.logger.error(error && error.message);
throw new Error(error);
} else {
var asyncFunctions = [];
// Inform that core plugin is loaded
asyncFunctions.push(function(callback) {
server.onPluginLoaded(corePlugin, callback);
});
plugins.forEach(function(loadedPlugin) {
process.api.addPlugin(loadedPlugin);
asyncFunctions.push(function(callback) {
server.onPluginLoaded(loadedPlugin, callback);
});
});
async.series(asyncFunctions, function(error, results) {
callback();
});
}
});
},
// Load entities
function(callback) {
var entities = entityLoader.buildEntities(process.api.getPlugins());
storage.setEntities(entities);
callback();
},
// Load web service scopes
function(callback) {
var scopes = permissionLoader.buildScopes(storage.getEntities(), process.api.getPlugins());
storage.setWebServiceScopes(scopes);
callback();
},
// Execute migrations script
function(callback) {
var migrations = server.migrations;
migrationProcess.executeMigrationScript(migrations, callback);
},
// Intitializes plugins
function(callback) {
process.logger.debug('Launch plugins "init" step');
executePluginFunction('init', callback);
},
// Start plugins
function(callback) {
process.logger.debug('Launch plugins "start" step');
executePluginFunction('start', callback);
},
// Inform server that all plugins are loaded
function(callback) {
server.onPluginsLoaded(callback);
},
// Start server
function(callback) {
server.startServer(callback);
}
],
function(error) {
if (error)
throw error;
});