aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
blob: 767d7313b2de7ba2a109c5cc9a9149a2f3e4d1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <kore/kore.h>
#include <kore/http.h>
#include <kore/pgsql.h>
#include <kore/seccomp.h>

#include "assets.h"

#include "../lib/md4c/src/entity.h"
#include "../lib/md4c/src/entity.c"
#include "../lib/md4c/src/md4c.h"
#include "../lib/md4c/src/md4c.c"
#include "../lib/md4c/src/md4c-html.h"
#include "../lib/md4c/src/md4c-html.c"

KORE_SECCOMP_FILTER("clog",
    KORE_SYSCALL_ALLOW(getdents64),
    KORE_SYSCALL_ALLOW(sendmmsg),
    KORE_SYSCALL_ALLOW(uname)
)

int init(int);
int post(struct http_request *);
int posts(struct http_request *);
int render_posts_html(struct http_request *, const char *);
int render_posts_json(struct http_request *, const char *);
static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
static int render_md(char *, struct kore_buf *);

static const char *database = "db";

// Called when this module is loaded (see config)
int
init(int state)
{
	// Register the database
	return kore_pgsql_register(
		database,
		"host=postgres port=5432 user=postgres password=postgres dbname=clog sslmode=disable"
	);
}

int
post(struct http_request *req)
{
	int err;

	const char *accept, *resource;

	resource = req->path + strlen("/posts/");
	kore_log(LOG_DEBUG, "Resource /posts/%s", resource);

	err = http_request_header(req, "accept", &accept);
	if (err == KORE_RESULT_OK) {
		kore_log(LOG_DEBUG, "Accept: %s", accept);
		if (strcmp(accept, "application/json") == 0) {
			return render_posts_json(req, resource);
		}
	}

	return render_posts_html(req, resource);
}

int
posts(struct http_request *req)
{
	int err;

	const char *accept;

	err = http_request_header(req, "accept", &accept);
	err = http_request_header(req, "accept", &accept);
	if (err == KORE_RESULT_OK) {
		kore_log(LOG_DEBUG, "Accept: %s", accept);
		if (strcmp(accept, "application/json") == 0) {
			return render_posts_json(req, NULL);
		}
	}

	return render_posts_html(req, NULL);
}

int
render_posts_html(struct http_request *req, const char *resource)
{
	// Errors
	int err;

	// Buffer for response
	struct kore_buf *resp_buf = kore_buf_alloc(0);

	// SQL vars
	struct kore_pgsql sql;
	int row, rows;

	// Post attributes/query results.
	char *id, *title, *created_at, *body;

	// Start writing HTML.
	kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html);

	// Setup SQL
	kore_pgsql_init(&sql);

	// Initialise our kore_pgsql data structure with the database name
	// we want to connect to (note that we registered this earlier with
	// kore_pgsql_register()). We also say we will perform a synchronous
	// query (KORE_PGSQL_SYNC).
	err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
	if (err == KORE_RESULT_ERROR)
	{
		kore_buf_appendf(
			resp_buf,
			(const char *) asset_error_html,
			"There was an error processing the request."
		);
		kore_pgsql_logerror(&sql);
		goto out;
	}

	// Query for posts, check for error.
	if (resource) {
		// Query a post.
		err = kore_pgsql_query_params(
			&sql,
			"SELECT id, title, created_at::DATE, body FROM posts "
			"WHERE id = $1;",
			0,  // return string data
			1,  // param count
			KORE_PGSQL_PARAM_TEXT(resource)
		);
	} else {
		// Query all posts.
		err = kore_pgsql_query(
			&sql,
			"SELECT id, title, created_at::DATE, body FROM posts "
			"ORDER BY updated_at DESC;"
		);
	}
	if (err == KORE_RESULT_ERROR) {
		kore_buf_appendf(
			resp_buf,
			(const char *) asset_error_html,
			"There was an error processing the request."
		);
		kore_pgsql_logerror(&sql);
		goto out;
	}

	// Iterate over posts and render them.
	rows = kore_pgsql_ntuples(&sql);
	for (row = 0; row < rows; row++) {
		id = kore_pgsql_getvalue(&sql, row, 0);
		title = kore_pgsql_getvalue(&sql, row, 1);
		created_at = kore_pgsql_getvalue(&sql, row, 2);
		body = kore_pgsql_getvalue(&sql, row, 3);
		kore_log(LOG_DEBUG, "id: '%s'; title '%s'", id, title);

		// Allocate a buffer to render the markdown as HTML into.
		struct kore_buf *html_buf = kore_buf_alloc(0);
		// Render MD.
		err = render_md(body, html_buf);
		if (err == KORE_RESULT_ERROR) {
			kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
			kore_buf_free(html_buf);
			continue;
		}

		// Append rendered MD post.
		kore_buf_appendf(
			resp_buf,
			(const char *) asset_post_html,
			title,
			created_at,
			kore_buf_stringify(html_buf, NULL)
		);

		kore_buf_free(html_buf);
	}

