diff options
author | Michael McVady <femtonaut@gmail.com> | 2022-03-07 22:02:53 -0500 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2022-03-07 22:02:53 -0500 |
commit | 3123f09ce08d3824671ca218bef66c28748bda07 (patch) | |
tree | b30ef7531250813f5e411bac7e3b344b6791780f /src | |
parent | 3fcd6b69c8e9d0acd5b679fd609db82da20ffb3e (diff) |
Disable TLS & added posts resource
Diffstat (limited to 'src')
-rw-r--r-- | src/clog.c | 49 |
1 files changed, 38 insertions, 11 deletions
@@ -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; } |