lander/src/main.cpp

54 lines
1.3 KiB
C++
Raw Normal View History

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
int main() {
// Read in API key
char *api_key = getenv("LANDER_API_KEY");
2022-11-15 17:05:14 +01:00
if (api_key == NULL) {
printf("No API key provided.");
return 1;
}
2022-11-15 16:05:47 +01:00
TernaryTrie *trie = ternarytrie_init();
2022-11-15 17:25:04 +01:00
crow::SimpleApp app;
2022-11-15 17:25:04 +01:00
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, trie](const crow::request &req, std::string s) {
// Authenticate request
std::string provided_api_key = req.get_header_value("X-Api-Key");
2022-11-15 16:05:47 +01:00
if (strcmp(api_key, provided_api_key.c_str()) != 0) {
return crow::response(crow::status::UNAUTHORIZED);
}
bool res = ternarytrie_add(trie, s.c_str(), req.body.c_str());
if (!res) {
return crow::response(crow::status::CONFLICT);
}
return crow::response(crow::status::NO_CONTENT);
});
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Get)(
[trie](crow::response &res, std::string s) {
char *payload = ternarytrie_search(trie, s.c_str());
if (payload != NULL) {
res.redirect(payload);
} else {
res.code = 404;
}
res.end();
});
app.port(18080).multithreaded().run();
2022-11-15 16:05:47 +01:00
}