Skip to content
Kward Search API index

Class: Kward::PluginHTTPServer

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/plugins/http_server.rb

Overview

Small, plugin-owned HTTP listener constrained to loopback.

Constant Summary collapse

DEFAULT_MAX_BODY_BYTES =
1_048_576
DEFAULT_REQUEST_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "127.0.0.1", port: 0, max_body_bytes: DEFAULT_MAX_BODY_BYTES, request_timeout: DEFAULT_REQUEST_TIMEOUT, max_clients: 8, &handler) ⇒ PluginHTTPServer

Returns a new instance of PluginHTTPServer.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kward/plugins/http_server.rb', line 12

def initialize(host: "127.0.0.1", port: 0, max_body_bytes: DEFAULT_MAX_BODY_BYTES, request_timeout: DEFAULT_REQUEST_TIMEOUT, max_clients: 8, &handler)
  raise ArgumentError, "Plugin HTTP server requires a handler" unless handler
  raise ArgumentError, "Plugin HTTP server must bind to loopback" unless IPAddr.new(host).loopback?
  raise ArgumentError, "Plugin HTTP server port must be between 0 and 65535" unless Integer(port).between?(0, 65_535)
  raise ArgumentError, "Plugin HTTP server body limit must be positive" unless Integer(max_body_bytes).positive?
  raise ArgumentError, "Plugin HTTP server request timeout must be positive" unless Float(request_timeout).positive?
  raise ArgumentError, "Plugin HTTP server client limit must be positive" unless Integer(max_clients).positive?

  @host = host
  @requested_port = Integer(port)
  @handler = handler
  @server = WEBrick::HTTPServer.new(
    BindAddress: host,
    Port: @requested_port,
    MaxReqBytes: Integer(max_body_bytes),
    RequestTimeout: Float(request_timeout),
    MaxClients: Integer(max_clients),
    AccessLog: [],
    Logger: WEBrick::Log.new(File::NULL)
  )
  @thread = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#host ⇒ Object (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/kward/plugins/http_server.rb', line 10

def host
  @host
end

#requested_port ⇒ Object (readonly)

Returns the value of attribute requested_port.



10
11
12
# File 'lib/kward/plugins/http_server.rb', line 10

def requested_port
  @requested_port
end

Instance Method Details

#port ⇒ Object



63
64
65
# File 'lib/kward/plugins/http_server.rb', line 63

def port
  @port || requested_port
end

#start ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kward/plugins/http_server.rb', line 36

def start
  @mutex.synchronize do
    return self if @thread&.alive?

    @server.mount_proc("/") { |request, response| @handler.call(request, response) }
    @thread = Thread.new { @server.start }
    @thread.report_on_exception = false
  end
  wait_until_ready
  self
end

#stop ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/kward/plugins/http_server.rb', line 52

def stop
  thread = @mutex.synchronize do
    current = @thread
    @server.shutdown if current&.alive?
    @thread = nil
    current
  end
  thread&.join(2)
  self
end

#wait ⇒ Object



48
49
50
# File 'lib/kward/plugins/http_server.rb', line 48

def wait
  @thread&.join
end