diff options
author | Michael McVady <femtonaut@gmail.com> | 2021-11-18 18:15:07 -0500 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2021-11-18 18:15:07 -0500 |
commit | 26290a5fb0374b0dbccf775265d25988aefdce1e (patch) | |
tree | a15d665514db6c4f696346a6ae5a3eab87b4ce5e | |
parent | 9bd6fe0e5f690970c49ee993e5012bef5868d9ab (diff) |
Cleanup/comments
-rw-r--r-- | src/clog.c | 49 |
1 files changed, 28 insertions, 21 deletions
@@ -16,13 +16,15 @@ 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 *); +static const char *database = "db"; + // Called when this module is loaded (see config) int init(int state) { // Register the database int err = kore_pgsql_register( - "db", + database, "host=127.0.0.1 port=5432 user=postgres password=p0stgres dbname=clog sslmode=disable" ); @@ -33,28 +35,25 @@ int page(struct http_request *req) { - struct kore_buf *resp_buf = kore_buf_alloc(4096); + // req->status = HTTP_STATUS_INTERNAL_ERROR; - kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html); + // Buffer for response + struct kore_buf *resp_buf = kore_buf_alloc(0); // SQL vars struct kore_pgsql sql; int row, rows; - // Results + // Post attributes/query 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); + // 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); kore_log(LOG_ERR, "kore_pgsql_setup: %d", err); if (!err) { @@ -62,16 +61,21 @@ page(struct http_request *req) 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;")) { + // Start writing page. + kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html); + + // Query for posts, check for error. + err = kore_pgsql_query( + &sql, + "SELECT id, title, created_at::DATE, body FROM posts " + "ORDER BY id ASC;" + ); + if (!err) { kore_pgsql_logerror(&sql); goto out; } - // Iterate over the result and dump it to somewhere. + // Iterate over posts and render them. rows = kore_pgsql_ntuples(&sql); for (row = 0; row < rows; row++) { @@ -81,14 +85,17 @@ page(struct http_request *req) 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); + // 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); kore_buf_free(html_buf); continue; } + // Append rendered MD post. kore_buf_appendf( resp_buf, (const char *) asset_post_html, @@ -108,7 +115,7 @@ out: ; 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. + // Cleanup. kore_pgsql_cleanup(&sql); kore_buf_free(resp_buf); |