From ca79e4f79708593ca61f03f45518e8df13d48822 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 28 Jan 2020 10:17:49 -0500 Subject: [PATCH] add /status endpoint --- Docker/build.sh | 2 ++ scripts/get_static_libs.sh | 3 ++- src/index/elastic.c | 25 +++++++++++++++++++++++++ src/index/elastic.h | 2 ++ src/web/serve.c | 18 ++++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Docker/build.sh b/Docker/build.sh index cf34b96..8771533 100755 --- a/Docker/build.sh +++ b/Docker/build.sh @@ -8,3 +8,5 @@ echo "Version ${version}" docker build . -t simon987/sist2:${version} -t simon987/sist2:latest docker push simon987/sist2:${version} docker push simon987/sist2:latest + +docker run --rm -it simon987/sist2 -v \ No newline at end of file diff --git a/scripts/get_static_libs.sh b/scripts/get_static_libs.sh index 96baf17..d8603d3 100755 --- a/scripts/get_static_libs.sh +++ b/scripts/get_static_libs.sh @@ -111,7 +111,8 @@ wget -nc https://curl.haxx.se/download/curl-7.68.0.tar.gz tar -xzf curl-7.68.0.tar.gz cd curl-7.68.0 ./configure --disable-ldap --disable-ldaps --without-librtmp --disable-rtsp --disable-crypto-auth \ - --disable-smtp --enable-static --disable-shared + --disable-smtp --without-libidn2 --without-nghttp2 --without-brotli --enable-static --disable-shared \ + --without-libpsl make -j $THREADS cd .. mv curl-7.68.0/lib/.libs/libcurl.a . diff --git a/src/index/elastic.c b/src/index/elastic.c index d484f42..95e7519 100644 --- a/src/index/elastic.c +++ b/src/index/elastic.c @@ -274,3 +274,28 @@ cJSON *elastic_get_document(const char *uuid_str) { free_response(r); return json; } + +char *elastic_get_status() { + char url[4096]; + snprintf(url, 4096, + "%s/_cluster/state/metadata/sist2?filter_path=metadata.indices.*.state", WebCtx.es_url); + + response_t *r = web_get(url); + cJSON *json = NULL; + char *status = malloc(128 * sizeof(char)); + status[0] = '\0'; + + if (r->status_code == 200) { + json = cJSON_Parse(r->body); + const cJSON *metadata = cJSON_GetObjectItem(json, "metadata"); + if (metadata != NULL) { + const cJSON *indices = cJSON_GetObjectItem(metadata, "indices"); + const cJSON *sist2 = cJSON_GetObjectItem(indices, "sist2"); + const cJSON *state = cJSON_GetObjectItem(sist2, "state"); + strcpy(status, state->valuestring); + } + } + free_response(r); + cJSON_Delete(json); + return status; +} diff --git a/src/index/elastic.h b/src/index/elastic.h index 34ddff6..a0d06cb 100644 --- a/src/index/elastic.h +++ b/src/index/elastic.h @@ -30,4 +30,6 @@ void elastic_init(int force_reset); cJSON *elastic_get_document(const char *uuid_str); +char *elastic_get_status(); + #endif diff --git a/src/web/serve.c b/src/web/serve.c index 37f8fd7..b8c3f53 100644 --- a/src/web/serve.c +++ b/src/web/serve.c @@ -431,6 +431,23 @@ int file(UNUSED(void *p), onion_request *req, onion_response *res) { return ret; } +int status(UNUSED(void *p), UNUSED(onion_request *req), onion_response *res) { + set_default_headers(res); + + onion_response_set_header(res, "Content-Type", "application/x-empty"); + + char *status = elastic_get_status(); + if (strcmp(status, "open") == 0) { + onion_response_set_code(res, 204); + } else { + onion_response_set_code(res, 500); + } + + free(status); + + return OCS_PROCESSED; +} + void serve(const char *hostname, const char *port) { onion *o = onion_new(O_POOL); onion_set_timeout(o, 3500); @@ -450,6 +467,7 @@ void serve(const char *hostname, const char *port) { onion_url_add(urls, "es", search); onion_url_add(urls, "scroll", scroll); + onion_url_add(urls, "status", status); onion_url_add( urls, "^t/([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})/"