lib/hx/cli.rb in hx-0.9.0 vs lib/hx/cli.rb in hx-0.10.0
- old
+ new
@@ -24,10 +24,14 @@
require 'hx'
require 'ostruct'
require 'optparse'
require 'pathname'
require 'tempfile'
+require 'webrick'
+require 'rack'
+require 'rack/handler/webrick'
+require 'hx/rack/application'
module Hx
module CLI
DEFAULT_CONFIG_FILENAME = "config.hx"
@@ -41,10 +45,11 @@
Usage: hx [--config CONFIG_FILE] [upgen]
hx [--config CONFIG_FILE] regen
hx [--config CONFIG_FILE] post[up] SOURCE:PATH
hx [--config CONFIG_FILE] edit[up] SOURCE:PATH
hx [--config CONFIG_FILE] list SOURCE:PATTERN
+ hx [--config CONFIG_FILE] serve [PORT]
EOS
opts.on("-c", "--config CONFIG_FILE",
"Use CONFIG_FILE instead of searching for " +
@@ -92,9 +97,21 @@
m = method(method_name)
rescue NameError
raise ArgumentError, "Unrecognized subcommand: #{subcommand}"
end
m.call(site, *args)
+end
+
+def self.cmd_serve(site, port=nil)
+ server = WEBrick::HTTPServer.new({:Port => port || 0})
+ real_port = server.config[:Port]
+ base_url = "http://localhost:#{real_port}/"
+ server.logger.info "Serving on #{base_url}"
+ site.options[:base_url] = base_url
+ app = Hx::Rack::Application.new(site, site.options)
+ server.mount('/', ::Rack::Handler::WEBrick, app)
+ %w(INT TERM).each { |s| trap(s) { server.shutdown } }
+ server.start
end
def self.cmd_regen(site)
do_gen(site, false)
end