Mercurial > sci
view page-api-workers.c @ 17:40fe70256fb0
sciworkerd: fix fchmod declaration
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 06 Oct 2021 16:22:23 +0200 |
parents | 215c0c3b3609 |
children |
line wrap: on
line source
#include <assert.h> #include "config.h" #include "page-api-workers.h" #include "page.h" #include "req.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 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 worker worker; struct req req; if ((req = req_worker_find(&worker, name)).status) page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL); else push(r, &worker); } static void get_one_id(struct kreq *r, int id) { struct worker worker; struct req req; if ((req = req_worker_find_id(&worker, id)).status) page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL); else push(r, &worker); } static void get_all(struct kreq *r) { struct worker workers[SCI_PROJECT_MAX]; struct req req; size_t workersz = UTIL_SIZE(workers); if ((req = req_worker_list(workers, &workersz)).status) 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); req_finish(&req); 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); } void page_api_v1_workers(struct kreq *r) { assert(r); switch (r->method) { case KMETHOD_GET: get(r); break; default: page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL); break; } }