diff options
author | Michael McVady <femtonaut@gmail.com> | 2023-01-05 23:20:45 -0600 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2023-01-05 23:20:45 -0600 |
commit | d88f5cced222ac76511702a80e26dc7de8af93f3 (patch) | |
tree | 3f76754b5f8bc981166487838bd57cedbc76d427 | |
parent | 17e9336e577d1453ac468956e04048c721306826 (diff) |
toy handles GET and POST requests
-rw-r--r-- | conf/clog.conf | 8 | ||||
-rw-r--r-- | src/clog.c | 66 |
2 files changed, 50 insertions, 24 deletions
diff --git a/conf/clog.conf b/conf/clog.conf index a2cf8dd..0cf3d26 100644 --- a/conf/clog.conf +++ b/conf/clog.conf @@ -28,14 +28,18 @@ domain * { route /posts { handler posts + # TODO Handle HTTP POST request. + methods get post } route ^/posts/[a-z0-9\-]+$ { handler post + methods get } - route /toy { - handler toy + route /toys { + handler toys + methods get post } route ^/.*$ { @@ -56,7 +56,9 @@ int posts(struct http_request *req); int render_posts(struct http_request *req, const char *resource); int render_posts_query(struct post_request *post_req); -int toy(struct http_request *req); +int toys(struct http_request *req); +int get_toys(struct http_request *req); +int post_toys(struct http_request *req); static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); static int render_md(const char *, struct kore_buf *); @@ -315,27 +317,43 @@ out: ; } int -toy(struct http_request *req) { +toys(struct http_request *req) { + if (req->method == HTTP_METHOD_GET) + get_toys(req); + else if (req->method == HTTP_METHOD_POST) + post_toys(req); + + return KORE_RESULT_OK; +} + +int +get_toys(struct http_request *req) { + + http_response(req, HTTP_STATUS_OK, "OK", strlen("OK")); + + return KORE_RESULT_OK; +} + +int +post_toys(struct http_request *req) { int err = 0; int status = HTTP_STATUS_OK; struct kore_json_item *item = NULL; + const char *id = NULL; + const char *title = NULL; + const char *body = NULL; + struct kore_json json; struct kore_pgsql sql; - const char *json_str = "{\"id\": \"00000000-0000-0000-0000-000000000000\", " - "\"title\": \"title\", \"body\": \"body\"}"; + // const char *json_str = "{\"id\": \"00000000-0000-0000-0000-000000000000\", " + // "\"title\": \"title\", \"body\": \"body\"}"; - // const char *q = "INSERT INTO posts (id, title, body) " - // "VALUES ('00000000-0000-0000-0000-000000000000', 'title', 'body');"; - // 'af5bbaec-8d2b-11ed-9bf3-db5637a87ac5' - - const char *q = "INSERT INTO posts (id, title, body) " - "VALUES ($1, $2, $3);"; - - kore_json_init(&json, json_str, strlen(json_str)); + // kore_json_init(&json, json_str, strlen(json_str)); + kore_json_init(&json, req->http_body->data, req->http_body->length); kore_pgsql_init(&sql); @@ -347,7 +365,8 @@ toy(struct http_request *req) { item = kore_json_find_string(json.root, "id"); if (item != NULL) { - kore_log(LOG_INFO, "id = '%s'\n", item->data.string); + id = item->data.string; + kore_log(LOG_INFO, "id = '%s'\n", id); } else { status = HTTP_STATUS_INTERNAL_ERROR; kore_log(LOG_ERR, "error parsing id: %s\n", kore_json_strerror()); @@ -355,16 +374,17 @@ toy(struct http_request *req) { } // Check for valid resource ID/UUID - err = validate_uuid(item->data.string); + err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { status = HTTP_STATUS_NOT_FOUND; - kore_log(LOG_ERR, "Invalid post UUID %s.", item->data.string); + kore_log(LOG_ERR, "Invalid post UUID %s.", id); goto out; } item = kore_json_find_string(json.root, "title"); if (item != NULL) { - kore_log(LOG_INFO, "title = '%s'\n", item->data.string); + title = item->data.string; + kore_log(LOG_INFO, "title = '%s'\n", title); } else { status = HTTP_STATUS_INTERNAL_ERROR; kore_log(LOG_ERR, "error parsing title: %s\n", kore_json_strerror()); @@ -373,7 +393,8 @@ toy(struct http_request *req) { item = kore_json_find_string(json.root, "body"); if (item != NULL) { - kore_log(LOG_INFO, "body = '%s'\n", item->data.string); + body = item->data.string; + kore_log(LOG_INFO, "body = '%s'\n", body); } else { status = HTTP_STATUS_INTERNAL_ERROR; kore_log(LOG_ERR, "error parsing body: %s\n", kore_json_strerror()); @@ -391,12 +412,13 @@ toy(struct http_request *req) { // err = kore_pgsql_query( err = kore_pgsql_query_params( &sql, - q, + "INSERT INTO posts (id, title, body) " + "VALUES ($1, $2, $3);", 0, 3, - KORE_PGSQL_PARAM_TEXT("00000000-0000-0000-0000-000000000000"), - KORE_PGSQL_PARAM_TEXT("title"), - KORE_PGSQL_PARAM_TEXT("body") + KORE_PGSQL_PARAM_TEXT(id), + KORE_PGSQL_PARAM_TEXT(title), + KORE_PGSQL_PARAM_TEXT(body) ); if (err == KORE_RESULT_ERROR) { @@ -407,8 +429,8 @@ toy(struct http_request *req) { out: ; - kore_pgsql_cleanup(&sql); kore_json_cleanup(&json); + kore_pgsql_cleanup(&sql); http_response(req, status, NULL, 0); |