yahns.git  about / heads / tags
sleepy, multi-threaded, non-blocking application server for Ruby
blob 538e00d9dfde7acf14fe3c77a7d507481425d3ac 1833 bytes (raw)
$ git show maint:lib/yahns/rackup_handler.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 
# Copyright (C) 2013-2016 all contributors <yahns-public@yhbt.net>
# License: GPL-3.0+ (https://www.gnu.org/licenses/gpl-3.0.txt)
# frozen_string_literal: true
require_relative '../yahns'

module Yahns::RackupHandler # :nodoc:
  def self.default_host
    environment  = ENV['RACK_ENV'] || 'development'
    environment == 'development' ? '127.0.0.1' : '0.0.0.0'
  end

  def self.run(app, o)
    cfg = Yahns::Config.new
    cfg.instance_eval do
      # we need this because "rackup -D" sends us to "/", which might be
      # fine for most apps, but we have SIGUSR2 restarts to support
      working_directory(Yahns::START[:cwd])

      app(:rack, app) do
        addr = o[:listen] || "#{o[:Host]||default_host}:#{o[:Port]||8080}"
        # allow listening to multiple addresses
        if addr.include?(',')
          addr.split(',').each { |l| listen(l) }
        else
          listen addr
        end

        val = o[:client_timeout] and client_timeout(val)
      end

      queue do
        wt = o[:worker_threads] and worker_threads(wt)
      end

      %w(stderr_path stdout_path).each do |x|
        val = o[x] and __send__(x, val)
      end
    end
    Yahns::Server.new(cfg).start.join
  end

  # this is called by Rack::Server
  def self.valid_options
    # these should be the most common options
    {
      "listen=ADDRESS" => "address(es) to listen on (e.g. /tmp/sock)",
      "worker_threads=NUM" => "number of worker threads to run",

      # this affects how quickly graceful shutdown goes
      "client_timeout=SECONDS" => "timeout for idle clients",

      # I don't want these here, but rackup supports daemonize and
      # we lose useful information when that sends stdout/stderr to /dev/null
      "stderr_path=PATH" => "stderr destination",
      "stdout_path=PATH" => "stdout destination",
    }
  end
end

git clone git://yhbt.net/yahns.git
git clone https://yhbt.net/yahns.git