From 77346bb2daf4ec527a14a08d65e087dc1b9b0ced Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 10 May 2024 18:10:51 +0900 Subject: [PATCH] Better handling of resolved paths. (#242) * Fixes #241. --- lib/falcon/command/paths.rb | 10 ++++++---- lib/falcon/command/proxy.rb | 2 +- lib/falcon/command/redirect.rb | 2 +- lib/falcon/command/virtual.rb | 2 ++ lib/falcon/environment/configured.rb | 10 +++++++--- test/falcon/command/virtual.rb | 15 +++++++++++++++ 6 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/falcon/command/paths.rb b/lib/falcon/command/paths.rb index a95b4c9d..273a73a7 100644 --- a/lib/falcon/command/paths.rb +++ b/lib/falcon/command/paths.rb @@ -12,16 +12,18 @@ module Paths # Resolve a set of `@paths` that may contain wildcards, into a sorted, unique array. # @returns [Array(String)] def resolved_paths(&block) - @paths.collect do |path| - Dir.glob(path) - end.flatten.sort.uniq.each(&block) + if @paths + @paths.collect do |path| + Dir.glob(path) + end.flatten.sort.uniq.each(&block) + end end # Build a configuration based on the resolved paths. def configuration configuration = Configuration.new - self.resolved_paths.each do |path| + self.resolved_paths do |path| path = File.expand_path(path) configuration.load_file(path) diff --git a/lib/falcon/command/proxy.rb b/lib/falcon/command/proxy.rb index f7f96e75..1189ef3c 100644 --- a/lib/falcon/command/proxy.rb +++ b/lib/falcon/command/proxy.rb @@ -56,7 +56,7 @@ def call buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}" buffer.puts "- To reload: kill -HUP #{Process.pid}" - self.resolved_paths.each do |path| + self.resolved_paths do |path| buffer.puts "- Loading configuration from #{path}" end end diff --git a/lib/falcon/command/redirect.rb b/lib/falcon/command/redirect.rb index 12d29deb..32c3ca6a 100644 --- a/lib/falcon/command/redirect.rb +++ b/lib/falcon/command/redirect.rb @@ -55,7 +55,7 @@ def call buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}" buffer.puts "- To reload: kill -HUP #{Process.pid}" - self.resolved_paths.each do |path| + self.resolved_paths do |path| buffer.puts "- Loading configuration from #{path}" end end diff --git a/lib/falcon/command/virtual.rb b/lib/falcon/command/virtual.rb index e4c97693..a56aa20e 100644 --- a/lib/falcon/command/virtual.rb +++ b/lib/falcon/command/virtual.rb @@ -3,6 +3,8 @@ # Released under the MIT License. # Copyright, 2018-2024, by Samuel Williams. +require 'async/service' + require_relative '../environment/virtual' require 'samovar' diff --git a/lib/falcon/environment/configured.rb b/lib/falcon/environment/configured.rb index a92e8553..e9e1d125 100644 --- a/lib/falcon/environment/configured.rb +++ b/lib/falcon/environment/configured.rb @@ -16,9 +16,13 @@ def configuration_paths # All the falcon application configuration paths, with wildcards expanded. def resolved_configuration_paths - configuration_paths.flat_map do |path| - Dir.glob(path) - end.uniq + if configuration_paths = self.configuration_paths + configuration_paths.flat_map do |path| + Dir.glob(path) + end.uniq + else + [] + end end def configuration diff --git a/test/falcon/command/virtual.rb b/test/falcon/command/virtual.rb index c425be3a..a69774cc 100644 --- a/test/falcon/command/virtual.rb +++ b/test/falcon/command/virtual.rb @@ -42,6 +42,21 @@ def around let(:insecure_client) {Async::HTTP::Client.new(command.insecure_endpoint, retries: 0)} + with 'no paths' do + let (:paths) {[]} + + it "should still start correctly" do + request = Protocol::HTTP::Request.new("https", "hello.localhost", "GET", "/index") + + Async do + response = insecure_client.get("/index") + + expect(response).to be(:failure?) + pp response + end + end + end + it "gets redirected from insecure to secure endpoint" do request = Protocol::HTTP::Request.new("http", "hello.localhost", "GET", "/index")