aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/post_edit.html11
-rw-r--r--conf/clog.conf18
-rw-r--r--src/clog.c79
3 files changed, 107 insertions, 1 deletions
diff --git a/assets/post_edit.html b/assets/post_edit.html
new file mode 100644
index 0000000..6635b87
--- /dev/null
+++ b/assets/post_edit.html
@@ -0,0 +1,11 @@
+<form action="/form" method="post" enctype="multipart/form-data">
+<dl>
+<dt><h3>id</h3></dt>
+<dd><input type="text" id="id" name="id"></input></dd>
+<dt><h3>title</h3></dt>
+<dd><input type="text" id="title" name="title"></input></dd>
+<dt><h3>body</h3></dt>
+<dd><textarea id="body" name="body"></textarea></dd>
+<dd><input type="submit" value="Edit"></dd>
+</dl>
+</form> \ No newline at end of file
diff --git a/conf/clog.conf b/conf/clog.conf
index 8006503..01e1715 100644
--- a/conf/clog.conf
+++ b/conf/clog.conf
@@ -17,6 +17,9 @@ privsep worker {
seccomp_tracing no
+validator v_number regex ^[0-9]*$
+validator v_text function v_example_func
+
domain * {
attach notls
@@ -52,6 +55,21 @@ domain * {
methods delete
}
+ # toy endpoint
+ route ^/form {
+ handler get_form
+ methods get
+ }
+
+ route ^/form {
+ handler post_form
+ methods post
+
+ validate post id v_number
+ validate post title v_number
+ validate post body v_text
+ }
+
route ^/.*$ {
handler redirect
}
diff --git a/src/clog.c b/src/clog.c
index e3a96b0..fd784d2 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -74,6 +74,11 @@ int post_posts(struct http_request *req);
int put_posts(struct http_request *req);
int delete_posts(struct http_request *req);
+int v_example_func(struct http_request *req, char *data);
+
+int get_form(struct http_request *req);
+int post_form(struct http_request *req);
+
int sql_select_posts(struct post_query *pq);
int sql_update_posts(const char *id, const char *title, const char *body);
int sql_delete_posts(const char *id);
@@ -225,6 +230,78 @@ int redirect(struct http_request *req) {
return KORE_RESULT_OK;
}
+int v_example_func(struct http_request *req, char *data) {
+ kore_log(LOG_NOTICE, "v_example_func called");
+ return KORE_RESULT_OK;
+}
+
+int get_form(struct http_request *req) {
+ struct kore_buf *resp_buf = kore_buf_alloc(0);
+
+ kore_buf_append(
+ resp_buf,
+ (const char *) asset_post_edit_html,
+ asset_len_post_edit_html
+ );
+
+ http_ok_resp(req, HTML, HTTP_STATUS_OK, resp_buf);
+
+ kore_buf_free(resp_buf);
+
+ return KORE_RESULT_OK;
+}
+
+
+int post_form(struct http_request *req) {
+
+ struct kore_buf *html_buf = kore_buf_alloc(0);
+ struct kore_buf *resp_buf = kore_buf_alloc(0);
+
+ char *id = NULL;
+ char *title = NULL;
+ char *body = NULL;
+
+ http_populate_multipart_form(req);
+ // http_populate_post(req);
+
+ if (http_argument_get_string(req, "id", &id)) {
+ kore_log(LOG_INFO, "form id %s.", id);
+ }
+
+ if (http_argument_get_string(req, "title", &title)) {
+ kore_log(LOG_INFO, "form title %s.", title);
+ }
+
+ if (http_argument_get_string(req, "body", &body)) {
+ kore_log(LOG_INFO, "form body %s.", body);
+ }
+
+ // Render MD.
+ (void) render_md(body, html_buf);
+ // if (err == KORE_RESULT_ERROR) {
+ // kore_log(LOG_ERR, "Error rendering markdown for post %s.", id);
+ // continue;
+ // }
+
+ // Append rendered MD post.
+ kore_buf_appendf(
+ resp_buf,
+ (const char *) asset_post_html,
+ id,
+ title,
+ "1981-12-23 06:02:00",
+ kore_buf_stringify(html_buf, NULL)
+ );
+
+ http_ok_resp(req, HTML, HTTP_STATUS_OK, resp_buf);
+
+ kore_buf_free(html_buf);
+ kore_buf_free(resp_buf);
+
+ return KORE_RESULT_OK;
+}
+
+
int get_posts(struct http_request *req) {
struct post_query pq;
@@ -380,7 +457,7 @@ int put_posts(struct http_request *req) {
if (item != NULL) {
title = item->data.string;
kore_log(LOG_INFO, "title = '%s'\n", title);
- } else {
+ } else {
status = HTTP_STATUS_BAD_REQUEST;
kore_log(LOG_ERR, "Error parsing title: %s\n", kore_json_strerror());
goto out;