Class: Falcon::Command::Serve

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/falcon/command/serve.rb

Overview

Implements the ‘falcon serve` command. Designed for development.

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

Instance Method Summary collapse

Instance Method Details

#callObject

Prepare the environment and run the controller.



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/falcon/command/serve.rb', line 129

def call
	Console.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

#clientObject

Create a new client suitable for accessing the application.



124
125
126
# File 'lib/falcon/command/serve.rb', line 124

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

#client_endpointObject

The endpoint suitable for a client to connect.



119
120
121
# File 'lib/falcon/command/serve.rb', line 119

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

#configurationObject

Build the service configuration for the serve command.



95
96
97
98
99
# File 'lib/falcon/command/serve.rb', line 95

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

#container_classObject

The container class to use.



102
103
104
105
106
107
108
109
110
111
# File 'lib/falcon/command/serve.rb', line 102

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

#container_optionsObject

Extract container-related options from the command line options.



54
55
56
# File 'lib/falcon/command/serve.rb', line 54

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

#endpointObject

The endpoint to bind to.



114
115
116
# File 'lib/falcon/command/serve.rb', line 114

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

#endpoint_optionsObject

Extract endpoint-related options from the command line options.



60
61
62
# File 'lib/falcon/command/serve.rb', line 60

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

#environmentObject

Create the environment for the serve command.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/falcon/command/serve.rb', line 72

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

#nameObject

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



66
67
68
# File 'lib/falcon/command/serve.rb', line 66

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