-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2020-2023, by Samuel Williams. | ||
|
||
require_relative '../service/proxy' | ||
require_relative 'paths' | ||
|
||
require 'samovar' | ||
|
||
module Falcon | ||
module Command | ||
# Implements the `falcon proxy` command. | ||
# | ||
# Manages a {Controller::Proxy} instance which is responsible for proxing incoming requests. | ||
class Proxy < Samovar::Command | ||
self.description = "Proxy to one or more backend hosts." | ||
|
||
# The command line options. | ||
# @attribute [Samovar::Options] | ||
options do | ||
option '--bind <address>', "Bind to the given hostname/address", default: "https://[::]:443" | ||
|
||
option '-t/--timeout <duration>', "Specify the maximum time to wait for non-blocking operations.", type: Float, default: nil | ||
end | ||
|
||
# One or more paths to the configuration files. | ||
# @name paths | ||
# @attribute [Array(String)] | ||
many :paths | ||
|
||
include Paths | ||
|
||
def environment | ||
Async::Service::Environment.new(Falcon::Service::Proxy::Environment).with( | ||
root: Dir.pwd, | ||
verbose: self.parent&.verbose?, | ||
name: "proxy", | ||
|
||
url: @options[:bind], | ||
) | ||
end | ||
|
||
def configuration | ||
Configuration.new.tap do |configuration| | ||
configuration.add(self.environment) | ||
end | ||
end | ||
|
||
# The container class to use. | ||
def container_class | ||
Async::Container.best_container_class | ||
end | ||
|
||
# Prepare the environment and run the controller. | ||
def call | ||
Console.logger.info(self) do |buffer| | ||
buffer.puts "Falcon Proxy v#{VERSION} taking flight!" | ||
buffer.puts "- Binding to: #{@options[:bind]}" | ||
buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}" | ||
buffer.puts "- To reload: kill -HUP #{Process.pid}" | ||
end | ||
|
||
Async::Service::Controller.run(self.configuration, container_class: self.container_class) | ||
end | ||
|
||
# The endpoint to bind to. | ||
def endpoint(**options) | ||
Async::HTTP::Endpoint.parse(@options[:bind], timeout: @options[:timeout], **options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
# Released under the MIT License. | ||
# Copyright, 2020-2023, by Samuel Williams. | ||
|
||
require_relative '../service/redirect' | ||
require_relative 'paths' | ||
|
||
require 'samovar' | ||
|
||
module Falcon | ||
module Command | ||
class Redirect < Samovar::Command | ||
self.description = "Redirect from insecure HTTP to secure HTTP." | ||
|
||
# The command line options. | ||
# @attribute [Samovar::Options] | ||
options do | ||
option '--bind <address>', "Bind to the given hostname/address", default: "http://[::]:80" | ||
option '--redirect <address>', "Redirect using this address as a template.", default: "https://[::]:443" | ||
|
||
option '-t/--timeout <duration>', "Specify the maximum time to wait for non-blocking operations.", type: Float, default: nil | ||
end | ||
|
||
# One or more paths to the configuration files. | ||
# @name paths | ||
# @attribute [Array(String)] | ||
many :paths | ||
|
||
include Paths | ||
|
||
def environment | ||
Async::Service::Environment.new(Falcon::Service::Proxy::Environment).with( | ||
root: Dir.pwd, | ||
verbose: self.parent&.verbose?, | ||
name: "proxy", | ||
|
||
url: @options[:bind], | ||
) | ||
end | ||
|
||
def configuration | ||
Configuration.new.tap do |configuration| | ||
configuration.add(self.environment) | ||
end | ||
end | ||
|
||
# The container class to use. | ||
def container_class | ||
Async::Container.best_container_class | ||
end | ||
|
||
# Prepare the environment and run the controller. | ||
def call | ||
Console.logger.info(self) do |buffer| | ||
buffer.puts "Falcon Redirect v#{VERSION} taking flight!" | ||
buffer.puts "- Binding to: #{@options[:bind]}" | ||
buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}" | ||
buffer.puts "- To reload: kill -HUP #{Process.pid}" | ||
end | ||
|
||
Async::Service::Controller.run(self.configuration, container_class: self.container_class) | ||
end | ||
|
||
# The endpoint to bind to. | ||
def endpoint(**options) | ||
Async::HTTP::Endpoint.parse(@options[:bind], timeout: @options[:timeout], **options) | ||
end | ||
|
||
# The template endpoint to redirect to. | ||
def redirect_endpoint(**options) | ||
Async::HTTP::Endpoint.parse(@options[:redirect], **options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters