From 26318de22ee92d8d8d193d8a5709293bf39248e2 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 21 Nov 2022 12:21:01 +0100 Subject: [PATCH] feat: return full URL of added entry --- src/main.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2123aa7..7e0e6b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,9 +18,14 @@ extern "C" { } int main() { + // Initialize random seed for generating URLs + srand(time(NULL)); + ENV(api_key, "LANDER_API_KEY"); ENV(base_url, "LANDER_BASE_URL"); + const size_t base_url_len = strlen(base_url); + TernaryTrie *trie = ternarytrie_init(); std::string file_path = "lander.data"; @@ -44,7 +49,7 @@ int main() { res.end(); }); CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)( - [api_key, base_url, trie](const crow::request req) { + [api_key, base_url, base_url_len, trie](const crow::request req) { AUTH(); char *key = ternarytrie_add_random(trie, req.body.c_str()); @@ -53,20 +58,31 @@ int main() { return crow::response(crow::status::INTERNAL_SERVER_ERROR); } - return crow::response(key); + // Concatenate base URL & key + char *res = (char *)malloc(base_url_len + RANDOM_KEY_LENGTH + 1); + memcpy(res, base_url, base_url_len); + memcpy(res + base_url_len, key, RANDOM_KEY_LENGTH + 1); + + return crow::response(res); }); CROW_ROUTE(app, "/") .methods(crow::HTTPMethod::Post)( - [api_key, trie](const crow::request &req, std::string s) { + [api_key, base_url, base_url_len, trie](const crow::request &req, + std::string s) { AUTH(); - bool res = ternarytrie_add(trie, s.c_str(), req.body.c_str()); + bool added = ternarytrie_add(trie, s.c_str(), req.body.c_str()); - if (!res) { + if (!added) { return crow::response(crow::status::CONFLICT); } - return crow::response(crow::status::NO_CONTENT); + // Concatenate base URL & key + char *res = (char *)malloc(base_url_len + RANDOM_KEY_LENGTH + 1); + memcpy(res, base_url, base_url_len); + memcpy(res + base_url_len, s.c_str(), RANDOM_KEY_LENGTH + 1); + + return crow::response(res); }); app.port(18080).multithreaded().run();