class Falcon::Controller::Serve

A generic controller for serving an application. Uses {Server} for handling incoming requests.

Public Class Methods

new(command, **options) click to toggle source

Initialize the server controller. @parameter command [Command::Serve] The user-specified command-line options.

Calls superclass method
# File lib/falcon/controller/serve.rb, line 37
def initialize(command, **options)
        @command = command
        
        @endpoint = nil
        @bound_endpoint = nil
        @debug_trap = Async::IO::Trap.new(:USR1)
        
        super(**options)
end

Public Instance Methods

create_container() click to toggle source

Create the controller as specified by the command. e.g. `Async::Container::Forked`.

# File lib/falcon/controller/serve.rb, line 49
def create_container
        @command.container_class.new
end
endpoint() click to toggle source

The endpoint the server will bind to.

# File lib/falcon/controller/serve.rb, line 54
def endpoint
        @command.endpoint
end
load_app() click to toggle source

@returns [Protocol::HTTP::Middleware] an instance of the application to be served.

# File lib/falcon/controller/serve.rb, line 59
def load_app
        @command.load_app
end
name() click to toggle source

The name of the controller which is used for the process title.

# File lib/falcon/controller/serve.rb, line 79
def name
        "Falcon Server"
end
setup(container) click to toggle source

Setup the container with the application instance. @parameter container [Async::Container::Generic]

# File lib/falcon/controller/serve.rb, line 85
def setup(container)
        container.run(name: self.name, restart: true, **@command.container_options) do |instance|
                Async do |task|
                        # Load one app instance per container:
                        app = self.load_app
                        
                        task.async do
                                if @debug_trap.install!
                                        Console.logger.info(instance) do
                                                "- Per-process status: kill -USR1 #{Process.pid}"
                                        end
                                end
                                
                                @debug_trap.trap do
                                        Console.logger.info(self) do |buffer|
                                                task.reactor.print_hierarchy(buffer)
                                        end
                                end
                        end
                        
                        server = Falcon::Server.new(app, @bound_endpoint, protocol: @endpoint.protocol, scheme: @endpoint.scheme)
                        
                        server.run
                        
                        instance.ready!
                        
                        task.children.each(&:wait)
                end
        end
end
start() click to toggle source

Prepare the bound endpoint for the server.

Calls superclass method
# File lib/falcon/controller/serve.rb, line 64
def start
        @endpoint ||= self.endpoint
        
        @bound_endpoint = Async do
                Async::IO::SharedEndpoint.bound(@endpoint)
        end.wait
        
        Console.logger.info(self) { "Starting #{name} on #{@endpoint.to_url}" }
        
        @debug_trap.ignore!
        
        super
end
stop(*) click to toggle source

Close the bound endpoint.

Calls superclass method
# File lib/falcon/controller/serve.rb, line 117
def stop(*)
        @bound_endpoint&.close
        
        @debug_trap.default!
        
        super
end