feat: return full URL of added entry
ci/woodpecker/push/woodpecker Pipeline was successful Details

trie-skips
Jef Roosens 2022-11-21 12:21:01 +01:00
parent 51fc2867a8
commit 26318de22e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 22 additions and 6 deletions

View File

@ -18,9 +18,14 @@ extern "C" {
} }
int main() { int main() {
// Initialize random seed for generating URLs
srand(time(NULL));
ENV(api_key, "LANDER_API_KEY"); ENV(api_key, "LANDER_API_KEY");
ENV(base_url, "LANDER_BASE_URL"); ENV(base_url, "LANDER_BASE_URL");
const size_t base_url_len = strlen(base_url);
TernaryTrie *trie = ternarytrie_init(); TernaryTrie *trie = ternarytrie_init();
std::string file_path = "lander.data"; std::string file_path = "lander.data";
@ -44,7 +49,7 @@ int main() {
res.end(); res.end();
}); });
CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)( 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(); AUTH();
char *key = ternarytrie_add_random(trie, req.body.c_str()); 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(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, "/<string>") CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Post)( .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(); 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::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(); app.port(18080).multithreaded().run();