Class: Bridgetown::Rack::Server

Inherits:
Object
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/rack/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, port: nil, bind: nil) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'bridgetown-core/lib/bridgetown-core/rack/server.rb', line 8

def initialize(config, port: nil, bind: nil)
  @name = nil
  @port = port
  @bind = bind

  # Evaluate the configuration first
  if File.exist?(config)
    config = File.read(config)
    instance_eval(config)
  end

  # If no server is found, try and automatically detect one
  if @name.nil? && detected_server
    @name = detected_server.to_s.downcase
    include_default_environment
  end

  # Raise error if no server can be found
  if @name.nil?
    raise "No suitable Rack server was found."
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

rubocop:disable Style/MissingRespondToMissing



75
76
77
78
79
80
81
82
83
# File 'bridgetown-core/lib/bridgetown-core/rack/server.rb', line 75

def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondToMissing
  return super unless @name.nil?

  if block
    self.class.define_method(name, &block)
  else
    self.class.define_method(name) { args.first }
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'bridgetown-core/lib/bridgetown-core/rack/server.rb', line 6

def name
  @name
end

Class Method Details

.new(config = "config/web_server.rb", port: nil, bind: nil) ⇒ Object

We include the server’s environment module into the class during initialization. Hence, we create a duplicate before instantiating the class, so every time the server is created, it’s a clean slate.



35
36
37
38
39
# File 'bridgetown-core/lib/bridgetown-core/rack/server.rb', line 35

def self.new(config = "config/web_server.rb", port: nil, bind: nil)
  return super unless self == Bridgetown::Rack::Server

  dup.new(config, port: port, bind: bind)
end