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
|
#include <kore/kore.h>
#include <kore/http.h>
#include <kore/pgsql.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"
int init(int);
int page(struct http_request *);
static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
static int render_md(char *, struct kore_buf *);
// Called when this module is loaded (see config)
int
init(int state)
{
// Register the database
int err = kore_pgsql_register(
"db",
"host=127.0.0.1 port=5432 user=postgres password=p0stgres dbname=clog sslmode=disable"
);
return err;
}
int
page(struct http_request *req)
{
struct kore_buf *resp_buf = kore_buf_alloc(4096);
kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html);
// SQL vars
struct kore_pgsql sql;
int row, rows;
// Results
char *id, *title, *created_at, *body;
/* req->status = HTTP_STATUS_INTERNAL_ERROR; */
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).
*/
int err = kore_pgsql_setup(&sql, "db", KORE_PGSQL_SYNC);
kore_log(LOG_ERR, "kore_pgsql_setup: %d", err);
if (!err)
{
kore_pgsql_logerror(&sql);
goto out;
}
/*
* Now we can fire off the query, once it returns we either have
* a result on which we can operate or an error occurred.
*/
if (!kore_pgsql_query(&sql, "SELECT id, title, created_at, body FROM posts ORDER BY id ASC;")) {
kore_pgsql_logerror(&sql);
goto out;
}
// Iterate over the result and dump it to somewhere.
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_NOTICE, "id: '%s'; title '%s'", id, title);
struct kore_buf *html_buf = kore_buf_alloc(4096);
if (!render_md(body, html_buf)) {
kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
kore_buf_free(html_buf);
continue;
}
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: ;
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 the data structures.
kore_pgsql_cleanup(&sql);
kore_buf_free(resp_buf);
return (KORE_RESULT_OK);
}
static int
render_md(char *in, struct kore_buf *out)
{
static unsigned parser_flags = 0;
static unsigned renderer_flags = MD_HTML_FLAG_DEBUG;
int ret = md_html(
in,
(MD_SIZE) strlen(in),
process_md_output,
(void*) out,
parser_flags,
renderer_flags
);
if(ret != 0) {
fprintf(stderr, "Parsing failed.\n");
return -1;
}
return 1;
}
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);
}
|