aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c49
1 files changed, 38 insertions, 11 deletions
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;
}