class Oxy::Server

Attributes

options[RW]
status[RW]
stdout[R]

Public Class Methods

new(argv = [], stdin = $stdin, stdout = $stdout, stderr = $stderr) click to toggle source
# File lib/oxy/server.rb, line 13
def initialize(argv = [], stdin = $stdin, stdout = $stdout, stderr = $stderr)
  @stdout = stdout
  @options = OpenStruct.new
  @parser = option_parser
  # actually parse arguments input
  parse_options(argv)
end

Public Instance Methods

run() click to toggle source
# File lib/oxy/server.rb, line 21
def run
  # setup oxy's folder
  FileUtils.mkdir_p(oxy_home_path) unless File.exist?(oxy_home_path)
  # process user's options (if any)
  miscellaneous
  # start server's thread
  server_thread = start_server
  # wait for the server's thread to join
  server_thread.join if server_thread
end

Private Instance Methods

miscellaneous() click to toggle source

Miscellaneous options simply renders the appropriate information to the output.

# File lib/oxy/server.rb, line 91
def miscellaneous
  if @options.show_version then
    @stdout.puts "#{@parser.program_name}: version #{Oxy::VERSION}"
    exit 0
  elsif @options.show_help then
    @stdout.puts @parser.to_s
    exit 0
  elsif @options.error_message then
    @stdout.puts @options.error_message
    exit 1
  end
end
option_parser() click to toggle source
# File lib/oxy/server.rb, line 104
def option_parser
  # We derive our options from Puma to avoid confusion
  OptionParser.new do |op|
    op.separator ""
    # parse help
    op.on("-h", "--help", "Display this text") do
      @options.show_help = true
    end
    # parse version
    op.on("-V", "--version", "Show version") do
      @options.show_version = true
    end
    # parse uri
    op.on("-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)") do |uri|
      @options.uri = uri
    end
    # parse port no.
    op.on("-p", "--port PORT", Integer, "Define the TCP port to bind to", "Use -b for more advanced options") do |port|
      @options.port = port
    end
    # parse document root
    op.on("--directory PATH", "PATH to serve files from") do |path|
      @options.document_root = path
    end
    # parse quiet mode
    op.on("--quiet", "Do not display any logs to STDOUT") do
      @options.quiet = true
    end
    # parse rsvp middleware option
    op.on("--rsvp", "Enable RSVP middleware to handle form submissions") do
      @options.rsvp = true
    end
  end
end
oxy_home_path() click to toggle source
# File lib/oxy/server.rb, line 33
def oxy_home_path
  "#{ENV["HOME"]}/oxy"
end
parse_options(argv = []) click to toggle source
# File lib/oxy/server.rb, line 75
def parse_options(argv = [])
  begin
    # parse options input from the user
    @parser.parse!(argv)
    # set option defaults
    @options.document_root = Dir.pwd unless @options.document_root
    @options.port = 4000 unless @options.port
    @options.quiet = false unless @options.quiet
    @options.rsvp = false unless @options.rsvp
  rescue OptionParser::ParseError => pe
    msg = ["#{@parser.program_name}: #{pe}", "Try `#{@parser.program_name} --help` for more information"]
    @options.error_message = msg.join("\n")
  end
end
start_server() click to toggle source
# File lib/oxy/server.rb, line 58
def start_server
  # instantiate our Rack server
  @server = Rack::Server.new({
    :app  => static_site_app,
    :pid  => File.join(oxy_home_path, "oxy.pid"),
    :Port => @options.port,
    :Host => @options.address,
    :environment => "deployment",
    :server => "puma",
    :daemonize => false,
    :quiet => @options.quiet,
    :directory => @options.document_root
  })
  # spin the server
  Thread.new { @server.start }
end
static_site_app() click to toggle source
# File lib/oxy/server.rb, line 37
def static_site_app
  # pin options and logger
  options, logger = @options, @stdout
  # pin not_found_path file path
  not_found_path = File.join(options.document_root, "404.html")
  # nullify not_found_path if the file does not exist
  not_found_path = nil unless File.exist?(not_found_path)
  # build new app
  stack = Rack::Builder.new {
    # middleware to handle custom HTTP headers
    use Oxy::CustomHeaders, options
    # middleware to handle RSVP features
    use Oxy::RSVP, logger if options.rsvp
    # middleware to handle static resources and prohibit access to the configuration files
    use Oxy::TryStatic, options
    # fallback to 404 middleware as the main application
    run Rack::NotFound.new(not_found_path)
  }
  return stack.to_app
end