-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
151 lines (133 loc) · 5.01 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
'use strict';
// Handle heapdumps, this can be activated by sending a SIGUSR2 signal to the process (kill -USR2 <pid>)
// This will send a snapshot to the cwd
//require('heapdump');
var Hapi = require('hapi');
var FeedientAuthentication = require('./src/plugins/FeedientAuthentication');
var fs = require('fs');
var UserSession = require('./src/entities/userSession');
var http = require('http');
var https = require("https");
// ROUTES
var V1MiscApi = require('./src/routes/v1/misc');
var V1ProviderApi = require('./src/routes/v1/provider');
var V1ProvidersApi = require('./src/routes/v1/providers');
var V1UserApi = require('./src/routes/v1/user');
var V1MetricsApi = require('./src/routes/v1/metrics');
var V1NotificationsApi = require('./src/routes/v1/notification');
var V1WorkspaceApi = require('./src/routes/v1/workspace');
var V1PanelApi = require('./src/routes/v1/panel');
var V1PaymentApi = require('./src/routes/v1/payments');
// Socket Handler
var Socket = require('./src/lib/socket');
// CONFIGURATION
var config = require('./config/app');
// Include newrelic on production only
//if (config.server.new_relic_enabled) {
// require('newrelic');
//}
var serverPort = process.env.NODE_PORT || process.argv[3] || 8000; // Default port is 8000
// Set the max sockets to be open / origin, this gets regulated by the connected sockets
http.globalAgent.maxSockets = Number.MAX_VALUE;
https.globalAgent.maxSockets = Number.MAX_VALUE;
// Create logs directory if it doesn't exists yet
if (!fs.existsSync('./logs')) {
fs.mkdirSync('./logs');
}
// Create server object
var server = new Hapi.Server(config.server.ip, serverPort, {
cors: {
origin: config.server.cors_client_origins,
headers: config.server.cors_headers,
methods: config.server.cors_methods,
credentials: config.server.cors_credentials
}
});
// Validate Credentials (Used by the FeedientAuthenticate plugin)
var checkUserCredentials = function(token, callback) {
UserSession.validateUserToken(token, function(err, user) {
if (err) return callback(err, false);
if (!user) return callback(null, false);
return callback(null, true, user);
});
};
// Starts the API Server
function start(callback) {
// Load Logger
server.pack.register([
{
plugin: require('./src/plugins/FeedientAuthentication'),
options: { roles: config.user.roles }
},
{
plugin: require('good'),
options: {
opsInterval: 1000,
reporters: [{
reporter: require('good-console'),
args:[{ log: '*', request: '*' }]
}]
}
},
// {
// plugin: require('./src/plugins/FeedientMetrics'),
// options: config.metrics
// },
{
plugin: require('./src/plugins/FeedientMongoose'),
options: config.database.api_server
},
// {
// plugin: require('./src/plugins/FeedientMongodb'),
// options: config.database.metrics_server
// },
{
plugin: require('./src/plugins/FeedientI18N-Hapi'),
options: config.i18n
}
], function(err) {
if (err) { throw err; }
// Change default authentication strategy to feedient, also register feedient as a strategy
server.auth.strategy('default', 'feedient', { checkUserCredentialsFunc: checkUserCredentials });
server.auth.strategy('feedient', 'feedient', { checkUserCredentialsFunc: checkUserCredentials });
// Register the documentation generator
server.pack.register([
{
plugin: require('./src/plugins/hapi-swagger'),
options: config.documentation
}
], function(err) {
// Configure routes
V1MiscApi.routes(server);
V1ProviderApi.routes(server);
V1ProvidersApi.routes(server);
V1UserApi.routes(server);
V1MetricsApi.routes(server);
V1NotificationsApi.routes(server);
V1WorkspaceApi.routes(server);
V1PanelApi.routes(server);
V1PaymentApi.routes(server);
// Start the server
server.start(function() {
server.log(['info', 'server'], 'Server started on ' + config.server.ip + ':' + serverPort);
return callback();
});
});
});
};
function stop(timeout, callback) {
server.stop({ timeout: timeout }, function() {
return callback();
});
};
function startSocketListener(callback) {
var socketService = new Socket(server);
socketService.start();
return callback();
};
module.exports = {
start: start,
stop: stop,
startSocketListener: startSocketListener,
server: server
};