comparison scid/page-api-jobs.c @ 18:600204c31bf0

misc: refactor
author David Demelier <markand@malikania.fr>
date Tue, 12 Jul 2022 20:20:51 +0200
parents page-api-jobs.c@3051ef92173a
children
comparison
equal deleted inserted replaced
17:40fe70256fb0 18:600204c31bf0
1 /*
2 * page-api-jobs.c -- /api/v?/jobs route
3 *
4 * Copyright (c) 2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/types.h>
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include <kcgi.h>
26 #include <jansson.h>
27
28 #include "config.h"
29 #include "db.h"
30 #include "log.h"
31 #include "page-api-jobs.h"
32 #include "page.h"
33 #include "types.h"
34 #include "util.h"
35
36 static void
37 list(struct kreq *r, const struct job *jobs, size_t jobsz)
38 {
39 json_t *doc;
40 char *dump;
41
42 doc = job_to(jobs, jobsz);
43 dump = json_dumps(doc, JSON_COMPACT);
44
45 khttp_puts(r, dump);
46 free(dump);
47 json_decref(doc);
48 }
49
50 static int
51 save(const char *json)
52 {
53 struct jobresult res = {0};
54 int ret = -1;
55
56 json_t *doc;
57 json_error_t err;
58
59 if (!(doc = json_loads(json, 0, &err)))
60 log_warn("api/post: invalid JSON input: %s", err.text);
61 else if (jobresult_from(&res, 1, doc) < 0)
62 log_warn("api/post: failed to decode parameters");
63 else if (db_jobresult_add(&res) < 0)
64 log_warn("api/post: database save error");
65 else
66 ret = 0;
67
68 json_decref(doc);
69
70 return ret;
71 }
72
73 static void
74 get(struct kreq *r)
75 {
76 struct db_ctx ctx;
77 struct job jobs[SCI_JOB_LIST_MAX];
78 ssize_t jobsz;
79 struct worker wk = {
80 .name = util_basename(r->path)
81 };
82
83 if (db_worker_find(&ctx, &wk) < 0) {
84 page(r, NULL, KHTTP_404, KMIME_APP_JSON, NULL);
85 return;
86 }
87
88 db_ctx_finish(&ctx);
89
90 if ((jobsz = db_job_todo(&ctx, jobs, UTIL_SIZE(jobs), wk.id)) < 0)
91 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
92 else {
93 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
94 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
95 khttp_body(r);
96 list(r, jobs, jobsz);
97 db_ctx_finish(&ctx);
98 khttp_free(r);
99 }
100 }
101
102 static void
103 post(struct kreq *r)
104 {
105 if (r->fieldsz < 1)
106 page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
107 else if (save(r->fields[0].val) < 0)
108 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
109 else {
110 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
111 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
112 khttp_body(r);
113 khttp_free(r);
114 }
115 }
116
117 void
118 page_api_v1_jobs(struct kreq *r)
119 {
120 assert(r);
121
122 switch (r->method) {
123 case KMETHOD_GET:
124 get(r);
125 break;
126 case KMETHOD_POST:
127 post(r);
128 break;
129 default:
130 page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
131 break;
132 }
133 }