class Falcon::Controller::Virtual

A controller which mananages several virtual hosts. Spawns instances of {Proxy} and {Redirect} to handle incoming requests.

A virtual host is an application bound to a specific authority (essentially a hostname). The virtual controller manages multiple hosts and allows a single server to host multiple applications easily.

Public Class Methods

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

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

Calls superclass method
# File lib/falcon/controller/virtual.rb, line 34
def initialize(command, **options)
        @command = command
        
        super(**options)
        
        trap(SIGHUP, &self.method(:reload))
end

Public Instance Methods

assume_privileges(path) click to toggle source

Drop privileges according to the user and group of the specified path. @parameter path [String] The path to the application directory.

# File lib/falcon/controller/virtual.rb, line 44
def assume_privileges(path)
        stat = File.stat(path)
        
        Process::GID.change_privilege(stat.gid)
        Process::UID.change_privilege(stat.uid)
        
        home = Etc.getpwuid(stat.uid).dir
        
        return {
                'HOME' => home,
        }
end
falcon_path() click to toggle source

The path to the falcon executable from this gem. @returns [String]

# File lib/falcon/controller/virtual.rb, line 73
def falcon_path
        File.expand_path("../../../bin/falcon", __dir__)
end
setup(container) click to toggle source

Setup the container with {Redirect} and {Proxy} child processes. These processes are gracefully restarted if they are already running. @parameter container [Async::Container::Generic]

# File lib/falcon/controller/virtual.rb, line 80
def setup(container)
        if proxy = container[:proxy]
                proxy.kill(:HUP)
        end
        
        if redirect = container[:redirect]
                redirect.kill(:HUP)
        end
        
        container.reload do
                @command.resolved_paths do |path|
                        path = File.expand_path(path)
                        root = File.dirname(path)
                        
                        spawn(path, container, chdir: root)
                end
                
                container.spawn(name: "Falcon Redirector", restart: true, key: :redirect) do |instance|
                        instance.exec(falcon_path, "redirect",
                                "--bind", @command.bind_insecure,
                                "--timeout", @command.timeout.to_s,
                                "--redirect", @command.bind_secure,
                                *@command.paths, ready: false
                        )
                end
                
                container.spawn(name: "Falcon Proxy", restart: true, key: :proxy) do |instance|
                        instance.exec(falcon_path, "proxy",
                                "--bind", @command.bind_secure,
                                "--timeout", @command.timeout.to_s,
                                *@command.paths, ready: false
                        )
                end
        end
end
spawn(path, container, **options) click to toggle source

Spawn an application instance from the specified path. @parameter path [String] The path to the application directory. @parameter container [Async::Container::Generic] The container to spawn into. @parameter options [Options] The options which are passed to `exec`.

# File lib/falcon/controller/virtual.rb, line 61
def spawn(path, container, **options)
        container.spawn(name: "Falcon Application", restart: true, key: path) do |instance|
                env = assume_privileges(path)
                
                instance.exec(env,
                        "bundle", "exec", "--keep-file-descriptors",
                        path, ready: false, **options)
        end
end