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)


76
77
78
# File 'lib/falcon/command/serve.rb', line 76

def cache?
	@options[:cache]
end

#callObject

Prepare the environment and run the controller.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/falcon/command/serve.rb', line 120

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.



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

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

#client_endpointObject

The endpoint suitable for a client to connect.



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

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

#container_classObject

The container class to use.



57
58
59
60
61
62
63
64
65
66
# File 'lib/falcon/command/serve.rb', line 57

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.



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

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

#controllerObject

Prepare a new controller for the command.



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

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

#endpointObject

The endpoint to bind to.



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

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

#endpoint_optionsObject

Options for the #endpoint.



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

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

#load_appObject

Load the rack application from the specified configuration path.



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

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)


70
71
72
# File 'lib/falcon/command/serve.rb', line 70

def verbose?
	@parent&.verbose?
end