Skip to content

Commit

Permalink
Fix virtual tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 22, 2024
1 parent fba1a14 commit fc8e9cb
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 83 deletions.
2 changes: 1 addition & 1 deletion examples/hello/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require 'async'

Console.logger.debug!
# Console.logger.debug!

class RequestLogger
def initialize(app)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/preload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

$stderr.puts "Preloading..."
# $stderr.puts "Preloading..."
30 changes: 26 additions & 4 deletions lib/falcon/command/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@ class Proxy < Samovar::Command

include Paths

def environment
def environment(**options)
Async::Service::Environment.new(Falcon::Service::Proxy::Environment).with(
root: Dir.pwd,
verbose: self.parent&.verbose?,
name: "proxy",

url: @options[:bind],
timeout: @options[:timeout],
**options
)
end

def host_map(environments)
hosts = {}

environments.each do |environment|
next unless environment.implements?(Falcon::Service::Application::Environment)
evaluator = environment.evaluator
hosts[evaluator.authority] = evaluator
end

Console.info(self) {"Hosts: #{hosts}"}

return hosts
end

def configuration
configuration = super
hosts = host_map(configuration.environments)

Configuration.new.tap do |configuration|
configuration.add(self.environment)
environment = self.environment(hosts: hosts)
configuration.add(environment)
end
end

Expand All @@ -59,6 +77,10 @@ def call
buffer.puts "- Binding to: #{@options[:bind]}"
buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}"
buffer.puts "- To reload: kill -HUP #{Process.pid}"

self.resolved_paths.each do |path|
buffer.puts "- Loading configuration from #{path}"
end
end

Async::Service::Controller.run(self.configuration, container_class: self.container_class)
Expand Down
33 changes: 28 additions & 5 deletions lib/falcon/command/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,38 @@ class Redirect < Samovar::Command

include Paths

def environment
Async::Service::Environment.new(Falcon::Service::Proxy::Environment).with(
def environment(**options)
Async::Service::Environment.new(Falcon::Service::Redirect::Environment).with(
root: Dir.pwd,
verbose: self.parent&.verbose?,
name: "proxy",

url: @options[:bind],
redirect_url: @options[:redirect],
timeout: @options[:timeout],
**options
)
end

def host_map(environments)
hosts = {}

environments.each do |environment|
next unless environment.implements?(Falcon::Service::Application::Environment)
evaluator = environment.evaluator
hosts[evaluator.authority] = evaluator
end

Console.info(self) {"Hosts: #{hosts}"}

return hosts
end

def configuration
configuration = super
hosts = host_map(configuration.environments)

Configuration.new.tap do |configuration|
configuration.add(self.environment)
environment = self.environment(hosts: hosts)
configuration.add(environment)
end
end

Expand All @@ -57,6 +76,10 @@ def call
buffer.puts "- Binding to: #{@options[:bind]}"
buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}"
buffer.puts "- To reload: kill -HUP #{Process.pid}"

self.resolved_paths.each do |path|
buffer.puts "- Loading configuration from #{path}"
end
end

Async::Service::Controller.run(self.configuration, container_class: self.container_class)
Expand Down
20 changes: 20 additions & 0 deletions lib/falcon/command/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ def call

Async::Service::Controller.run(self.configuration)
end

# The insecure endpoint for connecting to the {Redirect} instance.
def insecure_endpoint(**options)
Async::HTTP::Endpoint.parse(@options[:bind_insecure], **options)
end

# The secure endpoint for connecting to the {Proxy} instance.
def secure_endpoint(**options)
Async::HTTP::Endpoint.parse(@options[:bind_secure], **options)
end

# An endpoint suitable for connecting to the specified hostname.
def host_endpoint(hostname, **options)
endpoint = secure_endpoint(**options)

url = URI.parse(@options[:bind_secure])
url.hostname = hostname

return Async::HTTP::Endpoint.new(url, hostname: endpoint.hostname)
end
end
end
end
60 changes: 0 additions & 60 deletions lib/falcon/service/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,66 +66,6 @@ def count
nil
end
end

