Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Suan-Aik Yeo
committed
Feb 10, 2012
1 parent
b8a536a
commit 79e614e
Showing
2 changed files
with
64 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,74 @@ | ||
#!/usr/bin/env node | ||
|
||
var http = require('http'), | ||
var server = require('http').createServer(httpHandler), | ||
spawn = require('child_process').spawn, | ||
exec = require('child_process').exec, | ||
io = require('socket.io').listen(server), | ||
fs = require('fs'), | ||
tmpfilePath; | ||
server, | ||
socket; | ||
|
||
http.createServer(function (req, res) { | ||
server.listen(8090); | ||
|
||
function httpHandler(req, res) { | ||
process.stdout.write(req.method + "\n"); | ||
switch(req.method) | ||
{ | ||
case 'GET': | ||
exec('mktemp -t instant-markdown', | ||
fs.readFile(__dirname + '/instant-markdown.html', function(err, data){ | ||
res.writeHead(200); | ||
res.end(data); | ||
}); | ||
break; | ||
|
||
case 'HEAD': | ||
res.writeHead(200); | ||
res.end(); | ||
exec('open -g http://localhost:8090', | ||
function(error, stdout, stderr){ | ||
tmpfilePath = stdout + '.html'; | ||
fs.writeFileSync(tmpfilePath, '<html></html>'); | ||
exec('open -a safari -g ' + tmpfilePath, | ||
function(error, stdout, stderr){ | ||
|
||
}); | ||
}); | ||
|
||
}); | ||
break; | ||
|
||
case 'DELETE': | ||
socketio.send('killing js', function(){ | ||
fs.rmdirSync(tmpfilePath); | ||
process.exit(); | ||
}); | ||
socket.emit('die'); | ||
process.exit(); | ||
break; | ||
|
||
case 'PUT': | ||
var gfm = spawn('gfm'); | ||
gfm.stdout.on('data', function(data) { | ||
socketio.write(data); | ||
}); | ||
gfm.on('exit',function(ecode){ | ||
|
||
io.write(data); | ||
}); | ||
// gfm.on('exit',function(ecode){ | ||
// | ||
// }); | ||
break; | ||
|
||
default: | ||
} | ||
res.writeHead(200, {'Content-Type': 'text/plain'}); | ||
res.end('Hello World\n'); | ||
}).listen(8090, "localhost"); | ||
} | ||
|
||
io.sockets.on('connection', function(sock){ | ||
socket = sock; | ||
process.stdout.write('connection established!'); | ||
}); | ||
|
||
process.stderr.on('data',function(err) { | ||
process.stdout.write('ERR: '+err); | ||
}) | ||
|
||
process.stdin.resume(); | ||
process.stdin.setEncoding('utf8'); | ||
|
||
process.stdin.on('data', function (chunk) { | ||
gfm.stdin.write(chunk); | ||
gfm.stdin.end(); | ||
}); | ||
|
||
// var mdFilePath = process.argv[2]; | ||
// if (mdFilePath){ } | ||
|
||
|
||
|
||
|
||
// process.stderr.on('data',function(err) { | ||
// process.stdout.write('ERR: '+err); | ||
// }) | ||
|
||
// process.stdin.resume(); | ||
// process.stdin.setEncoding('utf8'); | ||
|
||
// process.stdin.on('data', function (chunk) { | ||
// gfm.stdin.write(chunk); | ||
// gfm.stdin.end(); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html> | ||
<head> | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script> | ||
var socket = io.connect('http://localhost:8090/'); | ||
socket.on('connect', function () { | ||
socket.on('newContent', function(newHTML) { | ||
document.body.innerHTML = newHTML; | ||
}); | ||
socket.on('die', function(newHTML) { | ||
window.open('', '_self', '');window.close(); | ||
}); | ||
}); | ||
</script> | ||
</head> | ||
<body> | ||
</body> | ||
</html> |