diff options
| author | Michael McVady <femtonaut@gmail.com> | 2023-07-07 00:15:09 -0500 | 
|---|---|---|
| committer | Michael McVady <femtonaut@gmail.com> | 2023-07-07 00:15:09 -0500 | 
| commit | b5c64ec289fe9e07603b3da5f3d246bf88c8a124 (patch) | |
| tree | c1d5bbd690beb4e3de019c05bbea54dd565853c2 | |
| parent | 01da70c10dd338f1dc6aa319051ce281c73b1528 (diff) | |
Hack in markdown form
| -rw-r--r-- | assets/post_edit.html | 11 | ||||
| -rw-r--r-- | conf/clog.conf | 18 | ||||
| -rw-r--r-- | src/clog.c | 79 | 
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  	} @@ -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; | 
