aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--assets/entry.html13
-rw-r--r--assets/form_begin.html11
-rw-r--r--assets/form_end.html9
-rw-r--r--assets/index_begin.html14
-rw-r--r--assets/index_end.html10
-rw-r--r--src/clog.c80
-rw-r--r--src/queries.h5
8 files changed, 80 insertions, 64 deletions
diff --git a/README.md b/README.md
index d1a07a6..1471b5b 100644
--- a/README.md
+++ b/README.md
@@ -50,7 +50,7 @@ Use `psql` and load `db/db.sql` for models,
10. Get rid of `requests` dependency in `tests.py`
11. Fix `PATCH` redirects?
-## But using C in 2023 is dangerous?
+## But using C in 2024 is dangerous?
* https://chromium.googlesource.com/chromium/src/+/master/docs/security/rule-of-2.md
* https://verdagon.dev/blog/when-to-use-memory-safe-part-1
diff --git a/assets/entry.html b/assets/entry.html
index 445d2ce..d25750d 100644
--- a/assets/entry.html
+++ b/assets/entry.html
@@ -1,12 +1 @@
-
-<div class="entry">
-<hr class="hdiv">
-<div class="head">
-<h2><span class="title"><a href="/entries/%s">%s</a></span></h2>
-<span class="date">%s</span>
-</div>
-<div class="body">
-%s
-</div>
-<hr class="hdiv">
-</div>
+%s \ No newline at end of file
diff --git a/assets/form_begin.html b/assets/form_begin.html
deleted file mode 100644
index 4da1ce9..0000000
--- a/assets/form_begin.html
+++ /dev/null
@@ -1,11 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
-<!--meta name="theme-color" content="#E6E6FF"-->
-<title>bunkergate.org</title>
-<link rel="icon" type="image/vnd.microsoft.icon" href="/static/favicon.ico">
-<link rel="stylesheet" type="text/css" href="/static/css/retro.css">
-</head>
-<body>
diff --git a/assets/form_end.html b/assets/form_end.html
deleted file mode 100644
index 6131a40..0000000
--- a/assets/form_end.html
+++ /dev/null
@@ -1,9 +0,0 @@
-
-<script src="https://cdn.jsdelivr.net/npm/mermaid@9.4.3/dist/mermaid.min.js"></script>
-<script>
-mermaid.initialize({theme: "dark"});
-mermaid.init(undefined, ".language-mermaid");
-</script>
-
-</body>
-</html> \ No newline at end of file
diff --git a/assets/index_begin.html b/assets/index_begin.html
index bf13b45..0ff3660 100644
--- a/assets/index_begin.html
+++ b/assets/index_begin.html
@@ -3,20 +3,10 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
-<meta name="theme-color" content="#E6E6FF">
+<!--meta name="theme-color" content="#E6E6FF"-->
<title>bunkergate.org</title>
<link rel="icon" type="image/vnd.microsoft.icon" href="/static/favicon.ico">
-<link rel="stylesheet" type="text/css" href="/static/css/default.css">
+<link rel="stylesheet" type="text/css" href="/static/css/retro.css">
</head>
<body>
-<div id="head"><h1>bunkergate.org</h1></div>
-
-<div id="menu">
-<a href="/">www</a> |
-<a href="http://cgit.bunkergate.org">cgit</a>
-<span class="email-pipe">|</span>
-<a class="email" href="mailto:femtonaut@gmail.com">femtonaut@gmail.com</a>
-</div>
-
-<hr class="hdiv">
diff --git a/assets/index_end.html b/assets/index_end.html
index ba83da5..6131a40 100644
--- a/assets/index_end.html
+++ b/assets/index_end.html
@@ -1,11 +1,3 @@
-<hr class="hdiv">
-
-<span id="footmenu">
-<a href="https://validator.w3.org/check/referer">HTML5</a> |
-<a href="https://jigsaw.w3.org/css-validator/check/referer">CSS3</a>
-</span>
-
-<hr class="hdiv">
<script src="https://cdn.jsdelivr.net/npm/mermaid@9.4.3/dist/mermaid.min.js"></script>
<script>
@@ -14,4 +6,4 @@ mermaid.init(undefined, ".language-mermaid");
</script>
</body>
-</html>
+</html> \ No newline at end of file
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) "