Skip to content

Commit

Permalink
http_server: v2: reload: Provide GET endpoint to retrieve the count o…
Browse files Browse the repository at this point in the history
…f hot-reloaded

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Sep 6, 2023
1 parent 2aed767 commit e2d67ec
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions src/http_server/api/v2/reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@

#include <fluent-bit/flb_http_server.h>

static void cb_reload(mk_request_t *request, void *data)
static void handle_reload_request(mk_request_t *request, struct flb_config *config)
{
int ret;
flb_sds_t out_buf;
size_t out_size;
msgpack_packer mp_pck;
msgpack_sbuffer mp_sbuf;
struct flb_hs *hs = data;
struct flb_config *config = hs->config;

if (request->method != MK_METHOD_POST &&
request->method != MK_METHOD_PUT) {
mk_http_status(request, 400);
mk_http_done(request);
return;
}

/* initialize buffers */
msgpack_sbuffer_init(&mp_sbuf);
Expand Down Expand Up @@ -109,6 +100,58 @@ static void cb_reload(mk_request_t *request, void *data)
flb_sds_destroy(out_buf);
}

static void handle_get_reload_status(mk_request_t *request, struct flb_config *config)
{
flb_sds_t out_buf;
size_t out_size;
msgpack_packer mp_pck;
msgpack_sbuffer mp_sbuf;

/* initialize buffers */
msgpack_sbuffer_init(&mp_sbuf);
msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write);

msgpack_pack_map(&mp_pck, 1);
msgpack_pack_str(&mp_pck, 12);
msgpack_pack_str_body(&mp_pck, "hot_reloaded", 12);
msgpack_pack_int64(&mp_pck, config->hot_reloaded_count);

/* Export to JSON */
out_buf = flb_msgpack_raw_to_json_sds(mp_sbuf.data, mp_sbuf.size);
msgpack_sbuffer_destroy(&mp_sbuf);
if (!out_buf) {
mk_http_status(request, 400);
mk_http_done(request);
return;
}
out_size = flb_sds_len(out_buf);

mk_http_status(request, 200);
flb_hs_add_content_type_to_req(request, FLB_HS_CONTENT_TYPE_JSON);
mk_http_send(request, out_buf, out_size, NULL);
mk_http_done(request);

flb_sds_destroy(out_buf);
}

static void cb_reload(mk_request_t *request, void *data)
{
struct flb_hs *hs = data;
struct flb_config *config = hs->config;

if (request->method == MK_METHOD_POST ||
request->method == MK_METHOD_PUT) {
handle_reload_request(request, config);
}
else if (request->method == MK_METHOD_GET) {
handle_get_reload_status(request, config);
}
else {
mk_http_status(request, 400);
mk_http_done(request);
}
}

/* Perform registration */
int api_v2_reload(struct flb_hs *hs)
{
Expand Down

0 comments on commit e2d67ec

Please sign in to comment.