comparison http.c @ 3:215c0c3b3609

misc: use JSON everywhere (scictl/sciwebd)
author David Demelier <markand@malikania.fr>
date Mon, 14 Jun 2021 22:08:24 +0200
parents 5fa3d2f479b2
children 3051ef92173a
comparison
equal deleted inserted replaced
2:5fa3d2f479b2 3:215c0c3b3609
9 9
10 #include "http.h" 10 #include "http.h"
11 #include "log.h" 11 #include "log.h"
12 #include "page.h" 12 #include "page.h"
13 #include "page-api-jobs.h" 13 #include "page-api-jobs.h"
14 #include "page-api-script.h" 14 #include "page-api-projects.h"
15 #include "page-api-workers.h"
15 #include "req.h" 16 #include "req.h"
16 17
17 enum page { 18 enum page {
18 PAGE_API, 19 PAGE_API,
19 PAGE_LAST /* Not used. */ 20 PAGE_LAST /* Not used. */
24 { 25 {
25 static const struct { 26 static const struct {
26 const char *prefix; 27 const char *prefix;
27 void (*handler)(struct kreq *); 28 void (*handler)(struct kreq *);
28 } apis[] = { 29 } apis[] = {
29 { "v1/jobs", page_api_v1_jobs }, 30 { "v1/jobs", page_api_v1_jobs },
30 { "v1/script", page_api_v1_script }, 31 { "v1/projects", page_api_v1_projects },
31 { NULL, NULL } 32 { "v1/workers", page_api_v1_workers },
33 { NULL, NULL }
32 }; 34 };
33 35
34 if (req_connect(VARDIR "/run/sci.sock") < 0) { 36 for (size_t i = 0; apis[i].prefix; ++i)
35 page(req, NULL, KHTTP_500, KMIME_TEXT_HTML, "pages/500.html"); 37 if (strncmp(req->path, apis[i].prefix, strlen(apis[i].prefix)) == 0)
36 return; 38 return apis[i].handler(req);
37 }
38
39 for (size_t i = 0; apis[i].prefix; ++i) {
40 if (strncmp(req->path, apis[i].prefix, strlen(apis[i].prefix)) == 0) {
41 apis[i].handler(req);
42 goto finish;
43 }
44 }
45 39
46 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); 40 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html");
47
48 finish:
49 req_finish();
50 } 41 }
51 42
52 static const char *pages[] = { 43 static const char *pages[] = {
53 [PAGE_API] = "api" 44 [PAGE_API] = "api"
54 }; 45 };