middleman/middleman-core/lib/middleman-core/preview_server.rb

265 lines
7.1 KiB
Ruby
Raw Normal View History

require 'webrick'
require 'webrick/https'
require 'openssl'
2015-05-25 03:49:21 +02:00
require 'socket'
2012-10-13 22:12:47 +02:00
require 'middleman-core/meta_pages'
require 'middleman-core/logger'
require 'middleman-core/rack'
2014-07-02 20:05:57 +02:00
# rubocop:disable GlobalVars
module Middleman
module PreviewServer
class << self
extend Forwardable
attr_reader :app, :host, :port, :ssl_certificate, :ssl_private_key
def_delegator :app, :logger
def https?
@https
end
# Start an instance of Middleman::Application
# @return [void]
2012-09-13 19:13:57 +02:00
def start(opts={})
@options = opts
mount_instance(new_app)
2014-03-12 10:05:43 +01:00
logger.info "== The Middleman is standing watch at #{uri}"
logger.info "== Inspect your site configuration at #{uri + '__middleman'}"
@initialized ||= false
2014-07-02 19:11:52 +02:00
return if @initialized
@initialized = true
2014-07-02 19:11:52 +02:00
register_signal_handlers
2014-07-02 19:11:52 +02:00
# Save the last-used @options so it may be re-used when
# reloading later on.
::Middleman::Profiling.report('server_start')
2014-07-02 19:11:52 +02:00
loop do
@webrick.start
2014-07-02 19:11:52 +02:00
# $mm_shutdown is set by the signal handler
if $mm_shutdown
shutdown
exit
elsif $mm_reload
$mm_reload = false
reload
end
end
end
# Detach the current Middleman::Application instance
# @return [void]
def stop
begin
logger.info '== The Middleman is shutting down'
rescue
# if the user closed their terminal STDOUT/STDERR won't exist
end
unmount_instance
end
# Simply stop, then start the server
# @return [void]
def reload
logger.info '== The Middleman is reloading'
begin
app = new_app
2014-04-29 19:50:21 +02:00
rescue => e
logger.error "Error reloading Middleman: #{e}\n#{e.backtrace.join("\n")}"
logger.info '== The Middleman is still running the application from before the error'
return
end
2012-09-13 19:13:57 +02:00
unmount_instance
2015-05-02 22:48:47 +02:00
@webrick.shutdown
@webrick = nil
mount_instance(app)
logger.info '== The Middleman has reloaded'
end
# Stop the current instance, exit Webrick
# @return [void]
def shutdown
stop
@webrick.shutdown
end
2014-04-29 19:50:21 +02:00
private
2014-04-29 19:44:24 +02:00
2012-09-13 19:13:57 +02:00
def new_app
opts = @options.dup
::Middleman::Logger.singleton(
opts[:debug] ? 0 : 1,
opts[:instrumenting] || false
)
app = ::Middleman::Application.new do
config[:environment] = opts[:environment].to_sym if opts[:environment]
2014-07-16 03:01:45 +02:00
config[:watcher_disable] = opts[:disable_watcher]
config[:watcher_force_polling] = opts[:force_polling]
config[:watcher_latency] = opts[:latency]
config[:host] = opts[:host] if opts[:host]
config[:port] = opts[:port] if opts[:port]
config[:ssl_certificate] = opts[:ssl_certificate] if opts[:ssl_certificate]
config[:ssl_private_key] = opts[:ssl_private_key] if opts[:ssl_private_key]
2014-07-16 03:01:45 +02:00
ready do
match_against = [
%r{^config\.rb$},
%r{^environments/[^\.](.*)\.rb$},
%r{^lib/[^\.](.*)\.rb$},
%r{^#{@app.config[:helpers_dir]}/[^\.](.*)\.rb$}
]
# config.rb
watcher = files.watch :reload,
2015-09-17 18:41:17 +02:00
path: root,
only: match_against
# Hack around node_modules in root.
watcher.listener.ignore(/^node_modules/)
2015-08-17 23:33:19 +02:00
# Hack around sass cache in root.
watcher.listener.ignore(/^\.sass-cache/)
2015-08-18 00:59:48 +02:00
# Hack around bundler cache in root.
watcher.listener.ignore(/^vendor\/bundle/)
2014-07-16 03:01:45 +02:00
end
end
2015-04-26 20:32:47 +02:00
@host = app.config[:host]
@port = app.config[:port]
2015-05-04 19:02:32 +02:00
@https = app.config[:https]
2015-05-04 19:02:32 +02:00
@ssl_certificate = app.config[:ssl_certificate]
@ssl_private_key = app.config[:ssl_private_key]
2014-12-23 23:54:21 +01:00
app.files.on_change :reload do
2014-07-16 03:01:45 +02:00
$mm_reload = true
@webrick.stop
end
# Add in the meta pages application
meta_app = Middleman::MetaPages::Application.new(app)
app.map '/__middleman' do
run meta_app
end
app
2012-09-13 19:13:57 +02:00
end
# Trap some interupt signals and shut down smoothly
# @return [void]
def register_signal_handlers
%w(INT HUP TERM QUIT).each do |sig|
2014-07-02 19:11:52 +02:00
next unless Signal.list[sig]
Signal.trap(sig) do
# Do as little work as possible in the signal context
$mm_shutdown = true
2014-07-02 20:05:57 +02:00
2014-07-02 19:11:52 +02:00
@webrick.stop
end
end
end
# Initialize webrick
# @return [void]
def setup_webrick(is_logging)
http_opts = {
2014-04-29 19:50:21 +02:00
BindAddress: host,
Port: port,
AccessLog: [],
DoNotReverseLookup: true
}
if https?
http_opts[:SSLEnable] = true
if ssl_certificate || ssl_private_key
2015-05-04 02:11:49 +02:00
raise 'You must provide both :ssl_certificate and :ssl_private_key' unless ssl_private_key && ssl_certificate
http_opts[:SSLCertificate] = OpenSSL::X509::Certificate.new File.read ssl_certificate
http_opts[:SSLPrivateKey] = OpenSSL::PKey::RSA.new File.read ssl_private_key
else
# use a generated self-signed cert
http_opts[:SSLCertName] = [
2015-05-04 02:11:49 +02:00
%w(CN localhost),
%w(CN #{host})
].uniq
end
end
if is_logging
http_opts[:Logger] = FilteredWebrickLog.new
else
http_opts[:Logger] = ::WEBrick::Log.new(nil, 0)
end
2012-09-13 19:13:57 +02:00
begin
::WEBrick::HTTPServer.new(http_opts)
rescue Errno::EADDRINUSE
2015-05-25 03:49:21 +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=#{unused_tcp_port}"
2012-09-13 19:13:57 +02:00
exit(1)
end
end
# Attach a new Middleman::Application instance
# @param [Middleman::Application] app
# @return [void]
def mount_instance(app)
@app = app
@webrick ||= setup_webrick(@options[:debug] || false)
rack_app = ::Middleman::Rack.new(@app).to_app
@webrick.mount '/', ::Rack::Handler::WEBrick, rack_app
end
# Detach the current Middleman::Application instance
# @return [void]
def unmount_instance
@webrick.unmount '/'
2014-07-16 03:01:45 +02:00
@app.shutdown!
2014-07-16 03:01:45 +02:00
@app = nil
end
2014-03-12 10:05:43 +01:00
# Returns the URI the preview server will run on
# @return [URI]
def uri
host = (@host == '0.0.0.0') ? 'localhost' : @host
scheme = https? ? 'https' : 'http'
URI("#{scheme}://#{host}:#{@port}")
2014-03-12 10:05:43 +01:00
end
2015-05-25 03:49:21 +02:00
# Returns unused TCP port
# @return [Fixnum]
def unused_tcp_port
server = TCPServer.open(0)
port = server.addr[1]
server.close
port
end
end
class FilteredWebrickLog < ::WEBrick::Log
def log(level, data)
2014-07-02 19:11:52 +02:00
super(level, data) unless data =~ %r{Could not determine content-length of response body.}
end
end
end
end