class Falcon::Hosts

def add(host)

def add(host)
	@named[host.authority] = host
end

def each(&block)

def each(&block)
	@named.each(&block)
end

def endpoint

def endpoint
	@server_endpoint ||= Async::HTTP::Endpoint.parse(
		'https://[::]',
		ssl_context: self.ssl_context,
		reuse_address: true
	)
end

def host_context(socket, hostname)

def host_context(socket, hostname)
	if host = @named[hostname]
		Async.logger.debug(self) {"Resolving #{hostname} -> #{host}"}
		
		socket.hostname = hostname
		
		return host.ssl_context
	else
		Async.logger.warn(self) {"Unable to resolve #{hostname}!"}
		
		return nil
	end
end

def initialize(configuration)

def initialize(configuration)
	@named = {}
	@server_context = nil
	@server_endpoint = nil
	
	configuration.each do |environment|
		add(Host.new(environment))
	end
end

def proxy

def proxy
	Proxy.new(Falcon::BadRequest, @named)
end

def redirection(secure_endpoint)

def redirection(secure_endpoint)
	Redirection.new(Falcon::BadRequest, @named, secure_endpoint)
end

def run(container = Async::Container::Forked.new, **options)

def run(container = Async::Container::Forked.new, **options)
	@named.each do |name, host|
		host.run(container)
	end
	
	secure_endpoint = Async::HTTP::Endpoint.parse(options[:bind_secure], ssl_context: self.ssl_context)
	insecure_endpoint = Async::HTTP::Endpoint.parse(options[:bind_insecure])
	
	container.run(count: 1, name: "Falcon Proxy") do |task, instance|
		proxy = self.proxy
		
		proxy_server = Falcon::Server.new(proxy, secure_endpoint)
		
		proxy_server.run
	end
	
	container.run(count: 1, name: "Falcon Redirector") do |task, instance|
		redirection = self.redirection(secure_endpoint)
		
		redirection_server = Falcon::Server.new(redirection, insecure_endpoint)
		
		redirection_server.run
	end
	
	return container
end

def ssl_context

def ssl_context
	@server_context ||= OpenSSL::SSL::SSLContext.new.tap do |context|
		context.servername_cb = Proc.new do |socket, hostname|
			self.host_context(socket, hostname)
		end
		
		context.session_id_context = "falcon"
		context.alpn_protocols = DEFAULT_ALPN_PROTOCOLS
		context.set_params
		
		context.setup
	end
end