Class: Kino::Configuration
- Inherits:
-
Object
- Object
- Kino::Configuration
- Defined in:
- lib/kino/configuration.rb
Overview
Server settings with Puma-style precedence:
explicit Server.new kwargs > config file DSL > defaults.
Defined Under Namespace
Classes: DSL
Constant Summary collapse
- DEFAULTS =
Every setting and its default; the full reference lives in the generated sample config (
kino --init). { bind: "127.0.0.1", port: 0, workers: nil, # resolved to Etc.nprocessors in #to_h threads: nil, # resolved per mode in Server: 1 in :ractor, 3 in :threaded mode: :auto, queue_depth: 1024, queue_timeout: 5.0, request_timeout: nil, max_connections: nil, # nil = derive from the open-file limit max_body_size: 50 * 1024 * 1024, # 50 MB; nil/0 = unlimited batch: 1, lanes: false, log_requests: false, on_error: nil, shutdown_timeout: 30, tokio_threads: nil, tls: nil, environment: nil, pidfile: nil, rackup: nil }.freeze
- SETTINGS =
The known setting names.
DEFAULTS.keys.freeze
- SAMPLE_TEMPLATE =
Source template for sample.
File.("templates/kino.rb.tt", __dir__)
Class Method Summary collapse
-
.sample ⇒ String
The fully-commented sample config (see
kino --init). -
.write_sample(path, force: false) ⇒ String
Write the sample config to
path.
Instance Method Summary collapse
-
#[](key) ⇒ Object
The explicit value, or the default.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#load_file(path) ⇒ self
Load a config file (Ruby DSL) into this configuration.
-
#merge!(options) ⇒ self
Explicit kwargs win over everything already set.
-
#server_options ⇒ Hash{Symbol => Object}
The settings Server.new accepts: everything except the keys only the CLI consumes (rackup file selection, RACK_ENV).
- #set(key, value) ⇒ Object
-
#set?(key) ⇒ Boolean
Whether the key was explicitly set.
-
#to_h ⇒ Hash{Symbol => Object}
Every setting, defaults filled in.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
62 63 64 |
# File 'lib/kino/configuration.rb', line 62 def initialize @values = {} end |
Class Method Details
.sample ⇒ String
The fully-commented sample config (see kino --init).
42 43 44 |
# File 'lib/kino/configuration.rb', line 42 def self.sample File.read(SAMPLE_TEMPLATE) end |
.write_sample(path, force: false) ⇒ String
Write the sample config to path. Refuses to clobber an existing
file unless force: true.
53 54 55 56 57 58 59 60 |
# File 'lib/kino/configuration.rb', line 53 def self.write_sample(path, force: false) if File.exist?(path) && !force raise Error, "#{path} already exists (use force: true to overwrite)" end File.write(path, sample) path end |
Instance Method Details
#[](key) ⇒ Object
Returns the explicit value, or the default.
68 69 70 |
# File 'lib/kino/configuration.rb', line 68 def [](key) @values.fetch(key) { DEFAULTS.fetch(key) } end |
#load_file(path) ⇒ self
Load a config file (Ruby DSL) into this configuration.
90 91 92 93 94 95 |
# File 'lib/kino/configuration.rb', line 90 def load_file(path) raise Error, "config file not found: #{path}" unless File.exist?(path) DSL.new(self).instance_eval(File.read(path), path, 1) self end |
#merge!(options) ⇒ self
Explicit kwargs win over everything already set.
100 101 102 103 |
# File 'lib/kino/configuration.rb', line 100 def merge!() .each { |key, value| set(key, value) } self end |
#server_options ⇒ Hash{Symbol => Object}
The settings Server.new accepts: everything except the keys only the CLI consumes (rackup file selection, RACK_ENV).
115 116 117 |
# File 'lib/kino/configuration.rb', line 115 def to_h.except(:rackup, :environment) end |
#set(key, value) ⇒ Object
75 76 77 78 79 |
# File 'lib/kino/configuration.rb', line 75 def set(key, value) raise ArgumentError, "unknown setting #{key.inspect}" unless DEFAULTS.key?(key) @values[key] = value end |
#set?(key) ⇒ Boolean
Returns whether the key was explicitly set.
82 83 84 |
# File 'lib/kino/configuration.rb', line 82 def set?(key) @values.key?(key) end |
#to_h ⇒ Hash{Symbol => Object}
Returns every setting, defaults filled in.
106 107 108 109 110 |
# File 'lib/kino/configuration.rb', line 106 def to_h SETTINGS.to_h { |key| [key, self[key]] }.tap do |h| h[:workers] ||= Etc.nprocessors end end |