refactor: started refactoring trie api (contains segfaults)
This commit is contained in:
parent
088322c18f
commit
50ebf86589
4 changed files with 109 additions and 115 deletions
60
src/main.cpp
60
src/main.cpp
|
|
@ -7,6 +7,7 @@ extern "C" {
|
|||
#include "trie.h"
|
||||
}
|
||||
|
||||
static const std::string file_path = "lander.data";
|
||||
static const std::string index_page = R"(
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
|
@ -38,17 +39,18 @@ crow::response add_redirect(std::string base_url, Trie *trie, const char *url,
|
|||
// The key already gets copied into the trie, so this pointer is safe to use
|
||||
// ever after unlocking the trie
|
||||
trie_wlock(trie);
|
||||
char *key = trie_add_random(trie, new_entry, secure);
|
||||
char *key;
|
||||
TrieExitCode res = trie_add_random(trie, &key, new_entry, secure);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (key == NULL) {
|
||||
if (res != Ok) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
std::string res = base_url + key;
|
||||
std::string out = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
return crow::response(out);
|
||||
}
|
||||
|
||||
bool store_paste(const char *key, const char *body) {
|
||||
|
|
@ -71,10 +73,11 @@ crow::response add_paste(std::string base_url, Trie *trie, const char *body,
|
|||
Entry *new_entry = entry_new(Paste, "");
|
||||
|
||||
trie_wlock(trie);
|
||||
char *key = trie_add_random(trie, new_entry, secure);
|
||||
char *key;
|
||||
TrieExitCode res = trie_add_random(trie, &key, new_entry, secure);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (key == NULL) {
|
||||
if (res != Ok) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
|
|
@ -82,10 +85,10 @@ crow::response add_paste(std::string base_url, Trie *trie, const char *body,
|
|||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
std::string res = base_url + key;
|
||||
std::string out = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
return crow::response(out);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
@ -95,26 +98,21 @@ int main() {
|
|||
ENV(api_key, "LANDER_API_KEY");
|
||||
ENV(base_url, "LANDER_BASE_URL");
|
||||
|
||||
// Initialize trie and populate from data file
|
||||
Trie *trie = trie_init();
|
||||
|
||||
std::string file_path = "lander.data";
|
||||
|
||||
std::cout << "Populating trie from file '" << file_path << "'..."
|
||||
std::cout << "Initializing trie from file '" << file_path << "'..."
|
||||
<< std::endl;
|
||||
|
||||
// Web server hasn't started yet, so there's no point in locking the trie
|
||||
int count = trie_populate(trie, file_path.c_str());
|
||||
// Initialize trie and populate from data file
|
||||
Trie *trie;
|
||||
int res = trie_init(&trie, file_path.c_str());
|
||||
|
||||
if (count == -1) {
|
||||
std::cout << "An error occured while populating the trie." << std::endl;
|
||||
if (res != 0) {
|
||||
std::cout << "An error occured while initializing the trie." << std::endl;
|
||||
|
||||
exit(1);
|
||||
} else {
|
||||
std::cout << "Added " << count << " (" << trie_size(trie)
|
||||
<< ") entries to trie." << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Added " << trie_size(trie) << " entries to trie." << std::endl;
|
||||
|
||||
// Create pastes directory if not present
|
||||
// TODO don't just ignore errors here
|
||||
mkdir("pastes", 0700);
|
||||
|
|
@ -130,9 +128,10 @@ int main() {
|
|||
.methods(crow::HTTPMethod::Get)(
|
||||
[trie](crow::response &res, std::string key) {
|
||||
trie_rlock(trie);
|
||||
Entry *entry = trie_search(trie, key.c_str());
|
||||
Entry *entry;
|
||||
TrieExitCode status = trie_search(trie, &entry, key.c_str());
|
||||
|
||||
if (entry != NULL) {
|
||||
if (status == Ok) {
|
||||
if (entry->type == Redirect) {
|
||||
res.redirect(entry->string);
|
||||
} else if (entry->type == Paste) {
|
||||
|
|
@ -173,14 +172,17 @@ int main() {
|
|||
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
||||
|
||||
trie_wlock(trie);
|
||||
bool added = trie_add(trie, key.c_str(), new_entry);
|
||||
TrieExitCode status = trie_add(trie, key.c_str(), new_entry);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (!added) {
|
||||
switch (status) {
|
||||
case Ok:
|
||||
return crow::response(base_url + key);
|
||||
case AlreadyPresent:
|
||||
return crow::response(crow::status::CONFLICT);
|
||||
default:
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
return crow::response(base_url + key);
|
||||
});
|
||||
|
||||
// Add a new Paste with a short randomly generated key
|
||||
|
|
@ -209,10 +211,10 @@ int main() {
|
|||
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
trie_wlock(trie);
|
||||
bool added = trie_add(trie, key.c_str(), new_entry);
|
||||
TrieExitCode status = trie_add(trie, key.c_str(), new_entry);
|
||||
trie_unlock(trie);
|
||||
|
||||
if (!added) {
|
||||
if (status != Ok) {
|
||||
return crow::response(crow::status::CONFLICT);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue