-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (67 loc) · 1.97 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
var express = require('express');
var fs = require('fs');
var https = require('https');
var crypto = require('crypto');
var compressor = require('node-minify');
var app = express();
var slim;
new compressor.minify({
type: 'uglifyjs',
fileIn: 'slim.js',
fileOut: 'slim.min.gcc.js',
callback: function(err, min) {
slim = fs.readFileSync('slim.min.gcc.js').toString().replace('__HOST__',process.env.PUBLIC_HOST);
}
});
new compressor.minify({
type: 'uglifyjs',
fileIn: 'public/script.js',
fileOut: 'public/script.min.gcc.js',
callback: function(err, min) {
}
});
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res) {
var path = req.url;
var lines;
if (path[path.length - 1] === ']')
{
lines = path.substring(path.lastIndexOf('[') + 1, path.length - 1).split('-');
console.dir(lines);
path = path.substr(0, path.lastIndexOf('['));
}
var options = {
host: 'raw.githubusercontent.com',
path: path,
port: 443
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function(chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
if (lines)
{
str = str.split('\n').splice(lines[0], lines[1] - lines[0]).join('\n');
}
var b = new Buffer(str).toString('base64');
res.set('Content-Type', 'text/javascript');
var shasum = crypto.createHash('sha1');
shasum.update(req.url);
var d = shasum.digest('hex');
res.send('var hash="' + d + '";\n'
+ 'var link="' + req.url + '";\n'
+ 'var raw_content="' + b + '";\n'
+ slim);
});
};
https.request(options, callback).end();
});
var server = app.listen(8080, function() {
var host = server.address().address;
var port = server.address().port;
console.log('CodeMirror GitHub app listening at http://%s:%s', host, port);
});