-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
146 lines (129 loc) · 3.99 KB
/
app.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
/*
* Ethereum Sandbox
* Copyright (C) 2016 <ether.camp> ALL RIGHTS RESERVED (http://ether.camp)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License version 3 for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var http = require('http');
var fork = require('child_process').fork;
var express = require('express');
var cors = require('cors');
var app = express();
var bodyParser = require('body-parser');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var events = new EventEmitter();
var Control = require('./sandbox_control');
var plugins = require('./plugins');
plugins.load(events);
var server, control, unusedWatcher;
var lastTouch = Date.now();
var defaultPort = 8555;
var testModeUnusedLifetime = 15 * 1000;
var testModeUnusedCheckInterval = 10 * 1000;
function start(testMode, port, cb) {
if (_.isFunction(port)) {
cb = port;
port = defaultPort;
}
control = Control.init(events);
app.use(cors());
app.use(bodyParser.json({ limit: '10mb' }));
if (testMode) {
app.use(function(req, res, next) {
lastTouch = Date.now();
next();
});
}
app.post('/sandbox', function(req, res) {
control.create(req.query.id, req.body, function(err, instance) {
if (err) res.status(500).send(err);
else res.json({ id: instance.id });
});
});
app.post('/sandbox/:id', function(req, res, next) {
if (!control.contains(req.params.id)) res.sendStatus(404);
else control.instance(req.params.id).middleware(req, res, next);
});
app.delete('/sandbox/:id', function(req, res, next) {
if (!control.contains(req.params.id)) res.sendStatus(404);
else {
control.stop(req.params.id, function(err) {
if (err) res.status(500).send(err);
else res.sendStatus(200);
});
}
});
app.get('/sandbox', function(req, res) {
res.json(_.keys(control.instances));
});
app.post('/reset', function(req, res) {
control.reset(function(err) {
if (err) res.status(500).send(err);
else res.sendStatus(200);
});
});
server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Sandbox is listening at http://%s:%s', host, port);
if (cb) cb();
});
if (testMode) {
unusedWatcher = setInterval(stopIfUnused, testModeUnusedCheckInterval);
}
}
function stopIfUnused() {
if (Date.now() > lastTouch + testModeUnusedLifetime) stop();
}
function startDetached(port, cb) {
if (_.isFunction(port)) {
cb = port;
port = defaultPort;
}
fork(__filename, [ '--test', '--port=' + port ]);
var checksNum = 20;
(function check() {
if (checksNum-- == 0) return cb('Sandbox has not been started in 10 sec.');
isRunning(function(running) {
if (running) cb();
else setTimeout(check, 500);
});
})();
function isRunning(cb) {
http.get('http://localhost:' + port + '/sandbox', function(res) {
cb(res.statusCode == 200);
}).on('error', cb.bind(null, false));
}
}
function stop() {
if (unusedWatcher) clearInterval(unusedWatcher);
control.reset();
control.stopWatchingUnused();
server.close();
}
if (require.main === module) {
var testMode = false;
var port = defaultPort;
process.argv.forEach(function(arg) {
if (arg == '--test') testMode = true;
if (arg.indexOf('--port=') == 0) port = arg.substr(7);
});
start(testMode, port);
} else {
module.exports = {
start: start.bind(null, true),
startDetached: startDetached,
stop: stop
};
}