def initialize(...)
super

@bound_endpoint = nil
end

# Prepare the bound endpoint for the application instances.
# Invoke {preload!} to load shared resources into the parent process.
def start
endpoint = @evaluator.endpoint

Console.logger.info(self) {"Binding to #{endpoint}..."}

@bound_endpoint = Async::Reactor.run do
Async::IO::SharedEndpoint.bound(endpoint)
end.wait

preload!

super
end

# Setup instances of the application into the container.
# @parameter container [Async::Container::Generic]
def setup(container)
protocol = self.protocol
scheme = self.scheme

run_options = {
name: self.name,
restart: true,
}

run_options[:count] = count unless count.nil?

container.run(**run_options) do |instance|
Async do |task|
Console.logger.info(self) {"Starting application server for #{self.root}..."}

server = Server.new(self.middleware, @bound_endpoint, protocol: protocol, scheme: scheme)

server.run

instance.ready!

task.children.each(&:wait)
end
end

super
end

# Close the bound endpoint.
def stop
@bound_endpoint&.close
@bound_endpoint = nil

super
end
end
end
end
10 changes: 8 additions & 2 deletions lib/falcon/service/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def service_class
Proxy
end

def name
service_class.name
end

# The host that this proxy will receive connections for.
def url
"https://[::]:443"
Expand Down Expand Up @@ -63,14 +67,16 @@ def hosts
# @parameter socket [OpenSSL::SSL::SSLSocket] The incoming connection.
# @parameter hostname [String] The negotiated hostname.
def host_context(socket, hostname)
if host = self.hosts[hostname]
hosts = self.hosts

if host = hosts[hostname]
Console.logger.debug(self) {"Resolving #{hostname} -> #{host}"}

socket.hostname = hostname

return host.ssl_context
else
Console.logger.warn(self) {"Unable to resolve #{hostname}!"}
Console.logger.warn(self, hosts: hosts.keys) {"Unable to resolve #{hostname}!"}

return nil
end
Expand Down
6 changes: 0 additions & 6 deletions lib/falcon/service/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ def middleware
Middleware::Redirect.new(Middleware::NotFound, hosts, redirect_endpoint)
end
end

# services.each do |service|
# if service.is_a?(Service::Proxy)
# @hosts[service.authority] = service
# end
# end
end
end
end
17 changes: 14 additions & 3 deletions lib/falcon/service/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def service_class
Server
end

def name
"#{service_class.name} (#{url})"
end

# Options to use when creating the container.
def container_options
{restart: true}
Expand All @@ -28,11 +32,16 @@ def url
"http://[::]:9292"
end

def timeout
nil
end

# The upstream endpoint that will handle incoming requests.
# @returns [Async::HTTP::Endpoint]
def endpoint
::Async::HTTP::Endpoint.parse(url).with(
reuse_address: true,
timeout: timeout,
)
end

Expand Down Expand Up @@ -81,15 +90,15 @@ def preload!

# Prepare the bound endpoint for the server.
def start
endpoint = @evaluator.endpoint
@endpoint = @evaluator.endpoint

Sync do
@bound_endpoint = endpoint.bound
@bound_endpoint = @endpoint.bound
end

preload!

Console.logger.info(self) {"Starting #{name} on #{@endpoint.to_url}"}
Console.logger.info(self) {"Starting #{name} on #{@endpoint}"}

super
end
Expand Down Expand Up @@ -121,6 +130,8 @@ def stop(...)
@bound_endpoint = nil
end

@endpoint = nil

super
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/falcon/service/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def service_class
Virtual
end

def name
service_class.name
end

# All the falcon application configuration paths.
# @returns [Array(String)] Paths to the falcon application configuration files.
def configuration_paths
Expand Down
2 changes: 1 addition & 1 deletion test/falcon/command/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def around
Async do
response = insecure_client.call(request)

expect(response).to be_redirection
expect(response).to be(:redirection?)
expect(response.headers['location']).to be == "https://hello.localhost:8443/index"

response.close
Expand Down

0 comments on commit fc8e9cb

Please sign in to comment.