From 3123f09ce08d3824671ca218bef66c28748bda07 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Mon, 7 Mar 2022 22:02:53 -0500 Subject: Disable TLS & added posts resource --- src/clog.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index d3fa324..7bf6012 100644 --- a/src/clog.c +++ b/src/clog.c @@ -12,7 +12,9 @@ #include "../lib/md4c/src/md4c-html.c" int init(int); -int page(struct http_request *); +int post(struct http_request *); +int posts(struct http_request *); +int render_posts(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 *); @@ -27,16 +29,27 @@ init(int state) database, "host=127.0.0.1 port=5432 user=postgres password=p0stgres dbname=clog sslmode=disable" ); - return err; } int -page(struct http_request *req) +post(struct http_request *req) { + const char *resource; + resource = req->path + strlen("/posts/"); + kore_log(LOG_DEBUG, "Resource /posts/%s", resource); + return render_posts(req, resource); +} - // req->status = HTTP_STATUS_INTERNAL_ERROR; +int +posts(struct http_request *req) +{ + return render_posts(req, NULL); +} +int +render_posts(struct http_request *req, const char *resource) +{ // Buffer for response struct kore_buf *resp_buf = kore_buf_alloc(0); @@ -61,15 +74,29 @@ page(struct http_request *req) goto out; } - // Start writing page. + // Start writing HTML. 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 updated_at DESC;" - ); + if (resource) { + // Query post + err = kore_pgsql_query_params( + &sql, + "SELECT id, title, created_at::DATE, body FROM posts " + "WHERE id = $1 " + "ORDER BY updated_at DESC;", + 0, + 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); goto out; @@ -138,7 +165,7 @@ render_md(char *in, struct kore_buf *out) ); if(ret != 0) { - fprintf(stderr, "Parsing failed.\n"); + kore_log(LOG_ERR, "Parsing Markdown failed.\n"); return -1; } -- cgit v1.2.3