middleman/bin/mm-server

76 lines
1.7 KiB
Plaintext
Raw Normal View History

2009-07-28 01:25:32 +02:00
#!/usr/bin/env ruby
2009-10-01 23:26:39 +02:00
require 'optparse'
2009-07-28 01:25:32 +02:00
# Require Middleman
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
2009-10-01 23:26:39 +02:00
class Middleman::Base
set :root, Dir.pwd
2009-10-15 20:07:08 +02:00
set :logging, false
enable :static
end
2009-10-01 23:26:39 +02:00
2009-10-15 20:07:08 +02:00
env = ENV['RACK_ENV'] || 'development'
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
2009-10-01 23:26:39 +02:00
2009-10-15 20:07:08 +02:00
OptionParser.new { |opts|
opts.banner = "Usage: mm-server [rack options]"
opts.separator ""
opts.separator "Rack options:"
opts.on("-p", "--port PORT", "use PORT (default: 4567)") { |port|
options[:Port] = port
}
opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT for defaults (default: development)") { |e|
env = e
}
opts.parse! ARGV
}
require 'rack'
require 'rack/utils'
require 'rack/request'
require 'rack/response'
require 'rack/showexceptions'
module Middleman
class Static
def initialize(app, options={})
@app = app
root = Middleman::Base.public
@file_server = Rack::File.new(root)
end
def call(env)
path = env["PATH_INFO"]
if path.include?("favicon.ico") || File.exists?(File.join(Middleman::Base.public, path))
@file_server.call(env)
else
@app.call(env)
end
end
end
end
app_wrapper = lambda do |inner_app|
Rack::Builder.new {
use Rack::ShowExceptions
use Middleman::Static
run Middleman::Base
}.to_app
end
ENV['RACK_ENV'] = env
require 'shotgun'
require 'thin'
config = File.join(File.dirname(__FILE__), '..', 'lib', 'middleman', 'config.ru')
app = Shotgun.new(config, app_wrapper)
Thin::Logging.silent = true
Rack::Handler::Thin.run app, options do |inst|
puts "== The Middleman is standing watch on port #{options[:Port]}"
2009-10-01 23:26:39 +02:00
end