class Falcon::Command::Serve

def client

def client
	Async::HTTP::Client.new(client_endpoint)
end

def client_endpoint

def client_endpoint
	Async::HTTP::URLEndpoint.parse(@options[:bind], **endpoint_options)
end

def container_class

def container_class
	case @options[:container]
	when :threaded
		require 'async/container/threaded'
		return Async::Container::Threaded
	when :forked
		require 'async/container/forked'
		return Async::Container::Forked
	end
end

def endpoint_options

def endpoint_options
	# Oh, for Hash#slice(keys...)
	options = {}
	
	if @options[:hostname]
		options[:hostname] = @options[:hostname]
	end
	
	if @options[:port]
		options[:port] = @options[:port]
	end
	
	return options
end

def invoke(parent)

def invoke(parent)
	container = run(parent.verbose?)
	
	container.wait
end

def load_app(verbose)

def load_app(verbose)
	rack_app, options = Rack::Builder.parse_file(@options[:config])
	
	return Server.middleware(rack_app, verbose: verbose), options
end

def run(verbose = false)

def run(verbose = false)
	app, options = load_app(verbose)
	
	endpoint = Endpoint.parse(@options[:bind], **endpoint_options)
	
	bound_endpoint = Async::Reactor.run do
		Async::IO::SharedEndpoint.bound(endpoint)
	end.result
	
	Async.logger.info "Falcon taking flight! Binding to #{endpoint} [#{container_class} with concurrency: #{@options[:concurrency]}]"
	
	debug_trap = Async::IO::Trap.new(:USR1)
	
	container_class.new(concurrency: @options[:concurrency]) do |task|
		task.async do
			debug_trap.install!
			Async.logger.info "Send `kill -USR1 #{Process.pid}` for detailed status :)"
			
			debug_trap.trap do
				task.reactor.print_hierarchy($stderr)
			end
		end
		
		server = Falcon::Server.new(app, bound_endpoint, endpoint.protocol)
		
		server.run
		
		task.children.each(&:wait)
	end
end