class Falcon::Host

def app?

def app?
	@app || @config_path
end

def freeze

def freeze
	return if frozen?
	
	ssl_context
	
	super
end

def initialize

def initialize
	@app = nil
	@app_root = nil
	@config_path = "config.ru"
	
	@endpoint = nil
	
	@ssl_certificate = nil
	@ssl_key = nil
	
	@ssl_context = nil
end

def load_app(verbose = false)

def load_app(verbose = false)
	return @app if @app
	
	if @config_path
		rack_app, options = Rack::Builder.parse_file(@config_path)
		
		return Server.middleware(rack_app, verbose: verbose)
	end
end

def self_signed!(hostname)

def self_signed!(hostname)
	authority = Localhost::Authority.fetch(hostname)
	
	@ssl_context = authority.server_context.tap do |context|
		context.alpn_select_cb = lambda do |protocols|
			if protocols.include? "h2"
				return "h2"
			elsif protocols.include? "http/1.1"
				return "http/1.1"
			elsif protocols.include? "http/1.0"
				return "http/1.0"
			else
				return nil
			end
		end
		
		context.session_id_context = "falcon"
	end
end

def ssl_certificate_path= path

def ssl_certificate_path= path
	@ssl_certificate = OpenSSL::X509::Certificate.new(File.read(path))
end

def ssl_context

def ssl_context
	@ssl_context ||= OpenSSL::SSL::SSLContext.new.tap do |context|
		context.cert = @ssl_certificate
		context.key = @ssl_key
		
		context.session_id_context = "falcon"
		
		context.set_params
		
		context.setup
	end
end

def ssl_key_path= path

def ssl_key_path= path
	@ssl_key = OpenSSL::PKey::RSA.new(File.read(path))
end

def start(*args)

def start(*args)
	if self.app?
		Async::Container::Forked.new do
			Dir.chdir(@app_root) if @app_root
			
			app = self.load_app(*args)
			
			server = Falcon::Server.new(app, self.server_endpoint)
			
			server.run
		end
	end
end