Class: Fluent::PluginHelper::HttpServer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin_helper/http_server/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(addr:, port:, logger:, default_app: nil, tls_context: nil) ⇒ Server

Returns a new instance of Server.

Parameters:

  • logger (Logger)
  • default_app (Object) (defaults to: nil)

    This method must have #call.

  • tls_context (OpenSSL::SSL::SSLContext) (defaults to: nil)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin_helper/http_server/server.rb', line 32

def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil)
  @addr = addr
  @port = port
  @logger = logger

  # TODO: support http2
  scheme = tls_context ? 'https' : 'http'
  @uri = URI("#{scheme}://#{@addr}:#{@port}").to_s
  @router = Router.new(default_app)
  @reactor = Async::Reactor.new(nil, logger: @logger)

  opts = if tls_context
           { ssl_context: tls_context }
         else
           {}
         end
  @server = Async::HTTP::Server.new(App.new(@router, @logger), Async::HTTP::Endpoint.parse(@uri, **opts))

  if block_given?
    yield(self)
  end
end

Instance Method Details

#start(notify = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin_helper/http_server/server.rb', line 55

def start(notify = nil)
  @logger.debug("Start async HTTP server listening #{@uri}")
  task = @reactor.run do
    @server.run

    if notify
      notify.push(:ready)
    end
  end

  task.stop
  @logger.debug('Finished HTTP server')
end

#stopObject



69
70
71
72
73
74
75
# File 'lib/fluent/plugin_helper/http_server/server.rb', line 69

def stop
  @logger.debug('closing HTTP server')

  if @reactor
    @reactor.stop
  end
end