2012-07-09 09:16:13 +02:00
require " webrick "
2012-10-13 22:12:47 +02:00
require 'middleman-core/meta_pages'
2012-07-09 09:16:13 +02:00
2012-05-20 01:49:44 +02:00
module Middleman
module PreviewServer
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
DEFAULT_PORT = 4567
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
class << self
2012-11-11 03:16:47 +01:00
attr_reader :app , :host , :port
2012-07-15 20:04:45 +02:00
delegate :logger , :to = > :app
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
# Start an instance of Middleman::Application
# @return [void]
2012-09-13 19:13:57 +02:00
def start ( opts = { } )
@options = opts
2012-11-11 05:54:32 +01:00
@host = @options [ :host ] || Socket . gethostname
2012-09-13 19:13:57 +02:00
@port = @options [ :port ] || DEFAULT_PORT
2012-08-14 00:39:06 +02:00
2012-09-13 19:13:57 +02:00
mount_instance
2012-11-11 03:16:47 +01:00
logger . info " == The Middleman is standing watch at http:// #{ host } : #{ port } "
2013-04-13 08:30:19 +02:00
logger . info " == Inspect your site configuration at http:// #{ host } : #{ port } /__middleman/ "
2012-05-20 08:17:13 +02:00
@initialized || = false
unless @initialized
@initialized = true
2012-08-14 00:39:06 +02:00
2012-09-28 08:02:59 +02:00
register_signal_handlers
2012-08-14 00:39:06 +02:00
2012-09-13 19:13:57 +02:00
# Save the last-used @options so it may be re-used when
2012-06-16 00:20:40 +02:00
# reloading later on.
2012-07-19 07:10:02 +02:00
:: Middleman :: Profiling . report ( " server_start " )
2012-05-20 01:49:44 +02:00
@webrick . start
2013-03-03 21:31:42 +01:00
# $mm_shutdown is set by the signal handler
if $mm_shutdown
shutdown
exit
end
2012-05-20 01:49:44 +02:00
end
end
# Detach the current Middleman::Application instance
# @return [void]
def stop
2012-09-28 08:02:59 +02:00
begin
logger . info " == The Middleman is shutting down "
rescue
# if the user closed their terminal STDOUT/STDERR won't exist
end
2012-06-28 08:27:27 +02:00
if @listener
@listener . stop
@listener = nil
end
2012-05-20 01:49:44 +02:00
unmount_instance
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
# Simply stop, then start the server
# @return [void]
def reload
2012-09-13 19:13:57 +02:00
logger . info " == The Middleman is reloading "
2012-11-11 03:16:47 +01:00
2012-09-13 19:13:57 +02:00
unmount_instance
mount_instance
2012-11-11 03:16:47 +01:00
logger . info " == The Middleman is standing watch at http:// #{ host } : #{ port } "
2012-05-20 01:49:44 +02:00
end
# Stop the current instance, exit Webrick
# @return [void]
def shutdown
stop
@webrick . shutdown
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
private
2012-09-13 19:13:57 +02:00
def new_app
opts = @options
2013-04-13 07:48:56 +02:00
server = :: Middleman :: Application . server
# Add in the meta pages application
meta_app = Middleman :: MetaPages :: Application . new ( server )
server . map '/__middleman' do
run meta_app
end
@app = server . inst do
2012-09-13 19:13:57 +02:00
if opts [ :environment ]
2012-10-14 04:54:55 +02:00
config [ :environment ] = opts [ :environment ] . to_sym
2012-09-13 19:13:57 +02:00
end
2012-11-11 03:16:47 +01:00
2012-09-13 19:13:57 +02:00
logger ( opts [ :debug ] ? 0 : 1 , opts [ :instrumenting ] || false )
end
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
def start_file_watcher
2013-01-13 20:56:53 +01:00
return if @options [ :disable_watcher ]
2012-11-11 03:16:47 +01:00
2012-09-13 19:51:16 +02:00
first_run = ! @listener
2012-11-11 03:16:47 +01:00
2012-09-13 19:51:16 +02:00
if first_run
# Watcher Library
require " listen "
2013-01-13 20:56:53 +01:00
@listener = Listen . to ( Dir . pwd , :relative_paths = > true , :force_polling = > @options [ :force_polling ] )
2013-08-20 14:36:51 +02:00
@listener . latency ( @options [ :latency ] )
2012-09-13 19:51:16 +02:00
end
2012-11-11 03:16:47 +01:00
2012-06-28 08:27:27 +02:00
@listener . change do | modified , added , removed |
2012-05-20 01:49:44 +02:00
added_and_modified = ( modified + added )
2012-09-13 19:51:16 +02:00
# See if the changed file is config.rb or lib/*.rb
2013-03-22 18:28:38 +01:00
if needs_to_reload? ( added_and_modified + removed )
2012-09-13 19:51:16 +02:00
reload
else
2012-05-20 08:05:06 +02:00
added_and_modified . each do | path |
2012-09-13 19:13:57 +02:00
app . files . did_change ( path )
2012-05-20 01:49:44 +02:00
end
removed . each do | path |
2012-09-13 19:13:57 +02:00
app . files . did_delete ( path )
2012-05-20 01:49:44 +02:00
end
end
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
# Don't block this thread
2013-05-03 20:40:30 +02:00
@listener . start if first_run
2012-05-20 01:49:44 +02:00
end
2012-08-14 00:39:06 +02:00
2012-09-28 08:02:59 +02:00
# Trap some interupt signals and shut down smoothly
2012-05-20 01:49:44 +02:00
# @return [void]
def register_signal_handlers
2012-09-28 08:02:59 +02:00
%w( INT HUP TERM QUIT ) . each do | sig |
if Signal . list [ sig ]
Signal . trap ( sig ) do
2013-03-03 21:31:42 +01:00
# Do as little work as possible in the signal context
$mm_shutdown = true
@webrick . stop
2012-09-28 08:02:59 +02:00
end
end
end
2012-05-20 01:49:44 +02:00
end
2012-08-14 00:39:06 +02:00
# Initialize webrick
2012-05-20 01:49:44 +02:00
# @return [void]
2012-11-11 03:16:47 +01:00
def setup_webrick ( is_logging )
2012-05-20 01:49:44 +02:00
http_opts = {
2012-11-11 03:16:47 +01:00
:BindAddress = > host ,
2012-09-13 19:13:57 +02:00
:Port = > port ,
2012-05-20 01:49:44 +02:00
:AccessLog = > [ ]
}
2012-08-14 00:39:06 +02:00
2012-07-09 09:16:13 +02:00
if is_logging
http_opts [ :Logger ] = FilteredWebrickLog . new
else
http_opts [ :Logger ] = :: WEBrick :: Log . new ( nil , 0 )
2012-05-20 01:49:44 +02:00
end
2012-08-14 00:39:06 +02:00
2012-09-13 19:13:57 +02:00
begin
:: WEBrick :: HTTPServer . new ( http_opts )
2013-04-06 23:48:00 +02:00
rescue Errno :: EADDRINUSE
2012-09-13 19:13:57 +02:00
logger . error " == Port #{ port } is unavailable. Either close the instance of Middleman already running on #{ port } or start this Middleman on a new port with: --port= #{ port . to_i + 1 } "
exit ( 1 )
end
2012-05-20 01:49:44 +02:00
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
# Attach a new Middleman::Application instance
# @param [Middleman::Application] app
# @return [void]
2012-09-13 19:13:57 +02:00
def mount_instance
2012-09-28 08:02:59 +02:00
@app = new_app
2012-11-11 03:16:47 +01:00
@webrick || = setup_webrick ( @options [ :debug ] || false )
2012-09-13 19:51:16 +02:00
start_file_watcher
2013-03-22 18:28:38 +01:00
2012-10-13 22:12:47 +02:00
rack_app = app . class . to_rack_app
@webrick . mount " / " , :: Rack :: Handler :: WEBrick , rack_app
2012-05-20 01:49:44 +02:00
end
2012-08-14 00:39:06 +02:00
2012-05-20 01:49:44 +02:00
# Detach the current Middleman::Application instance
# @return [void]
def unmount_instance
@webrick . unmount " / "
@app = nil
end
# Whether the passed files are config.rb, lib/*.rb or helpers
# @param [Array<String>] paths Array of paths to check
# @return [Boolean] Whether the server needs to reload
def needs_to_reload? ( paths )
2012-09-13 19:13:57 +02:00
match_against = [
%r{ ^config \ .rb } ,
2013-03-22 18:22:16 +01:00
%r{ ^lib/[^ \ .](.*) \ .rb$ } ,
%r{ ^helpers/[^ \ .](.*) \ .rb$ }
2012-09-13 19:13:57 +02:00
]
2012-11-11 03:16:47 +01:00
2012-09-13 19:13:57 +02:00
if @options [ :reload_paths ]
@options [ :reload_paths ] . split ( ',' ) . each do | part |
match_against << %r{ ^ #{ part } }
end
end
2012-11-11 03:16:47 +01:00
2012-05-20 01:49:44 +02:00
paths . any? do | path |
2012-09-13 19:13:57 +02:00
match_against . any? do | matcher |
2013-02-09 09:20:37 +01:00
path =~ matcher
2012-09-13 19:13:57 +02:00
end
2012-05-20 01:49:44 +02:00
end
end
end
2012-07-09 09:16:13 +02:00
class FilteredWebrickLog < :: WEBrick :: Log
def log ( level , data )
unless data =~ %r{ Could not determine content-length of response body. }
super ( level , data )
end
end
end
2012-05-20 01:49:44 +02:00
end
2012-05-20 08:05:06 +02:00
end