Skip to content

Commit

Permalink
Server service working...
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 11, 2024
1 parent df3f6bd commit 6c7bb45
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 217 deletions.
11 changes: 11 additions & 0 deletions hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env async-service

require 'falcon/service/server'

service "hello-server" do
include Falcon::Service::Server

middleware do
::Protocol::HTTP::Middleware::HelloWorld
end
end
73 changes: 30 additions & 43 deletions lib/falcon/command/serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ class Serve < Samovar::Command
option '--threads <count>', "Number of threads (hybrid only).", type: Integer
end

# Options for the {endpoint}.
def endpoint_options
@options.slice(:hostname, :port, :reuse_port, :timeout)
end

def environment
Async::Service::Environment.build do
include Falcon::Service::Server

rackup_path @options[:config]

container_options @options.slice(:count, :forks, :threads)

preload @options[:preload]

verbose @parent&.verbose?

cache @options[:cache]

endpoint do
Endpoint.parse(@options[:bind], **endpoint_options)
end
end
end

# The container class to use.
def container_class
case @options[:container]
Expand All @@ -65,37 +90,6 @@ def container_class
end
end

# Whether verbose logging is enabled.
# @returns [Boolean]
def verbose?
@parent&.verbose?
end

# Whether to enable the application HTTP cache.
# @returns [Boolean]
def cache?
@options[:cache]
end

# Load the rack application from the specified configuration path.
# @returns [Protocol::HTTP::Middleware]
def load_app
rack_app, _ = Rack::Builder.parse_file(@options[:config])

return Server.middleware(rack_app, verbose: self.verbose?, cache: self.cache?)
end

# Options for the container.
# See {Controller::Serve#setup}.
def container_options
@options.slice(:count, :forks, :threads)
end

# Options for the {endpoint}.
def endpoint_options
@options.slice(:hostname, :port, :reuse_port, :timeout)
end

# The endpoint to bind to.
def endpoint
Endpoint.parse(@options[:bind], **endpoint_options)
Expand All @@ -111,11 +105,6 @@ def client
Async::HTTP::Client.new(client_endpoint)
end

# Prepare a new controller for the command.
def controller
Controller::Serve.new(self)
end

# Prepare the environment and run the controller.
def call
Console.logger.info(self) do |buffer|
Expand All @@ -125,22 +114,20 @@ def call
buffer.puts "- To reload configuration: kill -HUP #{Process.pid}"
end

if path = @options[:preload]
full_path = File.expand_path(path)
load(full_path)
end

begin
Bundler.require(:preload)
rescue Bundler::GemfileNotFound
# Ignore.
end

if GC.respond_to?(:compact)
if Process.respond_to?(:warmup)
Process.warmup
elsif GC.respond_to?(:compact)
3.times{GC.start}
GC.compact
end

self.controller.run
controller = Async::Service::Controller.new(container_class: self.container_class, environment: environment)
end
end
end
Expand Down
110 changes: 0 additions & 110 deletions lib/falcon/controller/serve.rb

This file was deleted.

61 changes: 0 additions & 61 deletions lib/falcon/service/generic.rb

This file was deleted.

27 changes: 25 additions & 2 deletions lib/falcon/service/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

require_relative 'generic'
require 'async/service/generic'

require 'async/http/endpoint'
require 'async/io/shared_endpoint'

module Falcon
module Service
class Proxy < Generic
class Proxy < Async::Service::Generic
module Environment
# The host that this proxy will receive connections for.
def url
"https://[::]:443"
end

# 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

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

def name
"#{self.class} for #{self.authority}"
end
Expand Down
Loading

0 comments on commit 6c7bb45

Please sign in to comment.