Module: Rackup::Handler::Kino

Defined in:
lib/rackup/handler/kino.rb

Overview

The Rack handler: lets any host that speaks the rackup protocol boot Kino, which is what rackup -s kino and rails server -u kino do. Loaded on demand by Rackup::Handler.get(:kino), so Rackup itself is already defined here; Kino is required only when the host calls in.

Class Method Summary collapse

Class Method Details

.run(app, **options) {|server| ... } ⇒ ::Kino::Server

Boot a server for app and block until it shuts down, the way the kino executable does (banner, INT/TERM drain, stats on USR1).

Parameters:

  • app (#call)

    the Rack application the host built

  • options (Hash)

    the host's options (see server_options)

Yields:

  • (server)

    the built, not yet started server, for hosts that want a handle on it

Returns:



29
30
31
32
33
34
# File 'lib/rackup/handler/kino.rb', line 29

def self.run(app, **options)
  require "kino" # audition:disable runtime-require
  server = ::Kino::Server.new(app, **server_options(options))
  yield server if block_given?
  server.run
end

.server_options(options) ⇒ Hash{Symbol => Object}

Translate host options into Kino::Server#initialize kwargs. Precedence: options the user typed > the config file > defaults the host supplied (rackup's and Rails' own Host and Port) > Kino's defaults. Hosts that say which options were typed pass user_supplied_options; when that list is absent every option counts as typed. Keys outside OPTION_MAP (the host's bookkeeping: environment, pid, config, ...) are ignored.

Parameters:

  • options (Hash{Symbol => Object})

Returns:

  • (Hash{Symbol => Object})


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rackup/handler/kino.rb', line 60

def self.server_options(options)
  require "kino" # audition:disable runtime-require
  options = options.dup
  host_defaults = {}
  if (typed = options.delete(:user_supplied_options))
    (options.keys - typed).each { |key| host_defaults[key] = options.delete(key) }
  end

  config = ::Kino::Configuration.new
  path = options.delete(:Config) || host_defaults.delete(:Config) || ::Kino::Configuration.default_path
  config.load_file(path) if path
  translate(host_defaults).each { |key, value| config.set(key, value) unless config.set?(key) }
  config.merge!(translate(options))
  config.set(:port, ::Kino::Configuration::DEFAULT_SERVING_PORT) unless config.set?(:port)
  config.server_options
end

.valid_optionsHash{String => String}

The -O NAME=VALUE options rackup -s kino --help lists (rackup shows its own -o/-p in place of Host and Port).

Returns:

  • (Hash{String => String})


39
40
41
42
43
44
45
46
47
48
# File 'lib/rackup/handler/kino.rb', line 39

def self.valid_options
  {
    "Host=HOST" => "Address to bind (default: 127.0.0.1)",
    "Port=PORT" => "Port to listen on (default: 9292)",
    "Workers=COUNT" => "Workers: ractors in :ractor mode, thread groups in :threaded (default: one per CPU)",
    "Threads=COUNT" => "Threads per worker (default: 1 in :ractor, 3 in :threaded)",
    "Mode=MODE" => "auto | ractor | threaded (default: auto)",
    "Config=PATH" => "Kino config file (default: kino.rb, then config/kino.rb)"
  }
end