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 Falcon::Controller::Serve instance which is responsible for running applications in a development environment.

Instance Method Summary collapse

Instance Method Details

#cache?Boolean

Whether to enable the application HTTP cache.

Returns:

  • (Boolean)


90
91
92
# File 'lib/falcon/command/serve.rb', line 90

def cache?
	@options[:cache]
end

#callObject

Prepare the environment and run the controller.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/falcon/command/serve.rb', line 134

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
	
	if path = @options[:preload]
		full_path = File.expand_path(path)
		load(full_path)
	end
	
	begin
		Bundler.require(:preload)
	rescue Bundler::GemfileNotFound
		# Ignore.
	end
	
	if GC.respond_to?(:compact)
		GC.compact
	end
	
	self.controller.run
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

#container_classObject

The container class to use.



71
72
73
74
75
76
77
78
79
80
# File 'lib/falcon/command/serve.rb', line 71

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

Options for the container. See Falcon::Controller::Serve#setup.



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

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

#controllerObject

Prepare a new controller for the command.



129
130
131
# File 'lib/falcon/command/serve.rb', line 129

def controller
	Controller::Serve.new(self)
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

Options for the #endpoint.



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

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

#load_appObject

Load the rack application from the specified configuration path.



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

def load_app
	rack_app, _ = Rack::Builder.parse_file(@options[:config])
	
	return Server.middleware(rack_app, verbose: self.verbose?, cache: self.cache?)
end

#verbose?Boolean

Whether verbose logging is enabled.

Returns:

  • (Boolean)


84
85
86
# File 'lib/falcon/command/serve.rb', line 84

def verbose?
	@parent&.verbose?
end