feat: create pastes directory; separate routes
parent
f1ec643f80
commit
a22a2e5cfa
2
Makefile
2
Makefile
|
@ -31,7 +31,7 @@ prod: cmake-release
|
||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: build
|
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
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
|
|
|
@ -8,7 +8,7 @@ if [ "$1" = add ]; then
|
||||||
-XPOST \
|
-XPOST \
|
||||||
-d "$2" \
|
-d "$2" \
|
||||||
-H "X-Api-Key: $API_KEY" \
|
-H "X-Api-Key: $API_KEY" \
|
||||||
"$URL/$3"
|
"$URL/s/$3"
|
||||||
|
|
||||||
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'
|
||||||
|
|
19
src/main.cpp
19
src/main.cpp
|
@ -1,3 +1,6 @@
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "crow.h"
|
#include "crow.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -24,10 +27,12 @@ int main() {
|
||||||
|
|
||||||
ENV(api_key, "LANDER_API_KEY");
|
ENV(api_key, "LANDER_API_KEY");
|
||||||
ENV(base_url, "LANDER_BASE_URL");
|
ENV(base_url, "LANDER_BASE_URL");
|
||||||
|
ENV(data_dir, "LANDER_DATA_DIR");
|
||||||
|
|
||||||
|
// Initialize trie and populate from data file
|
||||||
TernaryTrie *trie = ternarytrie_init();
|
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::cout << "Populating trie from file '" << file_path << "'..."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
@ -42,6 +47,10 @@ int main() {
|
||||||
std::cout << "Added " << count << " entries to trie." << std::endl;
|
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;
|
crow::SimpleApp app;
|
||||||
|
|
||||||
app.loglevel(crow::LogLevel::Warning);
|
app.loglevel(crow::LogLevel::Warning);
|
||||||
|
@ -60,7 +69,9 @@ int main() {
|
||||||
|
|
||||||
res.end();
|
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) {
|
[api_key, base_url, trie](const crow::request req) {
|
||||||
AUTH();
|
AUTH();
|
||||||
|
|
||||||
|
@ -76,7 +87,9 @@ int main() {
|
||||||
|
|
||||||
return crow::response(res);
|
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)(
|
.methods(crow::HTTPMethod::Post)(
|
||||||
[api_key, base_url, trie](const crow::request &req, std::string key) {
|
[api_key, base_url, trie](const crow::request &req, std::string key) {
|
||||||
AUTH();
|
AUTH();
|
||||||
|
|
Loading…
Reference in New Issue