diff scid/page-api-workers.c @ 18:600204c31bf0

misc: refactor
author David Demelier <markand@malikania.fr>
date Tue, 12 Jul 2022 20:20:51 +0200
parents page-api-workers.c@215c0c3b3609
children f98ea578b1ef
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scid/page-api-workers.c	Tue Jul 12 20:20:51 2022 +0200
@@ -0,0 +1,163 @@
+#include <assert.h>
+
+#include "config.h"
+#include "db.h"
+#include "log.h"
+#include "page-api-workers.h"
+#include "page.h"
+#include "types.h"
+#include "util.h"
+
+static void
+list(struct kreq *r, const struct worker *workers, size_t workersz)
+{
+	struct json_t *doc;
+	char *dump;
+
+	doc = worker_to(workers, workersz);
+	dump = json_dumps(doc, JSON_COMPACT);
+
+	khttp_puts(r, dump);
+	free(dump);
+	json_decref(doc);
+}
+
+static int
+save(const char *json)
+{
+	struct worker res = {0};
+	int ret = -1;
+
+	json_t *doc;
+	json_error_t err;
+
+	if (!(doc = json_loads(json, 0, &err)))
+		log_warn("api/post: invalid JSON input: %s", err.text);
+	else if (worker_from(&res, 1, doc) < 0)
+		log_warn("api/post: failed to decode parameters");
+	else if (db_worker_add(&res) < 0)
+		log_warn("api/post: database save error");
+	else
+		ret = 0;
+
+	json_decref(doc);
+
+	return ret;
+}
+
+static void
+push(struct kreq *r, const struct worker *p)
+{
+	struct json_t *json;
+	char *dump;
+
+	json = worker_to(p, 1);
+	dump = json_dumps(json, JSON_COMPACT);
+
+	khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
+	khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
+	khttp_body(r);
+	khttp_puts(r, dump);
+	khttp_free(r);
+
+	free(dump);
+	json_decref(json);
+}
+
+static void
+get_one(struct kreq *r, const char *name)
+{
+	struct db_ctx ctx;
+	struct worker worker = {
+		.name = name
+	};
+
+	if (db_worker_find(&ctx, &worker) < 0)
+		page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
+	else {
+		push(r, &worker);
+		db_ctx_finish(&ctx);
+	}
+}
+
+static void
+get_one_id(struct kreq *r, int id)
+{
+	struct db_ctx ctx;
+	struct worker worker = {
+		.id = id
+	};
+
+	if (db_worker_find_id(&ctx, &worker) < 0)
+		page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
+	else {
+		push(r, &worker);
+		db_ctx_finish(&ctx);
+	}
+}
+
+static void
+get_all(struct kreq *r)
+{
+	struct db_ctx ctx;
+	struct worker workers[SCI_PROJECT_MAX];
+	ssize_t workersz;
+
+	if ((workersz = db_worker_list(&ctx, workers, UTIL_SIZE(workers))) < 0)
+		page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
+	else {
+		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
+		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
+		khttp_body(r);
+		list(r, workers, workersz);
+		db_ctx_finish(&ctx);
+		khttp_free(r);
+	}
+}
+
+static void
+get(struct kreq *r)
+{
+	char name[128];
+	int id;
+
+	if (sscanf(r->path, "v1/workers/%d", &id) == 1)
+		get_one_id(r, id);
+	else if (sscanf(r->path, "v1/workers/%127s", name) == 1)
+		get_one(r, name);
+	else
+		get_all(r);
+}
+
+static void
+post(struct kreq *r)
+{
+	if (r->fieldsz < 1)
+		page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
+	else if (save(r->fields[0].val) < 0)
+		page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
+	else {
+		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
+		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
+		khttp_body(r);
+		khttp_free(r);
+	}
+}
+
+void
+page_api_v1_workers(struct kreq *r)
+{
+	assert(r);
+
+	switch (r->method) {
+	case KMETHOD_GET:
+		get(r);
+		break;
+	case KMETHOD_POST:
+		post(r);
+		break;
+	default:
+		page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
+		break;
+	}
+}