Class: Nanobot::Channels::Gateway

Inherits:
BaseChannel show all
Defined in:
lib/nanobot/channels/gateway.rb

Overview

HTTP Gateway channel exposing a synchronous /chat REST endpoint via WEBrick. Supports optional Bearer token authentication and a /health endpoint.

Instance Attribute Summary

Attributes inherited from BaseChannel

#bus, #config, #logger, #name

Instance Method Summary collapse

Methods inherited from BaseChannel

#allowed?, #initialize, #running?

Constructor Details

This class inherits a constructor from Nanobot::Channels::BaseChannel

Instance Method Details

#send(message) ⇒ Object

Deliver a response to the waiting HTTP request by pushing onto its queue.

Parameters:



39
40
41
42
# File 'lib/nanobot/channels/gateway.rb', line 39

def send(message)
  queue = @queues_mutex.synchronize { @response_queues.delete(message.chat_id) }
  queue&.push(message)
end

#start ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nanobot/channels/gateway.rb', line 13

def start
  @running = true
  @response_queues = {}
  @queues_mutex = Mutex.new

  @server = WEBrick::HTTPServer.new(
    Port: @config.port,
    BindAddress: @config.host,
    Logger: WEBrick::Log.new(IO::NULL),
    AccessLog: []
  )

  @server.mount_proc('/chat') { |req, res| handle_http_request(req, res) }
  @server.mount_proc('/health') { |_req, res| handle_health(res) }

  @logger.info "Gateway listening on #{@config.host}:#{@config.port}"
  @server.start
end

#stop ⇒ Object



32
33
34
35
# File 'lib/nanobot/channels/gateway.rb', line 32

def stop
  @running = false
  @server&.shutdown
end