Skip to content

Instantly share code, notes, and snippets.

@bkaney
Last active December 30, 2015 14:49
Show Gist options
  • Save bkaney/7844458 to your computer and use it in GitHub Desktop.
Save bkaney/7844458 to your computer and use it in GitHub Desktop.
gopow - a wrapper to setup pow, then run the server in the foreground.
#!/bin/sh
cmd=$*
function help {
cat <<-EOH
Usage: gopow <startup command>
gopow - A wrapper that selects a random port, writes to a file, and informs pow.
Note:
This program should be run in the root working directory of the application.
There are conventions that assume this. And you need pow (http://pow.cx/)
Description:
I wanted to make a program that would use the resolution and random port
aspects of pow, but didn't want to run the program in the background (mainly
because of how tedious remote-pry can be). I also wanted more fine grained
control of starting the app. This script assumes a foreman-based startup
command.
Some examples:
$ gopow bundle exec foreman start
$ gopow bundle exec foreman run rails s
$ gopow bundle exec foreman run rackup
Informing the local web app:
This program will write the port configured for pow to use to a file called
.foreman. This file sets defaults for foreman to use (overriding any
environment ones).
If you aren't using foreman, you will need to know what port to start the
application on. You could parse the .foreman file for the single line in
the form:
port: <port>
EOH
}
function gopow {
# NOTE: This finds the first unused, unpriviliged port
# see http://www.commandlinefu.com/commands/view/7299/find-an-unused-unprivileged-tcp-port
port="$((netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq 32768 61000) | sort -n | uniq -u | head -n 1)"
# Write to .foreman and to pow
if [ -f .foreman ]
then
cp -f .foreman .foreman-original
fi
echo "port: $port" >> .foreman
powfile="$(basename $PWD)"
powfile="$(echo $powfile | sed 's/_/-/g')"
echo $port > ~/.pow/$powfile
# Trap control-c, so we can cleanup
trap "echo Interrupted" INT
eval "($cmd)"
trap - INT
# Cleanup .foreman and pow
if [ -f .foreman-original ]
then
mv -f .foreman-original .foreman
fi
rm ~/.pow/$powfile
}
if [ "$cmd" == "" ]; then
help
exit 1
fi
gopow
@bkaney
Copy link
Author

bkaney commented Dec 7, 2013

Installation:

  1. Install pow
$ curl get.pow.cx | sh
  1. Install this program in your PATH (assume you have ~/bin, and it is in your path)
$ curl https://gist.github.com/bkaney/7844458/raw/gopow > ~/bin/gopow
$ chmod 0755 ~/bin/gopow
  1. Use it

Visit http://myapp.dev # => Pow error

$ cd myapp
$ gopow bundle exec foreman start

(or whatever you use to start your app, it should be foreman-based, see help)

Visit http://myapp.dev # => My App!

@bkaney
Copy link
Author

bkaney commented Dec 7, 2013

Oh, you may want to echo ".foreman" >> .gitignore

@bkaney
Copy link
Author

bkaney commented Dec 7, 2013

I am finding this alias to be useful:

alias gpr="gopow bundle exec foreman run"
alias gps="gopow bundle exec foreman start"

Then in my app gpr rails s or just gps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment