aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clog.c35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/clog.c b/src/clog.c
index 365787c..b6c3cb1 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -55,6 +55,9 @@ posts(struct http_request *req)
int
render_posts(struct http_request *req, const char *resource)
{
+ // Errors
+ int err;
+
// Buffer for response
struct kore_buf *resp_buf = kore_buf_alloc(0);
@@ -65,13 +68,14 @@ render_posts(struct http_request *req, const char *resource)
// Post attributes/query results.
char *id, *title, *created_at, *body;
+ // 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).
- int err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
+ err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
kore_log(LOG_ERR, "kore_pgsql_setup: %d", err);
if (!err)
{
@@ -83,8 +87,15 @@ render_posts(struct http_request *req, const char *resource)
kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html);
// Query for posts, check for error.
- if (resource) {
- // Query post
+ if (!resource) {
+ // Query all posts.
+ err = kore_pgsql_query(
+ &sql,
+ "SELECT id, title, created_at::DATE, body FROM posts "
+ "ORDER BY updated_at DESC;"
+ );
+ } else {
+ // Query a post.
err = kore_pgsql_query_params(
&sql,
"SELECT id, title, created_at::DATE, body FROM posts "
@@ -94,13 +105,6 @@ render_posts(struct http_request *req, const char *resource)
1,
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_pgsql_logerror(&sql);
@@ -110,7 +114,6 @@ render_posts(struct http_request *req, const char *resource)
// 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);
@@ -119,7 +122,6 @@ render_posts(struct http_request *req, const char *resource)
// Allocate a buffer to render the markdown as HTML into.
struct kore_buf *html_buf = kore_buf_alloc(0);
-
// Render MD.
if (!render_md(body, html_buf)) {
kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
@@ -137,13 +139,12 @@ render_posts(struct http_request *req, const char *resource)
);
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);
@@ -157,10 +158,12 @@ out: ;
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;
- int ret = md_html(
+ err = md_html(
in,
(MD_SIZE) strlen(in),
process_md_output,
@@ -169,7 +172,7 @@ render_md(char *in, struct kore_buf *out)
renderer_flags
);
- if(ret != 0) {
+ if(err != 0) {
kore_log(LOG_ERR, "Parsing Markdown failed.\n");
return -1;
}