comparison 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
comparison
equal deleted inserted replaced
17:40fe70256fb0 18:600204c31bf0
1 #include <assert.h>
2
3 #include "config.h"
4 #include "db.h"
5 #include "log.h"
6 #include "page-api-workers.h"
7 #include "page.h"
8 #include "types.h"
9 #include "util.h"
10
11 static void
12 list(struct kreq *r, const struct worker *workers, size_t workersz)
13 {
14 struct json_t *doc;
15 char *dump;
16
17 doc = worker_to(workers, workersz);
18 dump = json_dumps(doc, JSON_COMPACT);
19
20 khttp_puts(r, dump);
21 free(dump);
22 json_decref(doc);
23 }
24
25 static int
26 save(const char *json)
27 {
28 struct worker res = {0};
29 int ret = -1;
30
31 json_t *doc;
32 json_error_t err;
33
34 if (!(doc = json_loads(json, 0, &err)))
35 log_warn("api/post: invalid JSON input: %s", err.text);
36 else if (worker_from(&res, 1, doc) < 0)
37 log_warn("api/post: failed to decode parameters");
38 else if (db_worker_add(&res) < 0)
39 log_warn("api/post: database save error");
40 else
41 ret = 0;
42
43 json_decref(doc);
44
45 return ret;
46 }
47
48 static void
49 push(struct kreq *r, const struct worker *p)
50 {
51 struct json_t *json;
52 char *dump;
53
54 json = worker_to(p, 1);
55 dump = json_dumps(json, JSON_COMPACT);
56
57 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
58 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
59 khttp_body(r);
60 khttp_puts(r, dump);
61 khttp_free(r);
62
63 free(dump);
64 json_decref(json);
65 }
66
67 static void
68 get_one(struct kreq *r, const char *name)
69 {
70 struct db_ctx ctx;
71 struct worker worker = {
72 .name = name
73 };
74
75 if (db_worker_find(&ctx, &worker) < 0)
76 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
77 else {
78 push(r, &worker);
79 db_ctx_finish(&ctx);
80 }
81 }
82
83 static void
84 get_one_id(struct kreq *r, int id)
85 {
86 struct db_ctx ctx;
87 struct worker worker = {
88 .id = id
89 };
90
91 if (db_worker_find_id(&ctx, &worker) < 0)
92 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
93 else {
94 push(r, &worker);
95 db_ctx_finish(&ctx);
96 }
97 }
98
99 static void
100 get_all(struct kreq *r)
101 {
102 struct db_ctx ctx;
103 struct worker workers[SCI_PROJECT_MAX];
104 ssize_t workersz;
105
106 if ((workersz = db_worker_list(&ctx, workers, UTIL_SIZE(workers))) < 0)
107 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
108 else {
109 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
110 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
111 khttp_body(r);
112 list(r, workers, workersz);
113 db_ctx_finish(&ctx);
114 khttp_free(r);
115 }
116 }
117
118 static void
119 get(struct kreq *r)
120 {
121 char name[128];
122 int id;
123
124 if (sscanf(r->path, "v1/workers/%d", &id) == 1)
125 get_one_id(r, id);
126 else if (sscanf(r->path, "v1/workers/%127s", name) == 1)
127 get_one(r, name);
128 else
129 get_all(r);
130 }
131
132 static void
133 post(struct kreq *r)
134 {
135 if (r->fieldsz < 1)
136 page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
137 else if (save(r->fields[0].val) < 0)
138 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
139 else {
140 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
141 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
142 khttp_body(r);
143 khttp_free(r);
144 }
145 }
146
147 void
148 page_api_v1_workers(struct kreq *r)
149 {
150 assert(r);
151
152 switch (r->method) {
153 case KMETHOD_GET:
154 get(r);
155 break;
156 case KMETHOD_POST:
157 post(r);
158 break;
159 default:
160 page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
161 break;
162 }
163 }