-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.js
69 lines (53 loc) · 1.7 KB
/
serve.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
var http = require('http');
var url = require('url');
var fs = require('fs');
var error = require('./error');
var shelljs = require('shelljs');
require('colors');
var defaultPort = 8077;
module.exports = function(kernelPath, initrdPath, ipxePath, port) {
var port = port || defaultPort;
if (!fs.existsSync(kernelPath)) {
return error('error: no kernel found at "' + kernelPath + '"');
}
if (!fs.existsSync(initrdPath)) {
return error('error: no initrd found at "' + initrdPath + '"');
}
if (!fs.existsSync(ipxePath)) {
return error('error: no ipxe.txt found at "' + ipxePath + '"');
}
var server = http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
var servePath;
var contentType;
if ('/runtime' === uri) {
servePath = kernelPath;
contentType = 'application/octet-stream';
}
if ('/initrd' === uri) {
servePath = initrdPath;
contentType = 'application/octet-stream';
}
if ('/ipxe.txt' === uri) {
servePath = ipxePath;
contentType = 'text/plain';
}
if (servePath) {
var ip = req.connection.remoteAddress;
shelljs.echo('['.gray + ip.gray + '] '.gray + 'GET '.magenta + uri.yellow + ' \u2192 ' + servePath.green);
res.writeHead(200, { 'Content-Type': contentType });
fs.createReadStream(servePath).pipe(res);
return;
}
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not found');
})
server.listen(port);
server.on('listening', function() {
shelljs.echo('listening to port ' + String(port).yellow);
});
server.on('error', function() {
return error('error: serve failed');
});
};
module.exports.defaultPort = defaultPort;