Basic working version without persistent storage
This commit is contained in:
parent
cae62ce7d2
commit
a2fcbb4224
8 changed files with 83 additions and 25 deletions
58
src/main.cpp
58
src/main.cpp
|
|
@ -4,26 +4,50 @@ extern "C" {
|
|||
#include "ternarytrie.h"
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
TernaryTrie *trie = ternarytrie_init();
|
||||
int main() {
|
||||
// Read in API key
|
||||
char *api_key = getenv("LANDER_API_KEY");
|
||||
|
||||
crow::SimpleApp app;
|
||||
if (api_key == NULL) {
|
||||
printf("No API key provided.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
CROW_ROUTE(app, "/<string>").methods(crow::HTTPMethod::Post)
|
||||
([trie](std::string s){
|
||||
ternarytrie_add(trie, s.c_str());
|
||||
TernaryTrie *trie = ternarytrie_init();
|
||||
|
||||
return "added";
|
||||
});
|
||||
CROW_ROUTE(app, "/<string>").methods(crow::HTTPMethod::Get)
|
||||
([trie](std::string s){
|
||||
if (ternarytrie_search(trie, s.c_str())) {
|
||||
return "it's here";
|
||||
}
|
||||
crow::SimpleApp app;
|
||||
|
||||
return "nope";
|
||||
});
|
||||
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.port(18080).multithreaded().run();
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue