Skip to content

Instantly share code, notes, and snippets.

@mnmldave
Created June 8, 2011 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnmldave/1015433 to your computer and use it in GitHub Desktop.
Save mnmldave/1015433 to your computer and use it in GitHub Desktop.
Simple rack PHP server rake task
#
# Sample rake task for starting a Thin rack server for a PHP application (like
# Wordpress or Drupal or whatever).
#
# Requires php-cgi be available on path (I used homebrew on my mac to install
# php since it doesn't come pre-installed) and gems 'thin', 'rack' and
# 'rack-legacy'.
#
BASE_PATH = File.expand_path(File.dirname(__FILE__))
SRC_PATH = File.join(BASE_PATH, 'src')
SERVER_PORT = '3000'
SERVER_HOST = '0.0.0.0'
desc 'Starts a thin rack server to dish out the src directory'
task :server do
require 'thin'
require 'rack-legacy'
require 'rack/legacy/cgi'
require 'rack/legacy/php'
# Handler to sort of simulate Apache's DirectoryIndex directive
class DirectoryIndex
def initialize(app, root=Dir.pwd, files=[])
@app = app
@root = root
@files = files
end
def call(env)
if env['PATH_INFO'] =~ /\/$/
path = File.join(@root, Rack::Utils.unescape(env['PATH_INFO']))
if File.directory?(path)
@files.each do |file|
if File.exists?(File.join(path, file))
env['PATH_INFO'] = env['PATH_INFO'] + file
break
end
end
end
end
@app.call(env)
end
end
app = Rack::Builder.new do
use DirectoryIndex, SRC_PATH, ['index.php', 'index.html', 'index.htm']
use Rack::ShowExceptions
use Rack::Legacy::Php, SRC_PATH
use Rack::Legacy::Cgi, SRC_PATH
run Rack::Directory.new(SRC_PATH)
end
Rack::Handler::Thin.run(app, { :Host => SERVER_HOST, :Port => SERVER_PORT })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment