class Falcon::Command::Serve

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 invoke(parent)

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

def load_app(verbose)

def load_app(verbose)
	app, options = Rack::Builder.parse_file(@options[:config])
	
	# We adapt the rack app to Async::HTTP::Middleware
	app = Adapters::Rack.new(app)
	
	app =  Async::HTTP::ContentEncoding.new(app)
	
	if verbose
		app = Verbose.new(app)
	end
	
	return app, options
end

def run(verbose)

def run(verbose)
	app, options = load_app(verbose)
	
	endpoint = nil
	
	Async::Reactor.run do
		endpoint = Async::IO::SharedEndpoint.bound(
			Async::IO::Endpoint.parse(@options[:bind], reuse_port: true)
		)
	end
	
	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, endpoint)
		
		server.run
		
		task.children.each(&:wait)
	end
end