Class: Zapp::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/zapp/configuration.rb

Overview

Class holding the configuration values used by Zap

Constant Summary collapse

DEFAULT_OPTIONS =
{
  # Rack up file to use
  rackup_file: "config.ru",

  # Default to number of CPUs available
  # This is the amount of workers to run processing requests
  parallelism: Etc.nprocessors,
  # Number of Thread's to run within each worker
  threads_per_worker: 5,

  # Default logging behavior
  logger_class: Zapp::Logger,
  logger_out_io: $stdout,
  log_requests: true,
  log_uncaught_errors: true,

  host: "localhost",
  port: 3000,

  mode: ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development",
  env: ENV.to_hash.merge(
    {
      Rack::RACK_VERSION => Rack::VERSION,
      Rack::RACK_ERRORS => $stderr,
      Rack::RACK_MULTITHREAD => true,
      Rack::RACK_MULTIPROCESS => true,
      Rack::RACK_RUNONCE => false,
      Rack::RACK_URL_SCHEME => %w[yes on 1].include?(ENV["HTTPS"]) ? "https" : "http"
    }
  )
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



57
58
59
60
61
# File 'lib/zapp/configuration.rb', line 57

def initialize
  DEFAULT_OPTIONS.each_key do |key|
    public_send("#{key}=", DEFAULT_OPTIONS[key])
  end
end

Instance Attribute Details

#app(new = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/zapp/configuration.rb', line 73

def app(new = nil)
  @app = new unless new.nil?

  @app ||= begin
    raise(Zapp::ZappError, "Missing rackup file '#{rackup_file}'") unless File.exist?(rackup_file)

    rack_app, = rack_builder.parse_file(rackup_file)

    rack_app
  end
end

#envObject

Returns the value of attribute env.



23
24
25
# File 'lib/zapp/configuration.rb', line 23

def env
  @env
end

#host(new = nil) ⇒ Object



121
122
123
124
125
# File 'lib/zapp/configuration.rb', line 121

def host(new = nil)
  return @host if new.nil?

  @host = new
end

#log_requests(new = nil) ⇒ Object



109
110
111
112
113
# File 'lib/zapp/configuration.rb', line 109

def log_requests(new = nil)
  return @log_requests if new.nil?

  @log_requests = new
end

#log_uncaught_errors(new = nil) ⇒ Object



115
116
117
118
119
# File 'lib/zapp/configuration.rb', line 115

def log_uncaught_errors(new = nil)
  return @log_uncaught_errors if new.nil?

  @log_uncaught_errors = new
end

#logger_class(new = nil) ⇒ Object



97
98
99
100
101
# File 'lib/zapp/configuration.rb', line 97

def logger_class(new = nil)
  return @logger_class if new.nil?

  @logger_class = new
end

#logger_out_io(new = nil) ⇒ Object



103
104
105
106
107
# File 'lib/zapp/configuration.rb', line 103

def logger_out_io(new = nil)
  return @logger_out_io if new.nil?

  @logger_out_io = new
end

#modeObject

Returns the value of attribute mode.



23
24
25
# File 'lib/zapp/configuration.rb', line 23

def mode
  @mode
end

#parallelism(new = nil) ⇒ Object



85
86
87
88
89
# File 'lib/zapp/configuration.rb', line 85

def parallelism(new = nil)
  return @parallelism if new.nil?

  @parallelism = new
end

#port(new = nil) ⇒ Object



127
128
129
130
131
# File 'lib/zapp/configuration.rb', line 127

def port(new = nil)
  return @port if new.nil?

  @port = new
end

#rackup_file(new = nil) ⇒ Object



133
134
135
136
137
# File 'lib/zapp/configuration.rb', line 133

def rackup_file(new = nil)
  return @rackup_file if new.nil?

  @rackup_file = new
end

#threads_per_worker(new = nil) ⇒ Object



91
92
93
94
95
# File 'lib/zapp/configuration.rb', line 91

def threads_per_worker(new = nil)
  return @threads_per_worker if new.nil?

  @threads_per_worker = new
end

Instance Method Details

#rack_builderObject



63
64
65
66
67
68
69
70
71
# File 'lib/zapp/configuration.rb', line 63

def rack_builder
  @rack_builder ||= begin
    require("rack")
    require("rack/builder")
    Rack::Builder
  rescue LoadError => e
    Zapp::Logger.error("Failed to load Rack #{e}")
  end
end