feat: create pastes directory; separate routes

trie-skips
Jef Roosens 2022-11-21 15:45:31 +01:00
parent f1ec643f80
commit a22a2e5cfa
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 18 additions and 5 deletions

View File

@ -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:

View File

@ -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'

View File

@ -1,3 +1,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#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, "/<string>")
// Add a new Redirect with a given path
CROW_ROUTE(app, "/s/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, base_url, trie](const crow::request &req, std::string key) {
AUTH();