-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
208 lines (180 loc) · 5.8 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
const config = require('./config/server.config.js');
const chalk = require('chalk');
const fs = require('fs');
const uuidv4 = require('uuid/v4');
const intercept = require('intercept-stdout');
const userCont = require('./controllers/user');
const cluster = require('cluster');
if(cluster.isMaster) {
console.log(
chalk.green(` ___ _ _ _ _\n`),
chalk.green(` |_ _|__| | ___ _ __ | |_(_) |_ _ _\n`),
chalk.green(` | |/ _\` |/ _ \\ '_ \\| __| | __| | | |\n`),
chalk.green(` | | (_| | __/ | | | |_| | |_| |_| |\n`),
chalk.green(` |___\\__,_|\\___|_| |_|\\__|_|\\__|\\__, |\n`),
chalk.green(` |___/`)
);
console.log(
chalk.green(` ____ \n`),
chalk.green(` / ___| ___ _ ____ _____ _ __ \n`),
chalk.green(` \\___ \\ / _ \\ '__\\ \\ / / _ \\ '__|\n`),
chalk.green(` ___) | __/ | \\ V / __/ | \n`),
chalk.green(` |____/ \\___|_| \\_/ \\___|_| \n`)
);
}
const stickyCluster = require('sticky-cluster')(function(callback) {
//intercept stdout and add worker tags
intercept(function(txt) {
let str = chalk.magenta('[WORKER '+ process.env.stickycluster_worker_index + '] ') +txt;
return txt.includes('Primus') ? '' : str;
//return str;
});
//create express app
const express = require('express');
const app = express();
//create options and include keys for web server
const options = {
key: fs.readFileSync('./ssl/server.key'),
cert: fs.readFileSync('./ssl/server.crt'),
ca: fs.readFileSync('./ssl/ca.crt'),
requestCert: true,
rejectUnauthorized: false
};
//bind express app to https server
const server = require('https').createServer(options, app);
//force the use of https on the web server
const forceSSL = require('./ssl/forceSSL');
app.use(forceSSL);
//configure eureca RPC server
const Eureca = require('eureca.io');
const eurecaOptions = {
allow:[
'user',
'createdUser',
'lookup',
'reverseLookup',
'remove',
'modify',
'get',
'err',
'shutdown'
],
transport: 'faye'
};
const eurecaServer = new Eureca.Server(eurecaOptions);
eurecaServer.attach(server);
//functions under "exports" namespace will be exposed to client side
//A UUID generator test function
eurecaServer.exports.uuid = function () {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'requested uuid, generating...'
);
client.user({uuid: uuidv4()});
};
//Create a user
eurecaServer.exports.createUser = function (user) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'requested user creation...'
);
userCont.createUser(this, user);
}
//Lookup a user
eurecaServer.exports.lookup = function(loginName) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'looking up user ' + loginName + '...'
);
userCont.lookup(this, loginName);
}
//reverse lookup a user by UUID
eurecaServer.exports.reverseLookup = function(uuid) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'looking up user ' + uuid + '...'
);
userCont.reverseLookup(this, uuid);
}
//delete a user
eurecaServer.exports.delete = function(loginName, password) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'attempting to delete user ' + loginName + '...'
);
userCont.delete(this, loginName, password);
}
//modify a user
eurecaServer.exports.modify = function(oldLoginName, newLoginName, password) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'attempting to modify user ' + oldLoginName + '...'
);
userCont.modify(this, oldLoginName, newLoginName, password);
}
//get existing user list
eurecaServer.exports.get = function(arg) {
let client = this.clientProxy;
let connection = this.connection;
console.log(
chalk.green(`[${connection.id}]`),
'retrieving information...'
);
userCont.get(this, arg);
}
//Server side event handlers
eurecaServer.onConnect(function (connection) {
let client = connection.clientProxy;
console.log(chalk.green(`[${connection.id}]`), 'client connected');
});
//all messages middleware, good for debugging
/*eurecaServer.onMessage(function (msg) {
console.log('RECV', msg);
});*/
eurecaServer.onDisconnect(function (connection) {
console.log(chalk.green(`[${connection.id}]`), 'client disconnected');
});
eurecaServer.onError(function (e) {
console.log('an error occured', e);
});
process.on('SIGINT', function() {
let connections = eurecaServer.ioServer.primus.connections;
for(k in connections) {
console.log(
chalk.red('closing connection for'),
chalk.green(`[${k}].`)
);
eurecaServer.getClient(k).shutdown();
}
process.exit(0);
});
process.on('exit', function() {
console.log(chalk.red('shutting down server...'));
});
//start the server listening with sticky sessions (by IP)
callback(server);
},
{
concurrency: config.express.workers,
port: process.env.PORT || config.express.port,
debug: false,
env: function (index) { return { stickycluster_worker_index: index }; }
});
process.on('SIGINT', function() {
process.exit(0);
});
/*process.on('exit', function() {
console.log(chalk.red('\nshutting down parent server...'));
});*/