class Bunto::Commands::Serve
Constants
- COMMAND_OPTIONS
Public Class Methods
init_with_program(prog)
click to toggle source
# File lib/bunto/commands/serve.rb, line 20 def init_with_program(prog) prog.command(:serve) do |cmd| cmd.description "Serve your site locally" cmd.syntax "serve [options]" cmd.alias :server cmd.alias :s add_build_options(cmd) COMMAND_OPTIONS.each do |key, val| cmd.option key, *val end cmd.action do |_, opts| opts["serving"] = true opts["watch" ] = true unless opts.key?("watch") config = opts["config"] opts["url"] = default_url(opts) if Bunto.env == "development" Build.process(opts) opts["config"] = config Serve.process(opts) end end end
process(opts)
click to toggle source
# File lib/bunto/commands/serve.rb, line 46 def process(opts) opts = configuration_from_options(opts) destination = opts["destination"] setup(destination) start_up_webrick(opts, destination) end
Private Class Methods
boot_or_detach(server, opts)
click to toggle source
Keep in our area with a thread or detach the server as requested by the user. This method determines what we do based on what you ask us to do.
# File lib/bunto/commands/serve.rb, line 175 def boot_or_detach(server, opts) if opts["detach"] pid = Process.fork do server.start end Process.detach(pid) Bunto.logger.info "Server detached with pid '#{pid}'.", \ "Run `pkill -f bunto' or `kill -9 #{pid}' to stop the server." else t = Thread.new { server.start } trap("INT") { server.shutdown } t.join end end
create_error_page()
click to toggle source
# File lib/bunto/commands/serve.rb, line 65 def create_error_page @header["Content-Type"] = "text/html; charset=UTF-8" @body = IO.read(File.join(@config[:DocumentRoot], "404.html")) end
default_url(opts)
click to toggle source
# File lib/bunto/commands/serve.rb, line 149 def default_url(opts) config = configuration_from_options(opts) format_url( config["ssl_cert"] && config["ssl_key"], config["host"] == "127.0.0.1" ? "localhost" : config["host"], config["port"] ) end
enable_logging(opts)
click to toggle source
Make the stack verbose if the user requests it.
# File lib/bunto/commands/serve.rb, line 194 def enable_logging(opts) opts[:AccessLog] = [] level = WEBrick::Log.const_get(opts[:BuntoOptions]["verbose"] ? :DEBUG : :WARN) opts[:Logger] = WEBrick::Log.new($stdout, level) end
enable_ssl(opts)
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/bunto/commands/serve.rb, line 206 def enable_ssl(opts) return if !opts[:BuntoOptions]["ssl_cert"] && !opts[:BuntoOptions]["ssl_key"] if !opts[:BuntoOptions]["ssl_cert"] || !opts[:BuntoOptions]["ssl_key"] # rubocop:disable Style/RedundantException raise RuntimeError, "--ssl-cert or --ssl-key missing." end require "openssl" require "webrick/https" source_key = Bunto.sanitized_path(opts[:BuntoOptions]["source"], \ opts[:BuntoOptions]["ssl_key" ]) source_certificate = Bunto.sanitized_path(opts[:BuntoOptions]["source"], \ opts[:BuntoOptions]["ssl_cert"]) opts[:SSLCertificate] = OpenSSL::X509::Certificate.new(File.read(source_certificate)) opts[:SSLPrivateKey ] = OpenSSL::PKey::RSA.new(File.read(source_key)) opts[:SSLEnable] = true end
file_handler_opts()
click to toggle source
Recreate NondisclosureName under utf-8 circumstance
# File lib/bunto/commands/serve.rb, line 115 def file_handler_opts WEBrick::Config::FileHandler.merge({ :FancyIndexing => true, :NondisclosureName => [ ".ht*", "~*", ], }) end
format_url(ssl_enabled, address, port, baseurl = nil)
click to toggle source
# File lib/bunto/commands/serve.rb, line 137 def format_url(ssl_enabled, address, port, baseurl = nil) format("%{prefix}://%{address}:%{port}%{baseurl}", { :prefix => ssl_enabled ? "https" : "http", :address => address, :port => port, :baseurl => baseurl ? "#{baseurl}/" : "", }) end
launch_browser(server, opts)
click to toggle source
# File lib/bunto/commands/serve.rb, line 161 def launch_browser(server, opts) address = server_address(server, opts) return system "start", address if Utils::Platforms.windows? return system "xdg-open", address if Utils::Platforms.linux? return system "open", address if Utils::Platforms.osx? Bunto.logger.error "Refusing to launch browser; " \ "Platform launcher unknown." end
mime_types()
click to toggle source
# File lib/bunto/commands/serve.rb, line 235 def mime_types file = File.expand_path("../mime.types", File.dirname(__FILE__)) WEBrick::HTTPUtils.load_mime_types(file) end
server_address(server, options = {})
click to toggle source
# File lib/bunto/commands/serve.rb, line 127 def server_address(server, options = {}) format_url( server.config[:SSLEnable], server.config[:BindAddress], server.config[:Port], options["baseurl"] ) end
setup(destination)
click to toggle source
Do a base pre-setup of WEBRick so that everything is in place when we get ready to party, checking for an setting up an error page and making sure our destination exists.
# File lib/bunto/commands/serve.rb, line 59 def setup(destination) require_relative "serve/servlet" FileUtils.mkdir_p(destination) if File.exist?(File.join(destination, "404.html")) WEBrick::HTTPResponse.class_eval do def create_error_page @header["Content-Type"] = "text/html; charset=UTF-8" @body = IO.read(File.join(@config[:DocumentRoot], "404.html")) end end end end
start_callback(detached)
click to toggle source
# File lib/bunto/commands/serve.rb, line 226 def start_callback(detached) unless detached proc do Bunto.logger.info("Server running...", "press ctrl-c to stop.") end end end
start_up_webrick(opts, destination)
click to toggle source
# File lib/bunto/commands/serve.rb, line 104 def start_up_webrick(opts, destination) server = WEBrick::HTTPServer.new(webrick_opts(opts)).tap { |o| o.unmount("") } server.mount(opts["baseurl"].to_s, Servlet, destination, file_handler_opts) Bunto.logger.info "Server address:", server_address(server, opts) launch_browser server, opts if opts["open_url"] boot_or_detach server, opts end
webrick_opts(opts)
click to toggle source
# File lib/bunto/commands/serve.rb, line 76 def webrick_opts(opts) opts = { :BuntoOptions => opts, :DoNotReverseLookup => true, :MimeTypes => mime_types, :DocumentRoot => opts["destination"], :StartCallback => start_callback(opts["detach"]), :BindAddress => opts["host"], :Port => opts["port"], :DirectoryIndex => %W( index.htm index.html index.rhtml index.cgi index.xml ), } opts[:DirectoryIndex] = [] if opts[:BuntoOptions]["show_dir_listing"] enable_ssl(opts) enable_logging(opts) opts end