From 3fb6f96d13b3e83159887076c4eeb0644d62458c Mon Sep 17 00:00:00 2001 From: frosty Date: Sat, 3 Oct 2026 20:01:30 -0400 Subject: feat: add /info endpoint for getting information about an instance --- src/Routes/Info.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Routes/Info.h | 8 +++++++ 2 files changed, 77 insertions(+) create mode 100644 src/Routes/Info.c create mode 100644 src/Routes/Info.h (limited to 'src/Routes') diff --git a/src/Routes/Info.c b/src/Routes/Info.c new file mode 100644 index 0000000..cb5df29 --- /dev/null +++ b/src/Routes/Info.c @@ -0,0 +1,69 @@ +#include "Info.h" +#include "../Config.h" +#include "../Scraping/Scraping.h" +#include "../Utility/Utility.h" +#include +#include +#include +#include + +static int append_json(char *buf, size_t size, size_t *used, const char *format, + ...) { + if (*used >= size) { + return -1; + } + + va_list args; + va_start(args, format); + int written = vsnprintf(buf + *used, size - *used, format, args); + va_end(args); + + if (written < 0 || (size_t)written >= size - *used) { + return -1; + } + + *used += (size_t)written; + return 0; +} + +int info_handler(UrlParams *params) { + (void)params; + extern Config global_config; + + char json[INITIAL_BUFFER_SIZE]; + size_t used = 0; + + if (append_json(json, sizeof(json), &used, "{\"version\":\"%s\",", VERSION) != + 0 || + append_json(json, sizeof(json), &used, "\"git_remote\":\"%s\",", + GIT_REMOTE) != 0 || + append_json(json, sizeof(json), &used, "\"locale\":\"%s\",", + global_config.default_locale) != 0 || + append_json(json, sizeof(json), &used, "\"engines\":[") != 0) { + send_response("Failed to build instance info"); + return -1; + } + + int first = 1; + for (int i = 0; i < ENGINE_COUNT; i++) { + if (!ENGINE_REGISTRY[i].enabled) { + continue; + } + if (append_json(json, sizeof(json), &used, + "%s{\"id\":\"%s\",\"name\":\"%s\"}", first ? "" : ",", + ENGINE_REGISTRY[i].id, ENGINE_REGISTRY[i].name) != 0) { + send_response("Failed to build instance info"); + return -1; + } + first = 0; + } + + if (append_json(json, sizeof(json), &used, "]}") != 0) { + send_response("Failed to build instance info"); + return -1; + } + + serve_data(json, used, "application/json"); + + return 0; +} diff --git a/src/Routes/Info.h b/src/Routes/Info.h new file mode 100644 index 0000000..fee803c --- /dev/null +++ b/src/Routes/Info.h @@ -0,0 +1,8 @@ +#ifndef INFO_H +#define INFO_H + +#include + +int info_handler(UrlParams *params); + +#endif -- cgit v1.3