Mercurial > sci
comparison scid/page-api-todo.c @ 20:f98ea578b1ef
misc: revamp database
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 19 Jul 2022 21:52:42 +0200 |
parents | scid/page-api-jobs.c@600204c31bf0 |
children | dd078aea5d02 |
comparison
equal
deleted
inserted
replaced
19:de4bf839b565 | 20:f98ea578b1ef |
---|---|
1 /* | |
2 * page-api-todo.c -- /api/v?/todo 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-todo.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 #if 0 | |
51 | |
52 static int | |
53 save(const char *json) | |
54 { | |
55 struct jobresult res = {0}; | |
56 int ret = -1; | |
57 | |
58 json_t *doc; | |
59 json_error_t err; | |
60 | |
61 if (!(doc = json_loads(json, 0, &err))) | |
62 log_warn("api/post: invalid JSON input: %s", err.text); | |
63 else if (jobresult_from(&res, 1, doc) < 0) | |
64 log_warn("api/post: failed to decode parameters"); | |
65 else if (db_jobresult_add(&res) < 0) | |
66 log_warn("api/post: database save error"); | |
67 else | |
68 ret = 0; | |
69 | |
70 json_decref(doc); | |
71 | |
72 return ret; | |
73 } | |
74 | |
75 #endif | |
76 | |
77 /* | |
78 * GET /api/v1/todo/<worker-name> | |
79 * ---------------- | |
80 * | |
81 * Retrieve a list of jobs to perform for this worker name. | |
82 */ | |
83 static void | |
84 get(struct kreq *r) | |
85 { | |
86 struct job jobs[SCI_JOB_LIST_MAX]; | |
87 ssize_t jobsz; | |
88 struct worker wk = {0}; | |
89 | |
90 if (db_worker_find(&wk, util_basename(r->path)) < 0) { | |
91 page(r, NULL, KHTTP_404, KMIME_APP_JSON, NULL); | |
92 return; | |
93 } | |
94 | |
95 if ((jobsz = db_job_todo(jobs, UTIL_SIZE(jobs), wk.id)) < 0) | |
96 page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL); | |
97 else { | |
98 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]); | |
99 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); | |
100 khttp_body(r); | |
101 list(r, jobs, jobsz); | |
102 khttp_free(r); | |
103 } | |
104 | |
105 for (ssize_t i = 0; i < jobsz; ++i) | |
106 job_finish(&jobs[i]); | |
107 } | |
108 | |
109 void | |
110 page_api_v1_jobs(struct kreq *r) | |
111 { | |
112 assert(r); | |
113 | |
114 switch (r->method) { | |
115 case KMETHOD_GET: | |
116 get(r); | |
117 break; | |
118 default: | |
119 page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL); | |
120 break; | |
121 } | |
122 } |