Created
June 8, 2011 21:22
-
-
Save mnmldave/1015433 to your computer and use it in GitHub Desktop.
Simple rack PHP server rake task
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
# | |
# 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