comparison page-index.c @ 51:07b6887d3557

pasterd: split into individual pages While here add a similar mini.css theme as in imgup.
author David Demelier <markand@malikania.fr>
date Mon, 21 Dec 2020 18:22:44 +0100
parents
children fba88439ec0a
comparison
equal deleted inserted replaced
50:520f57836ff3 51:07b6887d3557
1 /*
2 * page-index.c -- page /
3 *
4 * Copyright (c) 2020 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
24 #include <kcgi.h>
25
26 #include "database.h"
27 #include "fragment-paste.h"
28 #include "page-index.h"
29 #include "page.h"
30 #include "paste.h"
31 #include "util.h"
32
33 struct template {
34 struct kreq *req;
35 const struct paste *pastes;
36 size_t pastesz;
37 };
38
39 static const char *keywords[] = {
40 "pastes"
41 };
42
43 static int
44 template(size_t keyword, void *arg)
45 {
46 struct template *tp = arg;
47
48 switch (keyword) {
49 case 0:
50 for (size_t i = 0; i < tp->pastesz; ++i)
51 fragment_paste(tp->req, &tp->pastes[i]);
52 break;
53 default:
54 break;
55 }
56
57 return 1;
58 }
59
60 static void
61 get(struct kreq *r)
62 {
63 struct paste pastes[10] = {0};
64 size_t pastesz = NELEM(pastes);
65
66 if (!database_recents(pastes, &pastesz))
67 page(r, NULL, KHTTP_500, "pages/500.html");
68 else
69 page_index_render(r, pastes, pastesz);
70
71 for (size_t i = 0; i < pastesz; ++i)
72 paste_finish(&pastes[i]);
73 }
74
75 void
76 page_index_render(struct kreq *r, const struct paste *pastes, size_t pastesz)
77 {
78 struct template data = {
79 .req = r,
80 .pastes = pastes,
81 .pastesz = pastesz
82 };
83
84 struct ktemplate kt = {
85 .key = keywords,
86 .keysz = NELEM(keywords),
87 .arg = &data,
88 .cb = template
89 };
90
91 page(r, &kt, KHTTP_200, "pages/index.html");
92 }
93
94 void
95 page_index(struct kreq *r)
96 {
97 switch (r->method) {
98 case KMETHOD_GET:
99 get(r);
100 break;
101 default:
102 page(r, NULL, KHTTP_400, "400.html");
103 break;
104 }
105 }