class VirtualServer

Virtual server hosting virtual service defined through files

Public Class Methods

determine_certs(webrick_options) click to toggle source
# File lib/file_sv/virtual_server.rb, line 26
def self.determine_certs(webrick_options)
  if GlobalSettings.key && GlobalSettings.cert
    webrick_options = own_certs webrick_options
  else
    puts "Using self signed cert"
    webrick_options[:ServerName] = "localhost"
    webrick_options[:SSLCertName] = "/CN=localhost"
  end
  webrick_options
end
own_certs(webrick_options) click to toggle source
# File lib/file_sv/virtual_server.rb, line 17
def self.own_certs(webrick_options)
  puts "Using cert from #{GlobalSettings.cert}"
  cert = OpenSSL::X509::Certificate.new File.read GlobalSettings.cert
  pkey = OpenSSL::PKey::RSA.new File.read GlobalSettings.key
  webrick_options[:SSLCertificate] = cert
  webrick_options[:SSLPrivateKey] = pkey
  webrick_options
end
run!() click to toggle source

Run as https with self signed cert

# File lib/file_sv/virtual_server.rb, line 38
def self.run!
  logger = WEBrick::Log.new(nil, WEBrick::BasicLog::WARN)
  webrick_options = { Port: port, SSLEnable: true, Logger: logger }
  webrick_options = determine_certs webrick_options
  # TODO: Following run does not work on Ruby 3
  Rack::Handler::WEBrick.run(self, webrick_options) do |server|
    %i[INT TERM].each { |sig| trap(sig) { server.stop } }
    server.threaded = settings.threaded if server.respond_to? :threaded=
    set :running, true
  end
end

Public Instance Methods

output_for(endpoint, binding) click to toggle source

Output for endpoint, either a file or text content @param [PlannedEndpoint] endpoint Planned endpoint to serve

# File lib/file_sv/virtual_server.rb, line 66
def output_for(endpoint, binding)
  endpoint.file? ? send_file(endpoint.serving_file_name) : endpoint.content(binding)
end
serve(endpoint, id = nil) click to toggle source

Log endpoint. Return content and status code defined by endpoint @param [PlannedEndpoint] endpoint Planned endpoint to serve

# File lib/file_sv/virtual_server.rb, line 72
def serve(endpoint, id = nil)
  message = "Using endpoint based on file #{endpoint.serving_file_name}."
  @id = id
  message += " Using param '#{@id}'" if id
  puts message
  [endpoint.status_code, output_for(endpoint, binding)]
end