This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathproduction.js
59 lines (52 loc) · 1.92 KB
/
production.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
var http = require('http');
var sandbox = require('./sandbox');
var db = require('./db');
var config = require('./config');
var boxes={};
var server = http.createServer(function (req, res) {
if(req.connection.remoteAddress != "127.0.0.1") {res.writeHead(400, {"Connection":"close"}); return res.end();}
if(boxes[req.headers.host]) {
boxes[req.headers.host]._last_use = Date.now();
boxes[req.headers.host].server(req,res);
}else{
var hostBase = req.headers.host.replace(config.productionBase, "");
console.log(req.headers.host);
db.get(hostBase != req.headers.host ? ("app_" + hostBase) : ("app_"+req.headers.host), function (code) {
console.log(code);
if(!code) {
res.writeHead(503, {"Content-type":"text/html"});
res.end("Host not found");
return;
}
code = JSON.parse(code);
boxes[req.headers.host] = new sandbox.SandBox(code.code, {test: false, user: code.user, name: code.name, log: function () {}, error: function () {}});
boxes[req.headers.host]._loaded = Date.now();
boxes[req.headers.host]._last_use = Date.now();
boxes[req.headers.host].server(req, res);
});
}
});
setInterval(function () {
var time = Date.now();
for(var a in boxes) {
if(time - boxes[a]._last_use > 300*1000) {
delete boxes[a];
}
}
for(var a in boxes) {
if(time - boxes[a]._loaded > 7*60*1000) {
var hostBase = a.replace(config.productionBase, "");
db.get(hostBase != a ? ("app_" + hostBase) : ("Domain_"+req.headers.host), function (code) {
code = JSON.parse(code);
boxes[a] = new sandbox.SandBox(code.code, {test: false, user: code.user, app: code.app});
boxes[a]._loaded = time;
boxes[a]._last_use = time - 60*1000;
});
}
}
}, 20000);
setInterval(function () {
console.log(config.checkOkCode);
}, config.sendOkInterval);
server.listen(/^[0-9]*$/.exec(process.argv[2]) ? process.argv[2] : process.argv[2]*1);
console.log("production up", process.argv[2]);