forked from collections-online/collections-online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (98 loc) · 3.44 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
'use strict';
const Q = require('q');
const plugins = require('./plugins');
const config = require('./lib/config');
const promiseRetry = require('promise-retry');
let co = {
config: (customizationPath) => {
require('./lib/config').setCustomizationPath(customizationPath);
},
registerPlugins: () => {
// Register the default fallback plugins
if(config.es) {
plugins.register(require('./plugins/elasticsearch'));
}
if(config.features.keystone) {
plugins.register(require('./plugins/keystone'));
}
if(config.features.users) {
plugins.register(require('./plugins/auth'));
}
},
initialize: (app) => {
if(!app) {
throw new Error('Needed an Express app when initializing');
}
// Setting the NODE_ENV environment if it's not already sat
// TODO: Consider removing this
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// After all plugins have initialized, the main server should start
return plugins.initialize(app).then(() => {
require('./lib/express')(app);
const ds = require('./lib/services/documents');
app.locals.config = config;
const helpers = require('./lib/helpers');
helpers.checkRequiredHelpers();
app.locals.helpers = helpers;
// Injects an SVG sprite
app.use(require('./lib/middleware/svg-sprite'));
// Trust the X-Forwarded-* headers from the Nginx reverse proxy infront of
// the app (See http://expressjs.com/api.html#app.set)
app.set('trust proxy', 'loopback');
const indecies = Object.keys(config.types).map((type) => {
return config.types[type].index;
});
return promiseRetry(retry => {
return ds.count({
index: indecies,
body: {
query: config.search.baseQuery
}
})
.catch(( err ) => {
console.error('Could not connect to the Elasticsearch:',
'Is the elasticsearch service started?');
console.error('Retrying elasticsearch');
retry(err);
});
}, {minTimeout: 4000})
.then(response => {
console.log('Index exists and has', response.count, 'documents.');
}, err => {
console.log('Elasticsearch not found after several attempts.');
process.exit(1);
})
.then(() => {
console.log('Starting up the server');
// Start server
if(config.port && config.ip) {
app.listen(config.port, config.ip, function() {
console.log('Express server listening on %s:%d, in %s mode',
config.ip, config.port, app.get('env'));
});
} else if(config.socketPath) {
app.listen(config.socketPath, function() {
console.log('Express server listening on socket %s, in %s mode',
config.socketPath, app.get('env'));
});
} else {
throw new Error('Could not start server, needed "port" and "ip" ' +
'or "socketPath" in the configuration.');
}
}, (err) => {
console.error('Error when starting the app: ', err.stack);
process.exit(2);
});
});
},
registerRoutes: app => {
// Ask plugins to register their routes
plugins.registerRoutes(app);
// Register the core collections-online routes
require('./lib/routes')(app);
},
registerErrors: (app) => {
require('./lib/errors')(app);
}
};
module.exports = co;