-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (73 loc) · 2.67 KB
/
Copy pathserver.js
File metadata and controls
85 lines (73 loc) · 2.67 KB
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
var fs = require('fs');
var path = require('path');
var express = require('express');
var connect = require('connect');
var bodyParser = require('body-parser');
var http = require('http');
var https = require('https');
var defaults = require('./defaults');
var auth = require('./auth');
var extend = require('util')._extend;
var version = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version;
var Server = function(manager, options) {
options = options || {};
this._manager = manager;
this._httpsOptions = null;
this._app = express();
this._app.use(bodyParser.json());
this._authType = (options.auth && options.auth.type) || 'none';
auth.init(this, auth.server, 'auth', options);
this.options = extend(defaults, options);
this._routes();
};
Server.prototype.start = function(options) {
options = options || {};
options.port = options.port || this.options.port;
options.host = options.host || this.options.host;
this._server = this._httpsOptions ? https.createServer(this._httpsOptions, this._app) : http.createServer(this._app);
this._server.listen(options.port, options.host);
!options.quiet && console.log('Shoal Manager running on http://' + options.host + ':' + options.port + " (Authentication="+this._authType+")")
};
Server.prototype.stop = function() {
this._server && this._server.close();
};
Server.prototype._execute = function(req, res, next) {
var command = req.param('command');
var arguments = req.param('arguments');
if (!command) return this._error(res, 400, 'Need :command parameter');
if (defaults.commands.indexOf(command) == -1) return this._error(res, 400, 'Invalid command: ' + command);
try {
var result = this._manager[command].apply(this._manager, arguments || []);
if (result && typeof result == 'object')
result = JSON.parse(JSON.stringify(result, safeCycles()));
res.send({result: result || 'ok'});
} catch(err) {
this._error(res, 500, err.message);
}
};
Server.prototype._info = function(req, res, next) {
return res.send({shoal: {version: version}})
}
Server.prototype._routes = function() {
this._app.post('/execute', this._execute.bind(this));
this._app.get('/', this._info.bind(this));
};
Server.prototype._error = function(res, statusCode, message) {
res.status(statusCode).send({error: {message: message}});
};
// A JSON stringifier that handles cycles safely.
// Usage: JSON.stringify(obj, safeCycles())
function safeCycles() {
var seen = [];
return function (key, val) {
if (!val || typeof (val) !== 'object') {
return val;
}
if (seen.indexOf(val) !== -1) {
return '[Circular]';
}
seen.push(val);
return val;
};
}
module.exports = Server;