From e2d67eca52b85d891ec86edbaf6ae6bee0551780 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 6 Sep 2023 14:22:21 +0900 Subject: [PATCH] http_server: v2: reload: Provide GET endpoint to retrieve the count of hot-reloaded Signed-off-by: Hiroshi Hatake --- src/http_server/api/v2/reload.c | 63 +++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/http_server/api/v2/reload.c b/src/http_server/api/v2/reload.c index 83626f11ae2..6f3c2a32b09 100644 --- a/src/http_server/api/v2/reload.c +++ b/src/http_server/api/v2/reload.c @@ -33,22 +33,13 @@ #include -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); @@ -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) {