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.



111
112
113
114
115
116
117
118
119
120
# File 'lib/falcon/command/serve.rb', line 111

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)
end

#clientObject

Create a new client suitable for accessing the application.



106
107
108
# File 'lib/falcon/command/serve.rb', line 106

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

#client_endpointObject

The endpoint suitable for a client to connect.



101
102
103
# File 'lib/falcon/command/serve.rb', line 101

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

#configurationObject



77
78
79
80
81
# File 'lib/falcon/command/serve.rb', line 77

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

#container_classObject

The container class to use.



84
85
86
87
88
89
90
91
92
93
# File 'lib/falcon/command/serve.rb', line 84

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



47
48
49
# File 'lib/falcon/command/serve.rb', line 47

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

#endpointObject

The endpoint to bind to.



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

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

#endpoint_optionsObject



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

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

#environmentObject



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

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