Created
April 3, 2012 05:41
-
-
Save bluestrike2/2289563 to your computer and use it in GitHub Desktop.
Config.ru files with Pow! in mind
This file contains hidden or 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
# 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/[email protected]/gems/rack-1.3.0/lib/") | |
$:.push("/Users/#{`whoami`}/.rvm/gems/[email protected]/gems/rack-legacy-0.2.0/lib/") | |
$:.push("/Users/#{`whoami`}/.rvm/gems/[email protected]/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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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