riding shotgun
This commit is contained in:
parent
ca34f2fe61
commit
2400c670b4
2
Rakefile
2
Rakefile
|
@ -11,6 +11,8 @@ begin
|
||||||
gem.authors = ["Thomas Reynolds"]
|
gem.authors = ["Thomas Reynolds"]
|
||||||
gem.rubyforge_project = "middleman"
|
gem.rubyforge_project = "middleman"
|
||||||
gem.executables = %w(mm-init mm-build mm-server)
|
gem.executables = %w(mm-init mm-build mm-server)
|
||||||
|
gem.add_dependency("thin")
|
||||||
|
gem.add_dependency("shotgun")
|
||||||
gem.add_dependency("templater")
|
gem.add_dependency("templater")
|
||||||
gem.add_dependency("sprockets")
|
gem.add_dependency("sprockets")
|
||||||
gem.add_dependency("sinatra")
|
gem.add_dependency("sinatra")
|
||||||
|
|
|
@ -7,13 +7,70 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'middleman')
|
||||||
|
|
||||||
class Middleman::Base
|
class Middleman::Base
|
||||||
set :root, Dir.pwd
|
set :root, Dir.pwd
|
||||||
|
set :logging, false
|
||||||
|
enable :static
|
||||||
|
end
|
||||||
|
|
||||||
OptionParser.new { |op|
|
env = ENV['RACK_ENV'] || 'development'
|
||||||
op.on('-e env') { |val| set :environment, val.to_sym }
|
options = { :Port => 4567, :Host => 'localhost', :AccessLog => [] }
|
||||||
op.on('-s server') { |val| set :server, val }
|
|
||||||
op.on('-p port') { |val| set :port, val.to_i }
|
|
||||||
}.parse!(ARGV.dup)
|
|
||||||
|
|
||||||
# Start Middleman
|
OptionParser.new { |opts|
|
||||||
run!
|
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
|
end
|
|
@ -62,12 +62,11 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
include StaticRender
|
include StaticRender
|
||||||
|
|
||||||
# Disable static asset handling in Rack, so we can customize it here
|
|
||||||
disable :static
|
|
||||||
|
|
||||||
# This will match all requests not overridden in the project's init.rb
|
# This will match all requests not overridden in the project's init.rb
|
||||||
not_found do
|
not_found do
|
||||||
|
self.class.init!(true, false)
|
||||||
|
|
||||||
# Normalize the path and add index if we're looking at a directory
|
# Normalize the path and add index if we're looking at a directory
|
||||||
path = request.path
|
path = request.path
|
||||||
path << options.index_file if path.match(%r{/$})
|
path << options.index_file if path.match(%r{/$})
|
||||||
|
@ -90,33 +89,11 @@ module Middleman
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Copy, pasted & edited version of the setup in Sinatra.
|
@@inited = false
|
||||||
# Basically just changed the app's name and call out feature & configuration init.
|
|
||||||
def self.run!(options={}, &block)
|
|
||||||
init!
|
|
||||||
set options
|
|
||||||
handler = detect_rack_handler
|
|
||||||
handler_name = handler.name.gsub(/.*::/, '')
|
|
||||||
puts "== The Middleman is standing watch on port #{port}"
|
|
||||||
handler.run self, :Host => host, :Port => port do |server|
|
|
||||||
trap(:INT) do
|
|
||||||
## Use thins' hard #stop! if available, otherwise just #stop
|
|
||||||
server.respond_to?(:stop!) ? server.stop! : server.stop
|
|
||||||
puts "\n== The Middleman has ended his patrol"
|
|
||||||
end
|
|
||||||
|
|
||||||
if block_given?
|
|
||||||
block.call
|
|
||||||
## Use thins' hard #stop! if available, otherwise just #stop
|
|
||||||
server.respond_to?(:stop!) ? server.stop! : server.stop
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue Errno::EADDRINUSE => e
|
|
||||||
puts "== The Middleman is already standing watch on port #{port}!"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Require the features for this project
|
# Require the features for this project
|
||||||
def self.init!(quiet=false)
|
def self.init!(quiet=false, rerun=true)
|
||||||
|
return if @@inited && !rerun
|
||||||
|
|
||||||
# Built-in helpers
|
# Built-in helpers
|
||||||
require 'middleman/helpers'
|
require 'middleman/helpers'
|
||||||
helpers Middleman::Helpers
|
helpers Middleman::Helpers
|
||||||
|
@ -140,6 +117,8 @@ module Middleman
|
||||||
require "middleman/features/#{feature_name}"
|
require "middleman/features/#{feature_name}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@@inited = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
2
lib/middleman/config.ru
Normal file
2
lib/middleman/config.ru
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
require 'middleman'
|
||||||
|
run Middleman::Base
|
|
@ -16,6 +16,7 @@ module Middleman
|
||||||
if File.extname(path) == '.js' && File.exists?(source)
|
if File.extname(path) == '.js' && File.exists?(source)
|
||||||
secretary = ::Sprockets::Secretary.new( :asset_root => options.public,
|
secretary = ::Sprockets::Secretary.new( :asset_root => options.public,
|
||||||
:source_files => [source] )
|
:source_files => [source] )
|
||||||
|
# may need to rejigger since sprockets will only read views/ now
|
||||||
secretary.concatenation.to_s
|
secretary.concatenation.to_s
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
|
|
|
@ -18,14 +18,4 @@ class Templater::Actions::Template
|
||||||
browser.get(request_path)
|
browser.get(request_path)
|
||||||
browser.last_response.body
|
browser.last_response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def identical?
|
|
||||||
if File.exists?(destination)
|
|
||||||
extension = File.extname(source)
|
|
||||||
return true if !%w(.sass .js .haml).include?(extension) && File.exists?(source) && File.mtime(source) < File.mtime(destination)
|
|
||||||
File.read(destination) == render
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
Loading…
Reference in a new issue