Class: Falcon::Command::Serve

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/falcon/command/serve.rb

Instance Method Summary collapse

Instance Method Details

#cache?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/falcon/command/serve.rb', line 81

def cache?
  @options[:cache]
end

#callObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/falcon/command/serve.rb', line 128

def call
  Async.logger.info(self) do |buffer|
    buffer.puts "Falcon v#{VERSION} taking flight! Using #{self.container_class} #{self.container_options}."
    buffer.puts "- Binding to: #{self.endpoint}"
    buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}"
    buffer.puts "- To reload configuration: kill -HUP #{Process.pid}"
  end
  
  if path = @options[:preload]
    full_path = File.expand_path(path)
    load(full_path)
  end
  
  Bundler.require(:preload)
  
  if GC.respond_to?(:compact)
    GC.compact
  end
  
  self.controller.run
end

#clientObject



120
121
122
# File 'lib/falcon/command/serve.rb', line 120

def client
  Async::HTTP::Client.new(client_endpoint)
end

#client_endpointObject



116
117
118
# File 'lib/falcon/command/serve.rb', line 116

def client_endpoint
  Async::HTTP::Endpoint.parse(@options[:bind], **endpoint_options)
end

#container_classObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/falcon/command/serve.rb', line 66

def container_class
  case @options[:container]
  when :threaded
    return Async::Container::Threaded
  when :forked
    return Async::Container::Forked
  when :hybrid
    return Async::Container::Hybrid
  end
end

#container_optionsObject



104
105
106
# File 'lib/falcon/command/serve.rb', line 104

def container_options
  slice_options(:count, :forks, :threads)
end

#controllerObject



124
125
126
# File 'lib/falcon/command/serve.rb', line 124

def controller
  Controller::Serve.new(self)
end

#endpointObject



112
113
114
# File 'lib/falcon/command/serve.rb', line 112

def endpoint
  Endpoint.parse(@options[:bind], **endpoint_options)
end

#endpoint_optionsObject



108
109
110
# File 'lib/falcon/command/serve.rb', line 108

def endpoint_options
  slice_options(:hostname, :port, :reuse_port, :timeout)
end

#load_appObject



85
86
87
88
89
# File 'lib/falcon/command/serve.rb', line 85

def load_app
  rack_app, _ = Rack::Builder.parse_file(@options[:config])
  
  return Server.middleware(rack_app, verbose: self.verbose?, cache: self.cache?)
end

#slice_options(*keys) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/falcon/command/serve.rb', line 91

def slice_options(*keys)
  # TODO: Ruby 2.5 introduced Hash#slice
  options = {}
  
  keys.each do |key|
    if @options.key?(key)
      options[key] = @options[key]
    end
  end
  
  return options
end

#verbose?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/falcon/command/serve.rb', line 77

def verbose?
  @parent&.verbose?
end