out: ;

	// Finish building response.
	kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html);
	http_response_header(req, "content-type", "text/html; charset=utf-8");
	http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset);

	// Cleanup.
	kore_pgsql_cleanup(&sql);
	kore_buf_free(resp_buf);

	return KORE_RESULT_OK;
}

int
render_posts_json(struct http_request *req, const char *resource) {
	// Errors
	int err;

	// Buffer for response
	struct kore_buf *resp_buf = kore_buf_alloc(0);

	// SQL vars
	struct kore_pgsql sql;
	int rows;

	// JSON results form PostgreSQL.
	char *json;

	// Setup SQL
	kore_pgsql_init(&sql);

	// Initialise our kore_pgsql data structure with the database name
	// we want to connect to (note that we registered this earlier with
	// kore_pgsql_register()). We also say we will perform a synchronous
	// query (KORE_PGSQL_SYNC).
	err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
	if (err == KORE_RESULT_ERROR)
	{
		kore_buf_appendf(
			resp_buf,
			(const char *) asset_error_html,
			"There was an error processing the request."
		);
		kore_pgsql_logerror(&sql);
		goto out;
	}

	if (resource) {
		err = kore_pgsql_query_params(
			&sql,
			"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
				"SELECT id, title, body, created_at, updated_at FROM "
				"posts WHERE id = $1"
			") row;",
			0,  // return string data
			1,  // param count
			KORE_PGSQL_PARAM_TEXT(resource)
		);
	} else {
		err = kore_pgsql_query(
			&sql,
			"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
				"SELECT id, title, body, created_at, updated_at FROM "
				"posts ORDER BY updated_at DESC "
			") row;"
		);
	}
	if (err == KORE_RESULT_ERROR) {
		kore_buf_appendf(
			resp_buf,
			(const char *) asset_error_html,
			"There was an error processing the request."
		);
		kore_pgsql_logerror(&sql);
		goto out;
	}

	rows = kore_pgsql_ntuples(&sql);
	if (rows == 1) {
		json = kore_pgsql_getvalue(&sql, 0, 0);
		kore_buf_append(
			resp_buf,
			json,
			strlen(json)
		);
	} // else {

out: ;

	// Finish building response.
	http_response_header(req, "content-type", "application/json; charset=utf-8");
	http_response(req, HTTP_STATUS_OK, resp_buf->data, resp_buf->offset);

	// Cleanup.
	kore_pgsql_cleanup(&sql);
	kore_buf_free(resp_buf);

	return KORE_RESULT_OK;
}

static int
render_md(char *in, struct kore_buf *out)
{
	int err;

	static unsigned parser_flags = 0;
	static unsigned renderer_flags = MD_HTML_FLAG_DEBUG;

	err = md_html(
		in,
		(MD_SIZE) strlen(in),
		process_md_output,
		(void*) out,
		parser_flags,
		renderer_flags
	);

	if(err != 0) {
		kore_log(LOG_ERR, "Parsing Markdown failed.\n");
		return KORE_RESULT_ERROR;
	}

	return KORE_RESULT_OK;
}

static void
process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf)
{
	kore_buf_append((struct kore_buf *) buf, (const void *) html, (size_t) size);
}