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)


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

def cache?
	@options[:cache]
end

#callObject

Prepare the environment and run the controller.



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

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.



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

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

#client_endpointObject

The endpoint suitable for a client to connect.



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

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

#container_classObject

The container class to use.



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

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.



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

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

#controllerObject

Prepare a new controller for the command.



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

def controller
	Controller::Serve.new(self)
end

#endpointObject

The endpoint to bind to.



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

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

#endpoint_optionsObject

Options for the #endpoint.



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

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

#load_appObject

Load the rack application from the specified configuration path.



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

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)


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

def verbose?
	@parent&.verbose?
end