-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
105 lines (91 loc) · 3.16 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
var express = require('express');
var argv = require('optimist')
.usage('Heatwave: Visualize your code with realtime heatmaps!\nUsage: $0 --port=<portnum>\nAfter start, you can either \ncurl -sNT <yourcode.js> localhost:port or \nvisit localhost:port')
.alias('p', 'port')
.describe('p', 'Specify the port you want the server to run on')
.argv
;
var fs = require('fs');
var path = require('path');
if (!path.existsSync(__dirname + '/static/index.js')
|| !path.existsSync(__dirname + '/static/player.js')) {
process.stdout.write('Building source... ');
require('./bin/build')(argv);
console.log('ok');
}
var mkdirp = require('mkdirp');
mkdirp(__dirname + '/data', 0700);
var app = express.createServer();
var upload = require('./lib/upload.js');
app.use(function (req, res, next) {
if (req.method === 'PUT') {
upload.CURL(req, res);
}
else next()
});
var exampleFiles = [
'example0/callback.js',
'example1/loop.js',
'example2/interval.js'
];
var examples = exampleFiles.map(function (x) {
return fs.readFileSync(__dirname + '/data/' + x, 'utf8');
});
var recent = require('./lib/recent');
var strftime = require('strftime').strftime;
app.get('/', function (req, res) {
res.render('index.ejs', {
layout : false,
examples : examples,
host : req.headers.host || 'heatwave.nodejitsu.com',
recent : recent,
strftime : strftime
});
});
app.post('/upload', upload.WEB);
app.get(
new RegExp('^/(id|frame)/.+'),
require('./lib/player.js')
);
app.use('/file', express.static(__dirname + '/data'));
app.get(new RegExp('/files/(example[0-2]|[0-9a-f]+)'), function (req, res) {
var id = req.params[0];
fs.readdir(__dirname + '/data/' + id, function (err, files) {
if (err) {
console.log('fs.readdir error:', err.toString());
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.end(err.toString());
}
else {
var stats = {};
var pending = files.length;
function sortFiles () {
res.setHeader('content-type', 'application/json');
var sorted = Object.keys(stats).sort(function (a,b) {
return stats[a].mtime - stats[b].mtime;
});
res.end(JSON.stringify(sorted));
}
files.forEach(function (file) {
var pfile = __dirname + '/data/' + id + '/' + file;
fs.stat(pfile, function (err, stat) {
if (err) {
console.log('fs.stat error:', err.toString());
res.statusCode = 500;
res.setHeader('content-type', 'text/plain');
res.end(err.toString());
}
else {
stats[file] = stat;
if (--pending === 0) sortFiles()
}
})
});
}
});
});
app.use(express.static(__dirname + '/static'));
var port = argv.port || argv.p || 80;
app.listen(port)
console.log('Listening on :' + port);