aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosty <frostinator33@airmail.cc>2026-10-03 20:01:30 -0400
committerfrosty <frostinator33@airmail.cc>2026-10-03 20:01:30 -0400
commit3fb6f96d13b3e83159887076c4eeb0644d62458c (patch)
treead9a989284581e7b558a2b225d87a8e28b1089d1
parenta17f0238213ffb24a183117148252b1c60a8514c (diff)
feat: add /info endpoint for getting information about an instanceHEADmaster
-rw-r--r--src/Main.c2
-rw-r--r--src/Routes/Info.c69
-rw-r--r--src/Routes/Info.h8
3 files changed, 79 insertions, 0 deletions
diff --git a/src/Main.c b/src/Main.c
index 55464db..8016c65 100644
--- a/src/Main.c
+++ b/src/Main.c
@@ -12,6 +12,7 @@
#include "Routes/Home.h"
#include "Routes/ImageProxy.h"
#include "Routes/Images.h"
+#include "Routes/Info.h"
#include "Routes/Search.h"
#include "Routes/Settings.h"
#include "Routes/SettingsSave.h"
@@ -138,6 +139,7 @@ int main() {
set_handler("/search", results_handler);
set_handler("/images", images_handler);
set_handler("/proxy", image_proxy_handler);
+ set_handler("/info", info_handler);
set_handler("/settings", settings_handler);
set_handler("/save_settings", settings_save_handler);
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 <beaker.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+
+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 <beaker.h>
+
+int info_handler(UrlParams *params);
+
+#endif