refactor: start using void* with trie

This commit is contained in:
Jef Roosens 2022-12-07 23:20:39 +01:00
parent 01eb5ece55
commit c99bc83015
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
8 changed files with 83 additions and 57 deletions

View file

@ -125,25 +125,25 @@ int main() {
// Serve an entry
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Get)(
[trie](crow::response &res, std::string key) {
trie_rlock(trie);
Entry *entry;
TrieExitCode status = trie_search(trie, &entry, key.c_str());
.methods(
crow::HTTPMethod::Get)([trie](crow::response &res, std::string key) {
trie_rlock(trie);
Entry *entry;
TrieExitCode status = trie_search(trie, (void **)&entry, key.c_str());
if (status == Ok) {
if (entry->type == Redirect) {
res.redirect(entry->string);
} else if (entry->type == Paste) {
res.set_static_file_info("pastes/" + key);
}
} else {
res.code = 404;
}
if (status == Ok) {
if (entry->type == Redirect) {
res.redirect(entry->string);
} else if (entry->type == Paste) {
res.set_static_file_info("pastes/" + key);
}
} else {
res.code = 404;
}
res.end();
trie_unlock(trie);
});
res.end();
trie_unlock(trie);
});
// Add a new Redirect with a short randomly generated key
CROW_ROUTE(app, "/s/")
@ -214,15 +214,14 @@ int main() {
TrieExitCode status = trie_add(trie, key.c_str(), new_entry);
trie_unlock(trie);
if (status != Ok) {
switch (status) {
case Ok:
return crow::response(base_url + key);
case AlreadyPresent:
return crow::response(crow::status::CONFLICT);
}
if (!store_paste(key.c_str(), req.body.c_str())) {
default:
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
}
return crow::response(base_url + key);
});
app.port(18080).multithreaded().run();
}