feat: add route for adding pastes
This commit is contained in:
parent
a22a2e5cfa
commit
7f42d540b5
4 changed files with 51 additions and 14 deletions
54
src/main.cpp
54
src/main.cpp
|
|
@ -53,7 +53,7 @@ int main() {
|
|||
|
||||
crow::SimpleApp app;
|
||||
|
||||
app.loglevel(crow::LogLevel::Warning);
|
||||
app.loglevel(crow::LogLevel::Info);
|
||||
|
||||
CROW_ROUTE(app, "/<string>")
|
||||
.methods(crow::HTTPMethod::Get)(
|
||||
|
|
@ -71,22 +71,23 @@ int main() {
|
|||
});
|
||||
|
||||
// Add a new Redirect with a randomly generated key
|
||||
CROW_ROUTE(app, "/s/").methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request req) {
|
||||
AUTH();
|
||||
CROW_ROUTE(app, "/s/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request req) {
|
||||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
||||
char *key = ternarytrie_add_random(trie, new_entry);
|
||||
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);
|
||||
}
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
std::string res = base_url + key;
|
||||
free(key);
|
||||
|
||||
return crow::response(res);
|
||||
});
|
||||
return crow::response(res);
|
||||
});
|
||||
|
||||
// Add a new Redirect with a given path
|
||||
CROW_ROUTE(app, "/s/<string>")
|
||||
|
|
@ -105,5 +106,32 @@ int main() {
|
|||
return crow::response(base_url + key);
|
||||
});
|
||||
|
||||
// Add a new Paste with a randomly generated key
|
||||
CROW_ROUTE(app, "/p/")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, data_dir, trie](const crow::request &req) {
|
||||
AUTH();
|
||||
|
||||
Entry *new_entry = entry_new(Paste, "");
|
||||
char *key = ternarytrie_add_random(trie, new_entry);
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
// Write paste contents to file
|
||||
std::fstream file;
|
||||
file.open(data_dir + "/pastes/" + key, std::ios_base::out);
|
||||
|
||||
if (!file.is_open()) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
file << req.body;
|
||||
file.close();
|
||||
|
||||
return crow::response(base_url + key);
|
||||
});
|
||||
|
||||
app.port(18080).multithreaded().run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue