feat: add route for adding pastes
parent
a22a2e5cfa
commit
7f42d540b5
|
@ -1,3 +1,4 @@
|
||||||
build/
|
build/
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
lander.data
|
lander.data
|
||||||
|
data/
|
||||||
|
|
|
@ -12,4 +12,7 @@ if [ "$1" = add ]; then
|
||||||
|
|
||||||
elif [ "$1" = get ]; then
|
elif [ "$1" = get ]; then
|
||||||
curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p'
|
curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p'
|
||||||
|
|
||||||
|
elif [ "$1" = paste ]; then
|
||||||
|
curl -v --data-binary @"$2" -XPOST -H "X-Api-Key: $API_KEY" "$URL/p/"
|
||||||
fi
|
fi
|
||||||
|
|
32
src/main.cpp
32
src/main.cpp
|
@ -53,7 +53,7 @@ int main() {
|
||||||
|
|
||||||
crow::SimpleApp app;
|
crow::SimpleApp app;
|
||||||
|
|
||||||
app.loglevel(crow::LogLevel::Warning);
|
app.loglevel(crow::LogLevel::Info);
|
||||||
|
|
||||||
CROW_ROUTE(app, "/<string>")
|
CROW_ROUTE(app, "/<string>")
|
||||||
.methods(crow::HTTPMethod::Get)(
|
.methods(crow::HTTPMethod::Get)(
|
||||||
|
@ -71,7 +71,8 @@ int main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add a new Redirect with a randomly generated key
|
// Add a new Redirect with a randomly generated key
|
||||||
CROW_ROUTE(app, "/s/").methods(crow::HTTPMethod::Post)(
|
CROW_ROUTE(app, "/s/")
|
||||||
|
.methods(crow::HTTPMethod::Post)(
|
||||||
[api_key, base_url, trie](const crow::request req) {
|
[api_key, base_url, trie](const crow::request req) {
|
||||||
AUTH();
|
AUTH();
|
||||||
|
|
||||||
|
@ -105,5 +106,32 @@ int main() {
|
||||||
return crow::response(base_url + key);
|
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();
|
app.port(18080).multithreaded().run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,12 @@ char entry_type_to_char(EntryType et) {
|
||||||
Entry *entry_new(EntryType type, const char *string) {
|
Entry *entry_new(EntryType type, const char *string) {
|
||||||
Entry *entry = malloc(sizeof(Entry));
|
Entry *entry = malloc(sizeof(Entry));
|
||||||
entry->type = type;
|
entry->type = type;
|
||||||
|
|
||||||
|
if (string != NULL) {
|
||||||
entry->string = my_strdup(string);
|
entry->string = my_strdup(string);
|
||||||
|
} else {
|
||||||
|
entry->string = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue