class Falcon::Service::Application

Implements an application server using an internal clear-text proxy.

def count

@returns [Integer | nil]
Number of instances to start.
def count
  @environment.evaluator.count
end

def initialize(environment)

def initialize(environment)
	super
	
	@bound_endpoint = nil
end

def middleware

@returns [Protocol::HTTP::Middleware]
The middleware that will be served by this application.
def middleware
	# In a multi-threaded container, we don't want to modify the shared evaluator's cache, so we create a new evaluator:
	@environment.evaluator.middleware
end

def preload!

Preload any resources specified by the environment.
def preload!
	if scripts = @evaluator.preload
		scripts.each do |path|
			Console.logger.info(self) {"Preloading #{path}..."}
			full_path = File.expand_path(path, self.root)
			load(full_path)
		end
	end
end

def setup(container)

@parameter container [Async::Container::Generic]
Setup instances of the application into the container.
def setup(container)
	protocol = self.protocol
	scheme = self.scheme
	
	run_options = {
		name: self.name,
		restart: true,
	}
	
	run_options[:count] = count unless count.nil?
	
	container.run(**run_options) do |instance|
		Async do |task|
			Console.logger.info(self) {"Starting application server for #{self.root}..."}
			
			server = Server.new(self.middleware, @bound_endpoint, protocol: protocol, scheme: scheme)
			
			server.run
			
			instance.ready!
			
			task.children.each(&:wait)
		end
	end
	
	super
end

def start

Invoke {preload!} to load shared resources into the parent process.
Prepare the bound endpoint for the application instances.
def start
	Console.logger.info(self) {"Binding to #{self.endpoint}..."}
	
	@bound_endpoint = Async::Reactor.run do
		Async::IO::SharedEndpoint.bound(self.endpoint)
	end.wait
	
	preload!
	
	super
end

def stop

Close the bound endpoint.
def stop
	@bound_endpoint&.close
	@bound_endpoint = nil
	
	super
end