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.



114
115
116
117
118
119
120
121
122
123
# File 'lib/falcon/command/serve.rb', line 114

def call
	Console.logger.info(self) do |buffer|
		buffer.puts "Falcon v#{VERSION} taking flight! Using #{self.container_class} #{self.container_options}."
		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.



109
110
111
# File 'lib/falcon/command/serve.rb', line 109

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

#client_endpointObject

The endpoint suitable for a client to connect.



104
105
106
# File 'lib/falcon/command/serve.rb', line 104

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

#configurationObject



80
81
82
83
84
# File 'lib/falcon/command/serve.rb', line 80

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

#container_classObject

The container class to use.



87
88
89
90
91
92
93
94
95
96
# File 'lib/falcon/command/serve.rb', line 87

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



50
51
52
# File 'lib/falcon/command/serve.rb', line 50

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

#endpointObject

The endpoint to bind to.



99
100
101
# File 'lib/falcon/command/serve.rb', line 99

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

#endpoint_optionsObject



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

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

#environmentObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/falcon/command/serve.rb', line 58

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: "server",
		
		endpoint: ->{Endpoint.parse(url, **endpoint_options)}
	)
end