fix: assume server runs in working directory
ci/woodpecker/push/woodpecker Pipeline was successful Details

trie-skips
Jef Roosens 2022-11-21 16:48:34 +01:00
parent 97ed770166
commit 425695596b
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 9 additions and 10 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
build/ build/
compile_commands.json compile_commands.json
# Data files
lander.data lander.data
data/ pastes/

View File

@ -26,8 +26,6 @@ RUN make prod && \
FROM alpine:3.16.3 FROM alpine:3.16.3
ENV LANDER_DATA_DIR=/data
RUN apk add --update --no-cache boost && \ RUN apk add --update --no-cache boost && \
mkdir /data && \ mkdir /data && \
chown -R 1000:1000 /data chown -R 1000:1000 /data

View File

@ -27,12 +27,11 @@ 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 // Initialize trie and populate from data file
TernaryTrie *trie = ternarytrie_init(); TernaryTrie *trie = ternarytrie_init();
std::string file_path = data_dir + "/lander.data"; std::string file_path = "lander.data";
std::cout << "Populating trie from file '" << file_path << "'..." std::cout << "Populating trie from file '" << file_path << "'..."
<< std::endl; << std::endl;
@ -49,7 +48,7 @@ int main() {
// Create pastes directory if not present // Create pastes directory if not present
// TODO don't just ignore errors here // TODO don't just ignore errors here
mkdir((data_dir + "/pastes").c_str(), 0700); mkdir("pastes", 0700);
crow::SimpleApp app; crow::SimpleApp app;
app.loglevel(crow::LogLevel::Info); app.loglevel(crow::LogLevel::Info);
@ -57,14 +56,14 @@ int main() {
// Serve an entry // Serve an entry
CROW_ROUTE(app, "/<string>") CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Get)( .methods(crow::HTTPMethod::Get)(
[data_dir, trie](crow::response &res, std::string key) { [trie](crow::response &res, std::string key) {
Entry *entry = ternarytrie_search(trie, key.c_str()); Entry *entry = ternarytrie_search(trie, key.c_str());
if (entry != NULL) { if (entry != NULL) {
if (entry->type == Redirect) { if (entry->type == Redirect) {
res.redirect(entry->string); res.redirect(entry->string);
} else if (entry->type == Paste) { } else if (entry->type == Paste) {
res.set_static_file_info(data_dir + "/pastes/" + key); res.set_static_file_info("pastes/" + key);
} }
} else { } else {
res.code = 404; res.code = 404;
@ -112,7 +111,7 @@ int main() {
// Add a new Paste with a randomly generated key // Add a new Paste with a randomly generated key
CROW_ROUTE(app, "/p/") CROW_ROUTE(app, "/p/")
.methods(crow::HTTPMethod::Post)( .methods(crow::HTTPMethod::Post)(
[api_key, base_url, data_dir, trie](const crow::request &req) { [api_key, base_url, trie](const crow::request &req) {
AUTH(); AUTH();
Entry *new_entry = entry_new(Paste, ""); Entry *new_entry = entry_new(Paste, "");
@ -124,7 +123,7 @@ int main() {
// Write paste contents to file // Write paste contents to file
std::fstream file; std::fstream file;
file.open(data_dir + "/pastes/" + key, std::ios_base::out); file.open(std::string("pastes/") + key, std::ios_base::out);
if (!file.is_open()) { if (!file.is_open()) {
return crow::response(crow::status::INTERNAL_SERVER_ERROR); return crow::response(crow::status::INTERNAL_SERVER_ERROR);