FalconSourceFalconCommandServe

class Serve

Implements the falcon serve command. Designed for development.

Manages a Controller::Serve instance which is responsible for running applications in a development environment.

Nested

Definitions

def container_options

Extract container-related options from the command line options.

Signature

returns Hash

Options for container configuration.

Implementation

def container_options
	@options.slice(:count, :forks, :threads, :restart, :health_check_timeout)
end

def endpoint_options

Extract endpoint-related options from the command line options.

Signature

returns Hash

Options for endpoint configuration.

Implementation

def endpoint_options
	@options.slice(:hostname, :port, :timeout)
end

def name

Get the name for the service, using hostname if available, otherwise the bind address.

Signature

returns String

The service name.

Implementation

def name
	@options[:hostname] || @options[:bind]
end

def environment

Create the environment for the serve command.

Signature

returns Async::Service::Environment

The configured server environment.

Implementation

def environment
	Async::Service::Environment.new(Falcon::Environment::Server).with(
		Falcon::Environment::Rackup,
		root: Dir.pwd,
		
		verbose: self.parent&.verbose?,
		cache: @options[:cache],
		
		container_options: self.container_options,
		endpoint_options: self.endpoint_options,
		
		rackup_path: @options[:config],
		preload: [@options[:preload]].compact,
		url: @options[:bind],
		
		name: self.name,
		
		endpoint: ->{Endpoint.parse(url, **endpoint_options)}
	)
end

def configuration

Build the service configuration for the serve command.

Signature

returns Async::Service::Configuration

The service configuration.

Implementation

def configuration
	Async::Service::Configuration.new.tap do |configuration|
		configuration.add(self.environment)
	end
end

def container_class

The container class to use.

Implementation

def container_class
	case @options[:container]
	when :threaded
		return Async::Container::Threaded
	when :forked
		return Async::Container::Forked
	when :hybrid
		return Async::Container::Hybrid
	end
end

def endpoint

The endpoint to bind to.

Implementation

def endpoint
	Endpoint.parse(@options[:bind], **endpoint_options)
end

def client_endpoint

The endpoint suitable for a client to connect.

Implementation

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

def client

Create a new client suitable for accessing the application.

Implementation

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

def call

Prepare the environment and run the controller.

Implementation

def call
	Console.logger.info(self) do |buffer|
		buffer.puts "Falcon v#{VERSION} taking flight! Using #{self.container_class} #{self.container_options}."
		buffer.puts "- Running on #{RUBY_DESCRIPTION}"
		buffer.puts "- Binding to: #{self.endpoint}"
		buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}"
		buffer.puts "- To reload configuration: kill -HUP #{Process.pid}"
	end
	
	Async::Service::Controller.run(self.configuration, container_class: self.container_class, graceful_stop: @options[:graceful_stop])
end