class Falcon::Configuration
def add(name, *parents, &block)
def add(name, *parents, &block) raise KeyError.new("#{name} is already set", key: name) if @environments.key?(name) environments = parents.map{|name| @environments.fetch(name)} parent = Build::Environment.combine(*environments) @environments[name] = Build::Environment.new(parent, name: name, &block) end
def each
def each return to_enum unless block_given? @environments.each do |name, environment| if environment.include?(:authority) yield environment end end end
def host(name, *parents, &block)
def host(name, *parents, &block) add(name, :host, *parents, &block).tap do |environment| environment[:authority] = name end end
def initialize(verbose = false)
def initialize(verbose = false) @environments = {} @verbose = verbose add(:ssl) do ssl_session_id {"falcon"} end add(:host, :ssl) do ssl_certificate_path {File.expand_path("ssl/certificate.pem", root)} ssl_certificate {OpenSSL::X509::Certificate.new(File.read(ssl_certificate_path))} ssl_private_key_path {File.expand_path("ssl/private.key", root)} ssl_private_key {OpenSSL::PKey::RSA.new(File.read(ssl_private_key_path))} ssl_context do OpenSSL::SSL::SSLContext.new.tap do |context| context.cert = ssl_certificate context.key = ssl_private_key context.session_id_context = ssl_session_id context.set_params context.setup end end end add(:lets_encrypt, :ssl) do lets_encrypt_root '/etc/letsencrypt/live' ssl_certificate_path {File.join(lets_encrypt_root, authority, "fullchain.pem")} ssl_private_key_path {File.join(lets_encrypt_root, authority, "privkey.pem")} end add(:self_signed, :ssl) do ssl_context do contexts = Localhost::Authority.fetch(authority) contexts.server_context.tap do |context| context.alpn_select_cb = lambda do |protocols| if protocols.include? "h2" return "h2" elsif protocols.include? "http/1.1" return "http/1.1" elsif protocols.include? "http/1.0" return "http/1.0" else return nil end end context.session_id_context = "falcon" end end end add(:proxy, :host) do endpoint {::Async::HTTP::URLEndpoint.parse(url)} end add(:rack, :host) do config_path {::File.expand_path("config.ru", root)} middleware do ::Falcon::Server.middleware( ::Rack::Builder.parse_file(config_path).first, verbose: verbose ) end authority 'localhost' scheme 'https' protocol {::Async::HTTP::Protocol::HTTP2} ipc_path {::File.expand_path("server.ipc", root)} endpoint {ProxyEndpoint.unix(ipc_path, protocol: protocol, scheme: scheme, authority: authority)} bound_endpoint do Async::Reactor.run do Async::IO::SharedEndpoint.bound(endpoint) end.wait end server do ::Falcon::Server.new(middleware, bound_endpoint, protocol, scheme) end end end
def load_file(path)
def load_file(path) self.instance_eval(File.read(path), File.realpath(path)) end
def proxy(name, *parents, &block)
def proxy(name, *parents, &block) add(name, :proxy, *parents, &block).tap do |environment| environment[:authority] = name end end
def rack(name, *parents, &block)
def rack(name, *parents, &block) add(name, :rack, *parents, &block).tap do |environment| environment[:authority] = name end end