Class: Desiru::API::PersistenceMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/desiru/api/persistence_middleware.rb

Overview

Rack middleware for tracking API requests and module executions

Instance Method Summary collapse

Constructor Details

#initialize(app, enabled: true) ⇒ PersistenceMiddleware

Returns a new instance of PersistenceMiddleware.



9
10
11
12
# File 'lib/desiru/api/persistence_middleware.rb', line 9

def initialize(app, enabled: true)
  @app = app
  @enabled = enabled
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/desiru/api/persistence_middleware.rb', line 14

def call(env)
  return @app.call(env) unless @enabled && persistence_available?

  start_time = Time.now

  # Create request record
  request = Rack::Request.new(env)

  # Call the app
  status, headers, body = @app.call(env)

  # Calculate response time
  response_time = Time.now - start_time

  # Store the request and response
  store_request(request, status, headers, body, response_time)

  # Return the response
  [status, headers, body]
rescue StandardError => e
  # Log error but don't fail the request
  warn "PersistenceMiddleware error: #{e.message}"
  [status, headers, body] || [500, {}, ['Internal Server Error']]
end