Module: Lepus::Web

Defined in:
lib/lepus/web.rb,
lib/lepus/web/api.rb,
lib/lepus/web/app.rb,
lib/lepus/web/aggregator.rb,
lib/lepus/web/respond_with.rb,
lib/lepus/web/management_api.rb

Defined Under Namespace

Modules: ConfigExtensions, ConsumerExtensions, HandlerExtensions, WorkerExtensions Classes: API, Aggregator, App, ManagementAPI, RespondWith

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.aggregatorObject

Returns the value of attribute aggregator.



141
142
143
# File 'lib/lepus/web.rb', line 141

def aggregator
  @aggregator
end

.management_apiObject

Returns the value of attribute management_api.



142
143
144
# File 'lib/lepus/web.rb', line 142

def management_api
  @management_api
end

Class Method Details

.assets_pathObject



145
146
147
# File 'lib/lepus/web.rb', line 145

def self.assets_path
  @assets_path ||= Pathname.new(File.expand_path("../../", __dir__)).join("web")
end

.base_path(env) ⇒ Object



187
188
189
190
191
# File 'lib/lepus/web.rb', line 187

def self.base_path(env)
  script_name = env["SCRIPT_NAME"].to_s
  script_name = script_name.chomp("/")
  "#{script_name}/"
end

.call(env) ⇒ Object

Make the Web module directly mountable as a Rack application.

Mounting via ‘mount Lepus::Web => “/lepus”` in routes.rb does not call `Lepus::Web.start` — Rails just stashes a reference to this module and dispatches requests to `.call`. So we lazily start the aggregator and management API on first request and memoize the Rack app. This only runs in processes that actually serve HTTP for the dashboard; the supervisor loads `routes.rb` during boot but never dispatches requests, so it never pays this cost.



217
218
219
220
# File 'lib/lepus/web.rb', line 217

def self.call(env)
  ensure_started
  (@rack_app ||= App.build).call(env)
end

.ensure_startedObject



222
223
224
225
226
227
228
229
# File 'lib/lepus/web.rb', line 222

def self.ensure_started
  return if @rack_app && aggregator&.running?

  @boot_mutex ||= Mutex.new
  @boot_mutex.synchronize do
    start unless aggregator&.running?
  end
end

.mime_for(path) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/lepus/web.rb', line 193

def self.mime_for(path)
  case File.extname(path)
  when ".html" then "text/html"
  when ".css" then "text/css"
  when ".js" then "application/javascript"
  when ".png" then "image/png"
  when ".jpg", ".jpeg" then "image/jpeg"
  when ".svg" then "image/svg+xml"
  when ".woff", ".woff2" then "font/woff"
  when ".ttf" then "font/ttf"
  when ".eot" then "application/vnd.ms-fontobject"
  else "application/octet-stream"
  end
end

.render_index(env) ⇒ Object



181
182
183
184
185
# File 'lib/lepus/web.rb', line 181

def self.render_index(env)
  base = base_path(env)
  html = File.read(assets_path.join("index.html"))
  html.gsub("__BASE_PATH__", base)
end

.startObject

Start all web services (aggregator and management API)



170
171
172
173
# File 'lib/lepus/web.rb', line 170

def self.start
  start_aggregator
  start_management_api
end

.start_aggregatorObject



149
150
151
152
153
154
# File 'lib/lepus/web.rb', line 149

def self.start_aggregator
  return if aggregator&.running?

  self.aggregator = Aggregator.new
  aggregator.start
end

.start_management_apiObject



161
162
163
# File 'lib/lepus/web.rb', line 161

def self.start_management_api
  self.management_api = Lepus.config.build_management_api
end

.stopObject

Stop all web services



176
177
178
179
# File 'lib/lepus/web.rb', line 176

def self.stop
  stop_aggregator
  stop_management_api
end

.stop_aggregatorObject



156
157
158
159
# File 'lib/lepus/web.rb', line 156

def self.stop_aggregator
  aggregator&.stop
  self.aggregator = nil
end

.stop_management_apiObject



165
166
167
# File 'lib/lepus/web.rb', line 165

def self.stop_management_api
  self.management_api = nil
end