From ccb42eff2c3824dc3504132e6e2b01c994df5279 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 22 Mar 2024 21:31:58 +1300 Subject: [PATCH] WIP. --- falcon.gemspec | 2 +- lib/falcon/command/serve.rb | 7 ++- lib/falcon/environments/application.rb | 73 -------------------------- lib/falcon/environments/proxy.rb | 28 ---------- lib/falcon/environments/rack.rb | 21 ++++++-- lib/falcon/service/application.rb | 55 ++++++++++++++++++- lib/falcon/service/rackup.rb | 29 ---------- test/falcon/command/serve.rb | 2 +- test/falcon/command/top.rb | 2 +- test/falcon/command/virtual.rb | 2 +- 10 files changed, 81 insertions(+), 140 deletions(-) delete mode 100644 lib/falcon/environments/application.rb delete mode 100644 lib/falcon/environments/proxy.rb delete mode 100644 lib/falcon/service/rackup.rb diff --git a/falcon.gemspec b/falcon.gemspec index 3ca421fa..a7664375 100644 --- a/falcon.gemspec +++ b/falcon.gemspec @@ -30,7 +30,7 @@ Gem::Specification.new do |spec| spec.add_dependency "async-http", "~> 0.57" spec.add_dependency "async-http-cache", "~> 0.4.0" spec.add_dependency "async-io", "~> 1.22" - spec.add_dependency "async-service", "~> 0.7.0" + spec.add_dependency "async-service", "~> 0.8.0" spec.add_dependency "bundler" spec.add_dependency "localhost", "~> 1.1" spec.add_dependency "openssl", "~> 3.0" diff --git a/lib/falcon/command/serve.rb b/lib/falcon/command/serve.rb index 4918d0c6..05324275 100644 --- a/lib/falcon/command/serve.rb +++ b/lib/falcon/command/serve.rb @@ -6,7 +6,8 @@ require_relative '../server' require_relative '../endpoint' -require_relative '../service/rackup' +require_relative '../service/server' +require_relative '../environment/rackup' require 'async/container' require 'async/http/client' @@ -51,7 +52,9 @@ def endpoint_options end def environment - Async::Service::Environment.new(Falcon::Service::Rackup::Environment).with( + Async::Service::Environment.new(Falcon::Service::Server::Environment).with( + Falcon::Environment::Rackup, + root: Dir.pwd, verbose: self.parent&.verbose?, diff --git a/lib/falcon/environments/application.rb b/lib/falcon/environments/application.rb deleted file mode 100644 index 2eba2974..00000000 --- a/lib/falcon/environments/application.rb +++ /dev/null @@ -1,73 +0,0 @@ -# frozen_string_literal: true - -# Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. -# Copyright, 2020, by Daniel Evans. - -require_relative '../proxy_endpoint' -require_relative '../server' - -require_relative '../service/application' -require_relative '../environments' - -module Falcon - module Environments - # A general application environment. Suitable for use with any {Protocol::HTTP::Middleware}. - module Application - # The service class to use for the application. - # @returns [Class] - def service_class - ::Falcon::Service::Application - end - - # The middleware stack for the application. - # @returns [Protocol::HTTP::Middleware] - def middleware - ::Protocol::HTTP::Middleware::HelloWorld - end - - # The scheme to use to communicate with the application. - # @returns [String] - def scheme - 'https' - end - - # The protocol to use to communicate with the application. - # - # Typically one of {Async::HTTP::Protocol::HTTP1} or {Async::HTTP::Protocl::HTTP2}. - # - # @returns [Async::HTTP::Protocol] - def protocol - Async::HTTP::Protocol::HTTP2 - end - - # The IPC path to use for communication with the application. - # @returns [String] - def ipc_path - ::File.expand_path("application.ipc", root) - end - - # The endpoint that will be used for communicating with the application server. - # @returns [Async::IO::Endpoint] - def endpoint - ::Falcon::ProxyEndpoint.unix(ipc_path, - protocol: protocol, - scheme: scheme, - authority: authority - ) - end - - # Number of instances to start. - # @returns [Integer | nil] - def count - nil - end - - def preload - [] - end - end - - LEGACY_ENVIRONMENTS[:application] = Application - end -end diff --git a/lib/falcon/environments/proxy.rb b/lib/falcon/environments/proxy.rb deleted file mode 100644 index 323f244c..00000000 --- a/lib/falcon/environments/proxy.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -# Released under the MIT License. -# Copyright, 2019-2023, by Samuel Williams. - -require_relative '../service/proxy' -require_relative '../environments' - -module Falcon - module Environments - # A HTTP proxy environment. - module Proxy - # The upstream endpoint that will handle incoming requests. - # @attribute [Async::HTTP::Endpoint] - def endpoint - ::Async::HTTP::Endpoint.parse(url) - end - - # The service class to use for the proxy. - # @attribute [Class] - def service_class - ::Falcon::Service::Proxy - end - end - - LEGACY_ENVIRONMENTS[:proxy] = Proxy - end -end diff --git a/lib/falcon/environments/rack.rb b/lib/falcon/environments/rack.rb index 5ba855f4..18ceb960 100644 --- a/lib/falcon/environments/rack.rb +++ b/lib/falcon/environments/rack.rb @@ -3,11 +3,26 @@ # Released under the MIT License. # Copyright, 2019-2023, by Samuel Williams. -require_relative '../service/rackup' +require 'rack/builder' +require_relative '../server' require_relative '../environments' module Falcon - module Environments - LEGACY_ENVIRONMENTS[:rack] = Service::Rackup::Environment + module Environment + module Rack + def rackup_path + 'config.ru' + end + + def rack_app + ::Rack::Builder.parse_file(rackup_path) + end + + def middleware + ::Falcon::Server.middleware(rack_app, verbose: verbose, cache: cache) + end + end + + LEGACY_ENVIRONMENTS[:rack] = Rack end end diff --git a/lib/falcon/service/application.rb b/lib/falcon/service/application.rb index c14aa24a..6e607a52 100644 --- a/lib/falcon/service/application.rb +++ b/lib/falcon/service/application.rb @@ -13,7 +13,60 @@ module Falcon module Service # Implements an application server using an internal clear-text proxy. class Application < Server - def initialize(environment) + module Environment + include Server::Environment + + # The service class to use for the application. + # @returns [Class] + def service_class + ::Falcon::Service::Application + end + + # The middleware stack for the application. + # @returns [Protocol::HTTP::Middleware] + def middleware + ::Protocol::HTTP::Middleware::HelloWorld + end + + # The scheme to use to communicate with the application. + # @returns [String] + def scheme + 'https' + end + + # The protocol to use to communicate with the application. + # + # Typically one of {Async::HTTP::Protocol::HTTP1} or {Async::HTTP::Protocl::HTTP2}. + # + # @returns [Async::HTTP::Protocol] + def protocol + Async::HTTP::Protocol::HTTP2 + end + + # The IPC path to use for communication with the application. + # @returns [String] + def ipc_path + ::File.expand_path("application.ipc", root) + end + + # The endpoint that will be used for communicating with the application server. + # @returns [Async::IO::Endpoint] + def endpoint + ::Falcon::ProxyEndpoint.unix(ipc_path, + protocol: protocol, + scheme: scheme, + authority: authority + ) + end + + # Number of instances to start. + # @returns [Integer | nil] + def count + nil + end + end + + def initialize(...) super @bound_endpoint = nil diff --git a/lib/falcon/service/rackup.rb b/lib/falcon/service/rackup.rb deleted file mode 100644 index cabced84..00000000 --- a/lib/falcon/service/rackup.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -# Released under the MIT License. -# Copyright, 2024, by Samuel Williams. - -require_relative 'server' - -module Falcon - module Service - # A controller for redirecting requests. - class Rackup < Server - module Environment - include Server::Environment - - def rackup_path - 'config.ru' - end - - def rack_app - Rack::Builder.parse_file(rackup_path) - end - - def middleware - Falcon::Server.middleware(rack_app, verbose: verbose, cache: cache) - end - end - end - end -end diff --git a/test/falcon/command/serve.rb b/test/falcon/command/serve.rb index cdc098cc..26052d2f 100644 --- a/test/falcon/command/serve.rb +++ b/test/falcon/command/serve.rb @@ -16,7 +16,7 @@ it "can listen on specified port" do configuration = command.configuration - controller = Async::Service::Controller.new(configuration.services.to_a) + controller = configuration.controller controller.start diff --git a/test/falcon/command/top.rb b/test/falcon/command/top.rb index 2127c971..0bdfe584 100644 --- a/test/falcon/command/top.rb +++ b/test/falcon/command/top.rb @@ -16,7 +16,7 @@ ] serve = top.command - controller = Async::Service::Controller.new(serve.configuration.services.to_a) + controller = serve.configuration.controller controller.start Async do diff --git a/test/falcon/command/virtual.rb b/test/falcon/command/virtual.rb index bb5eb5eb..06ba80dc 100644 --- a/test/falcon/command/virtual.rb +++ b/test/falcon/command/virtual.rb @@ -29,7 +29,7 @@ def around configuration = command.configuration - controller = Async::Service::Controller.new(configuration.services.to_a) + controller = configuration.controller controller.start