feat: pave way for pastebin integration

This commit is contained in:
Jef Roosens 2022-11-21 15:28:02 +01:00
parent 689a878978
commit f1ec643f80
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 112 additions and 61 deletions

View file

@ -49,10 +49,11 @@ int main() {
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Get)(
[trie](crow::response &res, std::string s) {
char *payload = ternarytrie_search(trie, s.c_str());
Entry *entry = ternarytrie_search(trie, s.c_str());
if (payload != NULL) {
res.redirect(payload);
// TODO check entry type
if (entry != NULL) {
res.redirect(entry->string);
} else {
res.code = 404;
}
@ -63,7 +64,8 @@ int main() {
[api_key, base_url, trie](const crow::request req) {
AUTH();
char *key = ternarytrie_add_random(trie, req.body.c_str());
Entry *new_entry = entry_new(Redirect, req.body.c_str());
char *key = ternarytrie_add_random(trie, new_entry);
if (key == NULL) {
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
@ -76,11 +78,12 @@ int main() {
});
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, base_url, trie](const crow::request &req, std::string s) {
[api_key, base_url, trie](const crow::request &req, std::string key) {
AUTH();
std::string key = req.body;
bool added = ternarytrie_add(trie, s.c_str(), req.body.c_str());
Entry *new_entry = entry_new(Redirect, req.body.c_str());
bool added = ternarytrie_add(trie, key.c_str(), new_entry);
if (!added) {
return crow::response(crow::status::CONFLICT);