aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clog.c80
-rw-r--r--src/queries.h5
2 files changed, 75 insertions, 10 deletions
diff --git a/src/clog.c b/src/clog.c
index 80906d3..4eebe6f 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -23,8 +23,6 @@ KORE_SECCOMP_FILTER("clog",
KORE_SYSCALL_ALLOW(uname)
)
-enum content_type { CONTENT_JSON, CONTENT_X_WWW_FORM_URLENCODED };
-
enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND };
struct entry_query {
@@ -77,10 +75,12 @@ int sql_select_entries(struct entry_query *eq);
int sql_update_entry(const char *id, const char *title, const char *body);
int sql_delete_entry(const char *id);
int sql_insert_entry(const char *id, const char *title, const char *body);
+
int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq);
+int sql_render_index(struct kore_pgsql *sql, struct entry_query *eq);
-static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
-static int render_md(const char *, struct kore_buf *);
+static void process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf);
+static int render_md(const char *in, struct kore_buf *out);
void entry_query_init(struct entry_query *eq, const char *id) {
if (id != NULL)
@@ -242,7 +242,7 @@ int post_entry_form(struct http_request *req) {
http_response_header(req, "content-type", "text/html; charset=utf-8");
- kore_buf_append(resp_buf, asset_form_begin_html, asset_len_form_begin_html);
+ kore_buf_append(resp_buf, asset_index_begin_html, asset_len_index_begin_html);
// Append rendered MD entry.
size_t len = 0;
@@ -254,7 +254,7 @@ int post_entry_form(struct http_request *req) {
len
);
- kore_buf_append(resp_buf, asset_form_end_html, asset_len_form_end_html);
+ kore_buf_append(resp_buf, asset_index_end_html, asset_len_index_end_html);
http_response(
req,
@@ -482,10 +482,10 @@ int sql_select_entries(struct entry_query *eq) {
KORE_PGSQL_PARAM_TEXT(eq->id)
);
} else {
- // Query all entries.
+ // Query for index.
err = kore_pgsql_query(
&sql,
- q_select_entries
+ q_select_index
);
}
@@ -501,7 +501,11 @@ int sql_select_entries(struct entry_query *eq) {
goto out;
}
- (void) sql_render_entry(&sql, eq);
+ if (eq->id) {
+ (void) sql_render_entry(&sql, eq);
+ } else {
+ (void) sql_render_index(&sql, eq);
+ }
out: ;
@@ -578,14 +582,70 @@ int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq) {
kore_buf_appendf(
eq->result,
(const char *) asset_entry_html,
+ kore_buf_stringify(html_buf, NULL)
+ );
+
+ }
+
+ kore_buf_free(html_buf);
+
+ return KORE_RESULT_OK;
+}
+
+
+int sql_render_index(struct kore_pgsql *sql, struct entry_query *eq) {
+
+ int row = 0, rows = 0;
+
+ struct kore_buf *html_buf = NULL;
+
+ const char *id = NULL;
+ const char *title = NULL;
+ const char *created_at = NULL;
+ const char *updated_at = NULL;
+
+ // Allocate a buffer to render the markdown as HTML into.
+ html_buf = kore_buf_alloc(0);
+
+ // Write table header.
+ kore_buf_append(
+ eq->result,
+ (const char *) "<h1>clog.bunkergate.org</h1>\r\n<table><tr><th>Title</th><th>Created</th><th>Updated</th></tr>\r\n",
+ 94
+ );
+
+ // Iterate over entries and render them.
+ rows = kore_pgsql_ntuples(sql);
+ for (row = 0; row < rows; row++) {
+ // Reset buffer.
+ kore_buf_cleanup(html_buf);
+
+ // Fetch data.
+ id = kore_pgsql_getvalue(sql, row, 0);
+ title = kore_pgsql_getvalue(sql, row, 1);
+ created_at = kore_pgsql_getvalue(sql, row, 2);
+ updated_at = kore_pgsql_getvalue(sql, row, 3);
+ kore_log(LOG_DEBUG, "id: '%s'; title '%s'", id, title);
+
+ // Append rendered MD entry.
+ kore_buf_appendf(
+ eq->result,
+ (const char *) "<tr><td><a href=\"/entries/%s\">%s</a></td><td>%s</td><td>%s</td></tr>\r\n",
id,
title,
created_at,
- kore_buf_stringify(html_buf, NULL)
+ updated_at
);
}
+ // Write table footer.
+ kore_buf_append(
+ eq->result,
+ (const char *) "</table>\r\n",
+ 10
+ );
+
kore_buf_free(html_buf);
return KORE_RESULT_OK;
diff --git a/src/queries.h b/src/queries.h
index 986f180..c0bd1dc 100644
--- a/src/queries.h
+++ b/src/queries.h
@@ -8,6 +8,11 @@ const char *q_select_entries =
"FROM entries "
"ORDER BY updated_at DESC;";
+const char *q_select_index =
+"SELECT id, title, created_at::DATE, updated_at::DATE "
+"FROM entries "
+"ORDER BY updated_at DESC;";
+
const char *q_insert_entry =
"INSERT INTO entries "
"(title, body) "