refactor: start using void* with trie
This commit is contained in:
parent
01eb5ece55
commit
c99bc83015
8 changed files with 83 additions and 57 deletions
45
src/main.cpp
45
src/main.cpp
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue