Class: RailsAgents::OpenWireInboundController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rails_agents/open_wire_inbound_controller.rb

Overview

Receives open-wire/1 message.inbound POSTs from the Open-Wire gateway. Mounted under the Rails Agents engine (no separate OpenWire::Engine).

POST /agents/open_wire/inbound
Headers: X-Open-Wire-Secret, X-Open-Wire-Protocol, X-Open-Wire-Installation

Resolves agent via:

?agent=order_notification  OR  JSON meta / installation mapping in channels/*.yml

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/rails_agents/open_wire_inbound_controller.rb', line 17

def create
  slug = resolve_agent_slug
  return render json: { error: "agent is required (?agent=slug)" }, status: :bad_request if slug.blank?

  klass = load_agent_class(slug)
  return render json: { error: "Agent not found" }, status: :not_found unless klass

  secret = webhook_secret_for(slug)
  return render json: { error: "Open-Wire webhook secret not configured for agent" }, status: :unauthorized if secret.blank?

  message = RailsAgents::OpenWireAdapter.verify_inbound!(
    secret: secret,
    body: request.raw_post,
    headers: request.headers
  )

  result = dispatch_to_agent(klass, message, slug)

  render json: {
    object: "open_wire_inbound_ack",
    data: {
      agent: slug,
      message_id: message.id,
      thread_id: message.thread_id,
      result: result
    }
  }
rescue ::OpenWire::AuthError => e
  render json: { error: e.message }, status: :unauthorized
rescue ::OpenWire::WebhookError => e
  render json: { error: e.message }, status: :unprocessable_entity
rescue StandardError => e
  Rails.logger.error("[RailsAgents::OpenWireInbound] #{e.class}: #{e.message}\n#{e.backtrace&.first(8)&.join("\n")}")
  render json: { error: "Inbound handler failed" }, status: :internal_server_error
end