From a22a2e5cfa7fe111e09825052120300f1c41b756 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 21 Nov 2022 15:45:31 +0100 Subject: [PATCH] feat: create pastes directory; separate routes --- Makefile | 2 +- landerctl | 2 +- src/main.cpp | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 0b7201e..bd518ef 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ prod: cmake-release .PHONY: run run: build - @ LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test ./build/Debug/lander + @ LANDER_DATA_DIR=data LANDER_BASE_URL=http://localhost:18080/ LANDER_API_KEY=test ./build/Debug/lander .PHONY: clean clean: diff --git a/landerctl b/landerctl index acd9e48..e1f44de 100755 --- a/landerctl +++ b/landerctl @@ -8,7 +8,7 @@ if [ "$1" = add ]; then -XPOST \ -d "$2" \ -H "X-Api-Key: $API_KEY" \ - "$URL/$3" + "$URL/s/$3" elif [ "$1" = get ]; then curl -is "$URL/$2" | sed -En 's/^[lL]ocation: (.*)/\1/p' diff --git a/src/main.cpp b/src/main.cpp index 85f8596..3c8fa0f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,6 @@ +#include +#include + #include "crow.h" extern "C" { @@ -24,10 +27,12 @@ int main() { ENV(api_key, "LANDER_API_KEY"); ENV(base_url, "LANDER_BASE_URL"); + ENV(data_dir, "LANDER_DATA_DIR"); + // Initialize trie and populate from data file TernaryTrie *trie = ternarytrie_init(); - std::string file_path = "lander.data"; + std::string file_path = data_dir + "/lander.data"; std::cout << "Populating trie from file '" << file_path << "'..." << std::endl; @@ -42,6 +47,10 @@ int main() { std::cout << "Added " << count << " entries to trie." << std::endl; } + // Create pastes directory if not present + // TODO don't just ignore errors here + mkdir((data_dir + "/pastes").c_str(), 0700); + crow::SimpleApp app; app.loglevel(crow::LogLevel::Warning); @@ -60,7 +69,9 @@ int main() { res.end(); }); - CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)( + + // 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(); @@ -76,7 +87,9 @@ int main() { return crow::response(res); }); - CROW_ROUTE(app, "/") + + // Add a new Redirect with a given path + CROW_ROUTE(app, "/s/") .methods(crow::HTTPMethod::Post)( [api_key, base_url, trie](const crow::request &req, std::string key) { AUTH();