summaryrefslogtreecommitdiff
path: root/common/src/http_healthcheck.cpp
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2024-11-20 14:41:30 +0800
committerluwenpeng <[email protected]>2024-11-20 14:41:30 +0800
commita51344bba32663a6d5a9624072dd31e435eeea09 (patch)
treec58580109dd7c547c171d79fd0fd6b688bfb33dd /common/src/http_healthcheck.cpp
parent1942f792e7e207cf9ea41cff46744d7c95d01a00 (diff)
TSG-23844 请packet adapter提供健康状态查询的HTTP服务v2.1.1-20241120
Diffstat (limited to 'common/src/http_healthcheck.cpp')
-rw-r--r--common/src/http_healthcheck.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/common/src/http_healthcheck.cpp b/common/src/http_healthcheck.cpp
new file mode 100644
index 0000000..30d1fd5
--- /dev/null
+++ b/common/src/http_healthcheck.cpp
@@ -0,0 +1,66 @@
+#include <string.h>
+#include <limits.h>
+#include <pthread.h>
+
+#include "event2/event.h"
+#include "event2/buffer.h"
+#include "event2/http.h"
+#include "http_healthcheck.h"
+#include <MESA/MESA_prof_load.h>
+
+struct http_status_ctx
+{
+ pthread_t pthread;
+ struct event_base *base;
+
+ unsigned int enable;
+ unsigned int port;
+ char addr[PATH_MAX];
+ char path[PATH_MAX];
+ uint64_t is_error;
+} g_ctx = {0};
+
+static void http_callback(struct evhttp_request *req, void *arg)
+{
+ evhttp_send_reply(req, HTTP_OK, "", NULL);
+}
+
+static void *http_healthcheck_thread(void *arg)
+{
+ printf("http_healthcheck_thread running (%s:%d/%s)\n", g_ctx.addr, g_ctx.port, g_ctx.path);
+
+ g_ctx.base = event_base_new();
+ struct evhttp *http = evhttp_new(g_ctx.base);
+ evhttp_set_cb(http, g_ctx.path, http_callback, NULL);
+ evhttp_bind_socket(http, g_ctx.addr, g_ctx.port);
+ event_base_dispatch(g_ctx.base);
+ evhttp_free(http);
+ event_base_free(g_ctx.base);
+
+ printf("http_healthcheck_thread exit\n");
+ return NULL;
+}
+
+void http_healthcheck_server_start(const char *profile)
+{
+ MESA_load_profile_uint_def(profile, "http_healthcheck_server", "enable", &g_ctx.enable, 0);
+ MESA_load_profile_uint_def(profile, "http_healthcheck_server", "port", &g_ctx.port, 8080);
+ MESA_load_profile_string_def(profile, "http_healthcheck_server", "addr", g_ctx.addr, sizeof(g_ctx.addr), "127.0.0.1");
+ MESA_load_profile_string_def(profile, "http_healthcheck_server", "path", g_ctx.path, sizeof(g_ctx.path), "/status");
+
+ if (!g_ctx.enable)
+ {
+ return;
+ }
+
+ pthread_create(&g_ctx.pthread, NULL, http_healthcheck_thread, (void *)&g_ctx);
+}
+
+void http_healthcheck_server_stop()
+{
+ if (g_ctx.enable)
+ {
+ event_base_loopbreak(g_ctx.base);
+ pthread_join(g_ctx.pthread, NULL);
+ }
+}