Class: Vangrail::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/vangrail/server.rb

Overview

Loopback HTTP for step 2: other languages call this process, they do not embed MRI. One connection at a time, stdlib only, same JSON as Front.

Constant Summary collapse

MAX_BODY =
4 * 1024 * 1024
ROUTES =
{
  '/v1/check_input' => 'check_input',
  '/v1/check_output' => 'check_output',
  '/v1/check_context' => 'check_context',
  '/v1/screen' => 'screen',
  '/v1/assess' => 'assess',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(front:, host: '127.0.0.1', port: 0) ⇒ Server

Returns a new instance of Server.



25
26
27
28
29
30
31
# File 'lib/vangrail/server.rb', line 25

def initialize(front:, host: '127.0.0.1', port: 0)
  @front = front
  @host = host
  @port = port
  @server = TCPServer.new(host, port)
  @port = @server.addr[1]
end

Instance Attribute Details

#frontObject (readonly)

Returns the value of attribute front.



23
24
25
# File 'lib/vangrail/server.rb', line 23

def front
  @front
end

#hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/vangrail/server.rb', line 23

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



23
24
25
# File 'lib/vangrail/server.rb', line 23

def port
  @port
end

Class Method Details

.with(front:, **kwargs) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/vangrail/server.rb', line 49

def self.with(front:, **kwargs)
  server = new(front: front, **kwargs).start
  begin
    yield server
  ensure
    server.close
  end
end

Instance Method Details

#base_urlObject



33
34
35
# File 'lib/vangrail/server.rb', line 33

def base_url
  "http://#{host}:#{port}"
end

#closeObject



43
44
45
46
47
# File 'lib/vangrail/server.rb', line 43

def close
  @closing = true
  @server.close unless @server.closed?
  @thread&.kill
end

#startObject



37
38
39
40
41
# File 'lib/vangrail/server.rb', line 37

def start
  @thread = Thread.new { accept_loop }
  @thread.abort_on_exception = false
  self
end