feat: add /info endpoint for getting information about an instance
This commit is contained in:
parent
a17f023821
commit
3fb6f96d13
3 changed files with 79 additions and 0 deletions
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
69
src/Routes/Info.c
Normal file
69
src/Routes/Info.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
8
src/Routes/Info.h
Normal file
8
src/Routes/Info.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef INFO_H
|
||||
#define INFO_H
|
||||
|
||||
#include <beaker.h>
|
||||
|
||||
int info_handler(UrlParams *params);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in a new issue