76 lines
1.7 KiB
Ruby
Executable file
76 lines
1.7 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
|
|
require 'optparse'
|
|
|
|
# Require Middleman
|
|
require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
|
|
|
|
class Middleman::Base
|
|
set :root, Dir.pwd
|
|
set :logging, false
|
|
enable :static
|
|
end
|
|
|
|
env = ENV['RACK_ENV'] || 'development'
|
|
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
|
|
|
|
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]}"
|
|
end |