Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow serving static files with GHCJSi #616

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/etc/irunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var h$GHCJSiRecord = // true ||

var h$GHCJSiPort = process.env['GHCJSI_PORT'] || 6400;

/* An optional static directory to serve at root */
var h$GHCJSiStaticDir = process.env['GHCJSI_STATIC_DIR'];

var h$GHCJSiReplay = process.argv.length > 0 &&
process.argv[process.argv.length-1] === 'replay';

Expand All @@ -34,6 +37,20 @@ try {
io = require('socket.io')(server);
} catch (e) { }

if (h$GHCJSiStaticDir) {
try {
/* Try to set up a static file server for GHCJSi */
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serveStaticDir = serveStatic(h$GHCJSiStaticDir);
} catch (e) {
console.log("\ncan't set up static file server for " + h$GHCJSiStaticDir);
console.log("\nmake sure you have finalhandler and serve-static installed:");
console.log("\n\n npm install finalhandler serve-static\n");
}
}


// start listening
function h$initGHCJSi() {
process.stdin.setEncoding('utf8');
Expand Down Expand Up @@ -77,6 +94,9 @@ function h$startHTTPServer() {
return;
} else {
console.log("\nsocket.io found, browser session available at http://localhost:" + h$GHCJSiPort);
if (typeof serveStaticDir !== 'undefined') {
console.log("\nserving static files from " + h$GHCJSiStaticDir);
}
}

io.on('connection', function(socket) {
Expand Down Expand Up @@ -105,6 +125,8 @@ function h$handleHTTP(req, res) {
} else if(u.pathname === '/client.js') {
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.end(h$GHCJSi.clientJs);
} else if (typeof serveStaticDir !== 'undefined') {
serveStaticDir(req, res, finalhandler(req, res));
} else {
res.statusCode = 404;
res.statusMessage = 'not found';
Expand Down