Skip to content

Instantly share code, notes, and snippets.

@bluestrike2
Created April 3, 2012 05:41
Show Gist options
  • Save bluestrike2/2289563 to your computer and use it in GitHub Desktop.
Save bluestrike2/2289563 to your computer and use it in GitHub Desktop.
Config.ru files with Pow! in mind
# config.ru for running legacy apps through pow!
# credit: http://stuff-things.net/2011/05/16/legacy-development-with-pow/ & .rvm mod @ https://github.com/DoppioJP/php-on-.pow/blob/master/config.ru
# notes: just saving as a gist because in all honestly i'd probably lose it otherwise
$:.push("/Users/#{`whoami`}/.rvm/gems/ruby-1.9.2-p290@3.1/gems/rack-1.3.0/lib/")
$:.push("/Users/#{`whoami`}/.rvm/gems/ruby-1.9.2-p290@3.1/gems/rack-legacy-0.2.0/lib/")
$:.push("/Users/#{`whoami`}/.rvm/gems/ruby-1.9.2-p290@3.1/gems/rack-rewrite-1.0.2/lib/")
require "rack"
require "rack-legacy"
require "rack-rewrite"
INDEXES = ["index.html","index.php", "index.cgi"]
use Rack::Rewrite do
rewrite %r{(.*/$)}, lambda {|match, rack_env|
to_return = rack_env["PATH_INFO"]
INDEXES.each do |index|
if File.exists?(File.join(Dir.getwd, rack_env["PATH_INFO"], index))
to_return = rack_env["PATH_INFO"] + index
end
end
to_return
}
end
use Rack::Legacy::Php, Dir.getwd
use Rack::Legacy::Cgi, Dir.getwd
run Rack::File.new Dir.getwd
# config.ru with node in mind
# credit: http://labnotes.org/2011/08/09/using-pow-with-your-node-js-project/
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
# credit: https://github.com/danott/dotfiles/blob/master/phpow/config.ru
require 'net/http'
class PhpPowApp
def call(env)
request = Rack::Request.new(env)
headers = {}
# Make the POW request to the Apache server
# http://myapp.dev/full/path/goes/here/
# ...will map to...
# http://myapp.dev:8888/full/path/goes/here/
http = Net::HTTP.new(request.host, 8888)
response = http.send_request(request.request_method, request.fullpath, request.body.read, headers)
# Map Net::HTTP response back to Rack::Request.call expects
status, headers, body = response.code, response.to_hash, [response.body]
# Research showed that browsers were choking on this for some reason.
# Probably not the be-all-end-all solution, but works for local development thus far.
headers.delete('transfer-encoding')
# Send the response back to POW
[status, headers, body]
rescue Errno::ECONNREFUSED
[500, {}, ["Server is down, try $ sudo apachectl start"]]
end
end
run PhpPowApp.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment