Class: Falcon::Service::Virtual

Inherits:
Async::Service::Generic
  • Object
show all
Defined in:
lib/falcon/service/virtual.rb

Overview

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.

Defined Under Namespace

Modules: Environment

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



72
73
74
# File 'lib/falcon/service/virtual.rb', line 72

def self.included(target)
	target.include(Environnment)
end

Instance Method Details

#assume_privileges(path) ⇒ Object

Drop privileges according to the user and group of the specified path.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/falcon/service/virtual.rb', line 78

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_pathObject

The path to the falcon executable from this gem.



107
108
109
# File 'lib/falcon/service/virtual.rb', line 107

def falcon_path
	File.expand_path("../../../bin/falcon", __dir__)
end

#setup(container) ⇒ Object

Setup the container with Redirect and Proxy child processes. These processes are gracefully restarted if they are already running.



114
115
116
117
118
119
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
145
146
147
148
149
150
# File 'lib/falcon/service/virtual.rb', line 114

def setup(container)
	if proxy = container[:proxy]
		proxy.kill(:HUP)
	end
	
	if redirect = container[:redirect]
		redirect.kill(:HUP)
	end
	
	container.reload do
		evaluator = @environment.evaluator
		
		evaluator.configuration_paths.each 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", evaluator.bind_insecure,
				"--timeout", evaluator.timeout.to_s,
				"--redirect", evaluator.bind_secure,
				*evaluator.configuration_paths, ready: false
			)
		end
		
		container.spawn(name: "Falcon Proxy", restart: true, key: :proxy) do |instance|
			instance.exec(falcon_path, "proxy",
				"--bind", evaluator.bind_secure,
				"--timeout", evaluator.timeout.to_s,
				*evaluator.configuration_paths, ready: false
			)
		end
	end
end

#spawn(path, container, **options) ⇒ Object

Spawn an application instance from the specified path.



95
96
97
98
99
100
101
102
103
# File 'lib/falcon/service/virtual.rb', line 95

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