Fluentd
Search…
Plugin Helper: Http Server
The http_server helper creates an HTTP server. This helper was introduced in v1.6.0.
It supports async-http-based server to improve the performance. If async-http gem is not installed, this helper uses the standard webrick server instead.
Here is an example:
1
require 'fluent/plugin/input'
2
3
module Fluent::Plugin
4
class ExampleInput < Input
5
Fluent::Plugin.register_output('example', self)
6
7
# 1. Load http_server helper
8
helpers :http_server
9
10
config_param :bind, :string
11
config_param :port, :integer
12
13
def start
14
super
15
16
# 2. Create and start HTTP server
17
create_http_server(:example_http_server, addr: @bind, port: @port, logger: log) do |serv|
18
# Define endpoint `/hello` with GET method
19
serv.get('/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello!'] }
20
end
21
end
22
end
23
end
Copied!
NOTE: The launched plugin itself is managed by its plugin helper which stops it automatically. No need to stop it in the stop method.

Methods

create_http_server(title, addr:, port:, logger:, default_app: nil, &block) (deprecated)

This method is deprecated! Use http_server_create_http_server method instead.

http_server_create_http_server(title, addr:, port:, logger:, default_app: nil, proto: nil, tls_opts: nil, &block)

It creates and starts an HTTP server with the given routes defined in &block.
    title: The name of the listening thread. Must be unique!
    addr: The address to listen to.
    port: The port to listen to.
    logger: The logger used in the server helper.
    default_app: The object to handle the requests with unregistered paths. This
    object must have a #call method or must be a Proc object.
    proto: Protocol type. Supported values: {:tcp, :tls} (default: :tcp)
    tls_opts: TLS options. Same as the Server Helper's

http_server_create_https_server(title, addr:, port:, logger:, default_app: nil, tls_opts: nil, &block)

It creates and starts an HTTPS server with the given routes defined in &block.
    title: The name of the listening thread. Must be unique!
    addr: The address to listen to.
    port: The port to listen to.
    logger: The logger used in the server helper.
    default_app: The object to handle the requests with unregistered paths. This
    object must have a #call method or must be a Proc object.
    tls_opts: TLS options. Same as the Server Helper's

Handling of other HTTP Methods

1
create_http_server(:example_http_server, addr: @bind, port: @port, logger: log) do |serv|
2
# define POST method `/hello`
3
serv.post('/hello') { [200, { 'Content-Type' => 'text/plain' }, 'hello!'] }
4
5
# define HEAD method `/hello`
6
serv.head('/hello') { [200, { 'Content-Type' => 'text/plain' }, nil] }
7
end
Copied!

Request and Response

Request

Request supports these following methods:
    query_string: returns query string like hoge=v1&fuga=v2
    query: returns query which is query_string parsed by CGI.parse
    body: returns the request body
    path: returns the request path

Response

The http_server helper expects an array as the return value i.e.:
1
[${response_status}, ${headers}, ${body}]
Copied!
    ${response_status} should be an Integer
    ${headers} should be a Hash
    ${body} should be a String or nil

Example

Here is an example of request and response in JSON format:
1
create_http_server(:example_json_http_server, addr: @bind, port: @port, logger: log) do |serv|
2
serv.post('/hello.json') do |req|
3
body = JSON.parse(req.body)
4
log.info(body)
5
6
[code, { 'Content-Type' => 'application/json' }, { 'status' => 'success' }.to_json]
7
end
8
end
Copied!

Plugins using http_server

If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4mo ago