2022-11-21 15:45:31 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2022-11-15 16:05:47 +01:00
|
|
|
#include "crow.h"
|
2022-11-15 17:25:04 +01:00
|
|
|
|
|
|
|
extern "C" {
|
2022-11-15 17:05:14 +01:00
|
|
|
#include "ternarytrie.h"
|
2022-11-15 17:25:04 +01:00
|
|
|
}
|
2022-11-15 16:05:47 +01:00
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
#define ENV(var, env_var) \
|
2022-11-21 14:46:53 +01:00
|
|
|
const char *_##var = getenv(env_var); \
|
|
|
|
if (_##var == NULL) { \
|
2022-11-21 12:03:16 +01:00
|
|
|
printf("Missing environment variable %s.\n", env_var); \
|
|
|
|
return 1; \
|
2022-11-21 14:46:53 +01:00
|
|
|
} \
|
2022-11-21 14:19:56 +01:00
|
|
|
const std::string var = std::string(_##var);
|
2022-11-15 17:05:14 +01:00
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
#define AUTH() \
|
|
|
|
std::string provided_api_key = req.get_header_value("X-Api-Key"); \
|
2022-11-21 14:46:53 +01:00
|
|
|
if (api_key.compare(provided_api_key) != 0) { \
|
2022-11-21 12:03:16 +01:00
|
|
|
return crow::response(crow::status::UNAUTHORIZED); \
|
2022-11-15 21:12:08 +01:00
|
|
|
}
|
2022-11-15 16:05:47 +01:00
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
int main() {
|
2022-11-21 12:21:01 +01:00
|
|
|
// Initialize random seed for generating URLs
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2022-11-21 12:03:16 +01:00
|
|
|
ENV(api_key, "LANDER_API_KEY");
|
|
|
|
ENV(base_url, "LANDER_BASE_URL");
|
|
|
|
|
2022-11-21 15:45:31 +01:00
|
|
|
// Initialize trie and populate from data file
|
2022-11-15 21:12:08 +01:00
|
|
|
TernaryTrie *trie = ternarytrie_init();
|
2022-11-15 17:25:04 +01:00
|
|
|
|
2022-11-21 16:48:34 +01:00
|
|
|
std::string file_path = "lander.data";
|
2022-11-21 14:19:56 +01:00
|
|
|
|
2022-11-21 14:46:53 +01:00
|
|
|
std::cout << "Populating trie from file '" << file_path << "'..."
|
|
|
|
<< std::endl;
|
2022-11-21 14:19:56 +01:00
|
|
|
|
|
|
|
int count = ternarytrie_populate(trie, file_path.c_str());
|
|
|
|
|
|
|
|
if (count == -1) {
|
|
|
|
std::cout << "An error occured while populating the trie." << std::endl;
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
std::cout << "Added " << count << " entries to trie." << std::endl;
|
|
|
|
}
|
2022-11-15 22:49:36 +01:00
|
|
|
|
2022-11-21 15:45:31 +01:00
|
|
|
// Create pastes directory if not present
|
|
|
|
// TODO don't just ignore errors here
|
2022-11-21 16:48:34 +01:00
|
|
|
mkdir("pastes", 0700);
|
2022-11-21 15:45:31 +01:00
|
|
|
|
2022-11-15 21:12:08 +01:00
|
|
|
crow::SimpleApp app;
|
2022-11-21 16:22:52 +01:00
|
|
|
app.loglevel(crow::LogLevel::Info);
|
2022-11-15 21:12:08 +01:00
|
|
|
|
2022-11-21 16:28:27 +01:00
|
|
|
// Serve an entry
|
2022-11-15 21:12:08 +01:00
|
|
|
CROW_ROUTE(app, "/<string>")
|
|
|
|
.methods(crow::HTTPMethod::Get)(
|
2022-11-21 16:48:34 +01:00
|
|
|
[trie](crow::response &res, std::string key) {
|
2022-11-21 16:28:27 +01:00
|
|
|
Entry *entry = ternarytrie_search(trie, key.c_str());
|
2022-11-15 21:12:08 +01:00
|
|
|
|
2022-11-21 15:28:02 +01:00
|
|
|
if (entry != NULL) {
|
2022-11-21 16:28:27 +01:00
|
|
|
if (entry->type == Redirect) {
|
|
|
|
res.redirect(entry->string);
|
|
|
|
} else if (entry->type == Paste) {
|
2022-11-21 16:48:34 +01:00
|
|
|
res.set_static_file_info("pastes/" + key);
|
2022-11-21 16:28:27 +01:00
|
|
|
}
|
2022-11-15 21:12:08 +01:00
|
|
|
} else {
|
|
|
|
res.code = 404;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
2022-11-21 15:45:31 +01:00
|
|
|
|
|
|
|
// Add a new Redirect with a randomly generated key
|
2022-11-21 16:22:52 +01:00
|
|
|
CROW_ROUTE(app, "/s/")
|
|
|
|
.methods(crow::HTTPMethod::Post)(
|
|
|
|
[api_key, base_url, trie](const crow::request req) {
|
|
|
|
AUTH();
|
2022-11-21 12:03:16 +01:00
|
|
|
|
2022-11-21 16:22:52 +01:00
|
|
|
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
|
|
|
char *key = ternarytrie_add_random(trie, new_entry);
|
2022-11-21 12:03:16 +01:00
|
|
|
|
2022-11-21 16:22:52 +01:00
|
|
|
if (key == NULL) {
|
|
|
|
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
2022-11-21 12:03:16 +01:00
|
|
|
|
2022-11-21 16:22:52 +01:00
|
|
|
std::string res = base_url + key;
|
|
|
|
free(key);
|
2022-11-21 12:21:01 +01:00
|
|
|
|
2022-11-21 16:22:52 +01:00
|
|
|
return crow::response(res);
|
|
|
|
});
|
2022-11-21 15:45:31 +01:00
|
|
|
|
|
|
|
// Add a new Redirect with a given path
|
|
|
|
CROW_ROUTE(app, "/s/<string>")
|
2022-11-21 12:03:16 +01:00
|
|
|
.methods(crow::HTTPMethod::Post)(
|
2022-11-21 15:28:02 +01:00
|
|
|
[api_key, base_url, trie](const crow::request &req, std::string key) {
|
2022-11-21 12:03:16 +01:00
|
|
|
AUTH();
|
|
|
|
|
2022-11-21 15:28:02 +01:00
|
|
|
Entry *new_entry = entry_new(Redirect, req.body.c_str());
|
|
|
|
|
|
|
|
bool added = ternarytrie_add(trie, key.c_str(), new_entry);
|
2022-11-21 12:03:16 +01:00
|
|
|
|
2022-11-21 12:21:01 +01:00
|
|
|
if (!added) {
|
2022-11-21 12:03:16 +01:00
|
|
|
return crow::response(crow::status::CONFLICT);
|
|
|
|
}
|
|
|
|
|
2022-11-21 14:19:56 +01:00
|
|
|
return crow::response(base_url + key);
|
2022-11-21 12:03:16 +01:00
|
|
|
});
|
2022-11-15 21:12:08 +01:00
|
|
|
|
2022-11-21 16:22:52 +01:00
|
|
|
// Add a new Paste with a randomly generated key
|
|
|
|
CROW_ROUTE(app, "/p/")
|
|
|
|
.methods(crow::HTTPMethod::Post)(
|
2022-11-21 16:48:34 +01:00
|
|
|
[api_key, base_url, trie](const crow::request &req) {
|
2022-11-21 16:22:52 +01:00
|
|
|
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;
|
2022-11-21 16:48:34 +01:00
|
|
|
file.open(std::string("pastes/") + key, std::ios_base::out);
|
2022-11-21 16:22:52 +01:00
|
|
|
|
|
|
|
if (!file.is_open()) {
|
2022-11-21 19:48:01 +01:00
|
|
|
free(key);
|
2022-11-21 16:22:52 +01:00
|
|
|
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
file << req.body;
|
|
|
|
file.close();
|
|
|
|
|
2022-11-21 19:48:01 +01:00
|
|
|
std::string res = base_url + key;
|
|
|
|
free(key);
|
|
|
|
|
|
|
|
return crow::response(res);
|
2022-11-21 16:22:52 +01:00
|
|
|
});
|
|
|
|
|
2022-11-15 21:12:08 +01:00
|
|
|
app.port(18080).multithreaded().run();
|
2022-11-15 16:05:47 +01:00
|
|
|
}
|