2013-12-28 00:26:31 +00:00
require 'webrick'
2012-10-13 13:12:47 -07:00
require 'middleman-core/meta_pages'
2013-12-31 14:41:17 -08:00
require 'middleman-core/logger'
2012-07-09 00:16:13 -07:00
2014-04-29 10:44:24 -07:00
# rubocop:disable GlobalVars
2012-05-19 16:49:44 -07:00
module Middleman
module PreviewServer
DEFAULT_PORT = 4567
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
class << self
2012-11-11 13:16:47 +11:00
attr_reader :app , :host , :port
2014-04-29 10:44:24 -07:00
delegate :logger , to : :app
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
# Start an instance of Middleman::Application
# @return [void]
2012-09-13 10:13:57 -07:00
def start ( opts = { } )
@options = opts
2014-04-14 10:34:53 -07:00
@host = @options [ :host ] || '0.0.0.0'
2012-09-13 10:13:57 -07:00
@port = @options [ :port ] || DEFAULT_PORT
2012-08-13 15:39:06 -07:00
2013-10-19 19:39:10 -07:00
mount_instance ( new_app )
2012-11-11 13:16:47 +11:00
logger . info " == The Middleman is standing watch at http:// #{ host } : #{ port } "
2013-04-12 23:30:19 -07:00
logger . info " == Inspect your site configuration at http:// #{ host } : #{ port } /__middleman/ "
2012-05-19 23:17:13 -07:00
@initialized || = false
2014-07-02 10:11:52 -07:00
return if @initialized
@initialized = true
2012-08-13 15:39:06 -07:00
2014-07-02 10:11:52 -07:00
register_signal_handlers
2012-08-13 15:39:06 -07:00
2014-07-02 10:11:52 -07:00
# Save the last-used @options so it may be re-used when
# reloading later on.
:: Middleman :: Profiling . report ( 'server_start' )
2012-07-18 22:10:02 -07:00
2014-07-02 10:11:52 -07:00
loop do
@webrick . start
2013-03-03 12:31:42 -08:00
2014-07-02 10:11:52 -07:00
# $mm_shutdown is set by the signal handler
if $mm_shutdown
shutdown
exit
elsif $mm_reload
$mm_reload = false
reload
2013-03-03 12:31:42 -08:00
end
2012-05-19 16:49:44 -07:00
end
end
# Detach the current Middleman::Application instance
# @return [void]
def stop
2012-09-27 23:02:59 -07:00
begin
2013-12-28 00:26:31 +00:00
logger . info '== The Middleman is shutting down'
2012-09-27 23:02:59 -07:00
rescue
# if the user closed their terminal STDOUT/STDERR won't exist
end
2012-06-27 23:27:27 -07:00
if @listener
@listener . stop
@listener = nil
end
2012-05-19 16:49:44 -07:00
unmount_instance
end
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
# Simply stop, then start the server
# @return [void]
def reload
2013-12-28 00:26:31 +00:00
logger . info '== The Middleman is reloading'
2012-11-11 13:16:47 +11:00
2013-10-19 19:39:10 -07:00
begin
app = new_app
2014-04-28 16:02:18 -07:00
rescue = > e
2013-10-19 19:39:10 -07:00
logger . error " Error reloading Middleman: #{ e } \n #{ e . backtrace . join ( " \n " ) } "
2013-12-28 00:26:31 +00:00
logger . info '== The Middleman is still running the application from before the error'
2013-10-19 19:39:10 -07:00
return
end
2012-09-13 10:13:57 -07:00
unmount_instance
2013-10-19 19:39:10 -07:00
mount_instance ( app )
2012-11-11 13:16:47 +11:00
2013-12-28 00:26:31 +00:00
logger . info '== The Middleman has reloaded'
2012-05-19 16:49:44 -07:00
end
# Stop the current instance, exit Webrick
# @return [void]
def shutdown
stop
@webrick . shutdown
end
2012-08-13 15:39:06 -07:00
2014-04-28 16:02:18 -07:00
private
2014-04-29 10:44:24 -07:00
2012-09-13 10:13:57 -07:00
def new_app
2013-12-31 14:41:17 -08:00
opts = @options . dup
2013-04-12 22:48:56 -07: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
2013-12-31 14:41:17 -08:00
:: Middleman :: Logger . singleton (
opts [ :debug ] ? 0 : 1 ,
opts [ :instrumenting ] || false
)
2014-07-02 10:11:52 -07:00
config [ :environment ] = opts [ :environment ] . to_sym if opts [ :environment ]
2012-09-13 10:13:57 -07:00
end
end
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
def start_file_watcher
2014-07-18 10:54:48 -07:00
return if @listener || @options [ :disable_watcher ]
2012-11-11 13:16:47 +11:00
2014-07-02 18:26:18 +01:00
# Watcher Library
require 'listen'
2012-11-11 13:16:47 +11:00
2014-07-18 10:54:48 -07:00
options = { force_polling : @options [ :force_polling ] }
2014-07-02 18:26:18 +01:00
options [ :latency ] = @options [ :latency ] if @options [ :latency ]
2012-11-11 13:16:47 +11:00
2014-07-02 18:26:18 +01:00
@listener = Listen . to ( Dir . pwd , options ) do | modified , added , removed |
2012-05-19 16:49:44 -07:00
added_and_modified = ( modified + added )
2012-09-13 10:51:16 -07: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 )
2013-10-19 19:39:10 -07:00
$mm_reload = true
@webrick . stop
2012-09-13 10:51:16 -07:00
else
2012-05-19 23:05:06 -07:00
added_and_modified . each do | path |
2014-07-02 18:26:18 +01:00
relative_path = Pathname ( path ) . relative_path_from ( Pathname ( Dir . pwd ) ) . to_s
next if app . files . ignored? ( relative_path )
app . files . did_change ( relative_path )
2012-05-19 16:49:44 -07:00
end
removed . each do | path |
2014-07-02 18:26:18 +01:00
relative_path = Pathname ( path ) . relative_path_from ( Pathname ( Dir . pwd ) ) . to_s
next if app . files . ignored? ( relative_path )
app . files . did_delete ( relative_path )
2012-05-19 16:49:44 -07:00
end
end
end
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
# Don't block this thread
2014-07-02 18:26:18 +01:00
@listener . start
2012-05-19 16:49:44 -07:00
end
2012-08-13 15:39:06 -07:00
2012-09-27 23:02:59 -07:00
# Trap some interupt signals and shut down smoothly
2012-05-19 16:49:44 -07:00
# @return [void]
def register_signal_handlers
2012-09-27 23:02:59 -07:00
%w( INT HUP TERM QUIT ) . each do | sig |
2014-07-02 10:11:52 -07:00
next unless Signal . list [ sig ]
Signal . trap ( sig ) do
# Do as little work as possible in the signal context
$mm_shutdown = true
@webrick . stop
2012-09-27 23:02:59 -07:00
end
end
2012-05-19 16:49:44 -07:00
end
2012-08-13 15:39:06 -07:00
# Initialize webrick
2012-05-19 16:49:44 -07:00
# @return [void]
2012-11-11 13:16:47 +11:00
def setup_webrick ( is_logging )
2012-05-19 16:49:44 -07:00
http_opts = {
2014-04-29 10:44:24 -07:00
BindAddress : host ,
Port : port ,
AccessLog : [ ] ,
DoNotReverseLookup : true
2012-05-19 16:49:44 -07:00
}
2012-08-13 15:39:06 -07:00
2012-07-09 00:16:13 -07:00
if is_logging
http_opts [ :Logger ] = FilteredWebrickLog . new
else
http_opts [ :Logger ] = :: WEBrick :: Log . new ( nil , 0 )
2012-05-19 16:49:44 -07:00
end
2012-08-13 15:39:06 -07:00
2012-09-13 10:13:57 -07:00
begin
:: WEBrick :: HTTPServer . new ( http_opts )
2013-04-06 14:48:00 -07:00
rescue Errno :: EADDRINUSE
2014-04-28 16:02:18 -07: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 } "
2012-09-13 10:13:57 -07:00
exit ( 1 )
end
2012-05-19 16:49:44 -07:00
end
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
# Attach a new Middleman::Application instance
# @param [Middleman::Application] app
# @return [void]
2013-10-19 19:39:10 -07:00
def mount_instance ( app )
@app = app
2012-09-27 23:02:59 -07:00
2012-11-11 13:16:47 +11:00
@webrick || = setup_webrick ( @options [ :debug ] || false )
2012-09-13 10:51:16 -07:00
start_file_watcher
2013-03-22 18:28:38 +01:00
2012-10-13 13:12:47 -07:00
rack_app = app . class . to_rack_app
2013-12-28 00:26:31 +00:00
@webrick . mount '/' , :: Rack :: Handler :: WEBrick , rack_app
2012-05-19 16:49:44 -07:00
end
2012-08-13 15:39:06 -07:00
2012-05-19 16:49:44 -07:00
# Detach the current Middleman::Application instance
# @return [void]
def unmount_instance
2013-12-28 00:26:31 +00:00
@webrick . unmount '/'
2012-05-19 16:49:44 -07:00
@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 )
2014-09-15 15:36:41 -07:00
relative_paths = paths . map do | p |
Pathname ( p ) . relative_path_from ( Pathname ( app . root ) ) . to_s
end
2012-09-13 10:13:57 -07:00
match_against = [
2014-09-15 15:36:41 -07:00
%r{ ^config \ .rb$ } ,
2013-03-22 18:22:16 +01:00
%r{ ^lib/[^ \ .](.*) \ .rb$ } ,
%r{ ^helpers/[^ \ .](.*) \ .rb$ }
2012-09-13 10:13:57 -07:00
]
2012-11-11 13:16:47 +11:00
2012-09-13 10:13:57 -07:00
if @options [ :reload_paths ]
@options [ :reload_paths ] . split ( ',' ) . each do | part |
match_against << %r{ ^ #{ part } }
end
end
2012-11-11 13:16:47 +11:00
2014-09-15 15:36:41 -07:00
relative_paths . any? do | path |
2012-09-13 10:13:57 -07:00
match_against . any? do | matcher |
2013-02-09 00:20:37 -08:00
path =~ matcher
2012-09-13 10:13:57 -07:00
end
2012-05-19 16:49:44 -07:00
end
end
end
2012-07-09 00:16:13 -07:00
class FilteredWebrickLog < :: WEBrick :: Log
def log ( level , data )
2014-07-02 10:11:52 -07:00
super ( level , data ) unless data =~ %r{ Could not determine content-length of response body. }
2012-07-09 00:16:13 -07:00
end
end
2012-05-19 16:49:44 -07:00
end
2012-05-19 23:05:06 -07:00
end