Skip to content

Instantly share code, notes, and snippets.

@JorgenEvens
Created November 4, 2015 21:13
Show Gist options
  • Save JorgenEvens/5dca38d5ae4dc924d2df to your computer and use it in GitHub Desktop.
Save JorgenEvens/5dca38d5ae4dc924d2df to your computer and use it in GitHub Desktop.
Shared hosting nodejs application without providing shell access.
var path = require('path');
var chroot = require('chroot');
var CHROOT_DIR = '/www';
var CHROOT_WWW = '/vhost/domain/http/';
var CHROOT_NODE_BIN = '/vhost/bin/node';
var USER = 2000;
var GROUP = 2000;
var webDir = path.join(__dirname, '..', 'http');
process.chdir(webDir);
chroot(CHROOT_DIR, USER, GROUP);
process.chdir(CHROOT_WWW);
process.execPath = CHROOT_NODE_BIN;
var pkg = require(path.join(CHROOT_WWW, 'package.json'));
require(path.join(process.cwd(), pkg.main));
#!/bin/bash
CHROOT_DIR="/www";
CHROOT_WWW="/vhost/domain/http";
CHROOT_NODE_BIN="/vhost/bin/node";
export PATH="$PATH:${CHROOT_DIR}/vhost/bin"
export PORT="${CHROOT_WWW}/webserver.socket";
launch() {
if [ ! -z "$PID" ]; then
kill $PID
echo "killed node $PID"
fi
if [ -S "${CHROOT_DIR}${PORT}" ]; then
echo "removing socket"
rm -Rf "${CHROOT_DIR}${PORT}"
fi
sleep 1
node ./nodeapp.js > /dev/null &
PID=$!
sleep 1
chmod 777 "${CHROOT_DIR}${PORT}"
}
launch
LASTCHANGE="start"
LASTJSCHANGE=""
inotifywait -e modify,attrib,close_write,move,create,delete -m -r ../http | while [ ! -z "$LASTCHANGE" ]; do
CURCHANGE="$LASTCHANGE"
read -t2 LASTCHANGE
if [ ! -z "$(echo $CURCHANGE | grep -P '.*\.(js|json|jsx)')" ]; then
LASTJSCHANGE="$CURCHANGE"
fi
if [ -z "$LASTCHANGE" ]; then
if [ ! -z "$(echo $LASTJSCHANGE | grep -P '.*\.(js|json|jsx)')" ]; then
echo $LASTJSCHANGE
launch
LASTJSCHANGE=""
fi
read LASTCHANGE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment