Created
August 9, 2011 05:51
-
-
Save weskoop/1133483 to your computer and use it in GitHub Desktop.
powjs is an oh-my-zsh plugin to quickly configure your Node project for use with Pow.
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
# powjs r1 - @weskoop | |
# Config.ru concept and file by Assaf Arkin http://labnotes.org/2011/08/06/using-pow-with-your-node-js-project/ | |
function powjs { | |
if [[ $# -ne 0 ]]; then | |
if [[ $1 == "config" ]]; then | |
echo "Creating Node compatible config.ru..." | |
cat > config.ru <<POWJS | |
require "net/http" | |
class ProxyApp | |
def call(env) | |
begin | |
request = Rack::Request.new(env) | |
headers = {} | |
env.each do |key, value| | |
if key =~ /^http_(.*)/i | |
headers[$1] = value | |
end | |
end | |
http = Net::HTTP.new("localhost", 8080) | |
http.start do |http| | |
response = http.send_request(request.request_method, request.fullpath, request.body.read, headers) | |
[response.code, response.to_hash, [response.body]] | |
end | |
rescue Errno::ECONNREFUSED | |
[500, {}, ["Server is down, try $ npm start"]] | |
end | |
end | |
end | |
run ProxyApp.new | |
POWJS | |
elif [[ $1 == "link" ]]; then | |
echo "Symlinking `pwd` to ~./Pow..." | |
ln -s `pwd` ~/.pow | |
elif [[ $1 == "unlink" ]]; then | |
echo "Unlinking ${PWD##*/} from ~./Pow..." | |
rm ~/.pow/${PWD##*/} | |
fi | |
else | |
echo "" | |
echo "Usage: powjs <command>" | |
echo "" | |
echo " Commands: config # Create Node compatible config.ru (will overwrite exising file)" | |
echo " link # Link current directory in ~/.pow" | |
echo " unlink # Unlink current directory from ~/.pow" | |
echo "" | |
echo "Once configured and linked, run 'npm start' to start your server." | |
echo "Access it at http://${PWD##*/}.dev" | |
echo "" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment