feat: added randomly generated URLs
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
94f7400169
commit
51fc2867a8
6 changed files with 190 additions and 87 deletions
65
src/main.cpp
65
src/main.cpp
|
|
@ -4,15 +4,23 @@ extern "C" {
|
|||
#include "ternarytrie.h"
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Read in API key
|
||||
char *api_key = getenv("LANDER_API_KEY");
|
||||
|
||||
if (api_key == NULL) {
|
||||
printf("No API key provided.");
|
||||
return 1;
|
||||
#define ENV(var, env_var) \
|
||||
const char *var = getenv(env_var); \
|
||||
if (var == NULL) { \
|
||||
printf("Missing environment variable %s.\n", env_var); \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
#define AUTH() \
|
||||
std::string provided_api_key = req.get_header_value("X-Api-Key"); \
|
||||
if (strcmp(api_key, provided_api_key.c_str()) != 0) { \
|
||||
return crow::response(crow::status::UNAUTHORIZED); \
|
||||
}
|
||||
|
||||
int main() {
|
||||
ENV(api_key, "LANDER_API_KEY");
|
||||
ENV(base_url, "LANDER_BASE_URL");
|
||||
|
||||
TernaryTrie *trie = ternarytrie_init();
|
||||
|
||||
std::string file_path = "lander.data";
|
||||
|
|
@ -20,24 +28,8 @@ int main() {
|
|||
|
||||
crow::SimpleApp app;
|
||||
|
||||
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");
|
||||
app.loglevel(crow::LogLevel::Warning);
|
||||
|
||||
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) {
|
||||
|
|
@ -51,6 +43,31 @@ int main() {
|
|||
|
||||
res.end();
|
||||
});
|
||||
CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)(
|
||||
[api_key, base_url, trie](const crow::request req) {
|
||||
AUTH();
|
||||
|
||||
char *key = ternarytrie_add_random(trie, req.body.c_str());
|
||||
|
||||
if (key == NULL) {
|
||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
return crow::response(key);
|
||||
});
|
||||
CROW_ROUTE(app, "/<string>")
|
||||
.methods(crow::HTTPMethod::Post)(
|
||||
[api_key, trie](const crow::request &req, std::string s) {
|
||||
AUTH();
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
app.port(18080).multithreaded().run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue