feat: use more std::string
ci/woodpecker/push/woodpecker Pipeline was successful Details

trie-skips
Jef Roosens 2022-11-21 14:19:56 +01:00
parent 26318de22e
commit 494946d24a
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 38 additions and 24 deletions

View File

@ -13,3 +13,10 @@ pipeline:
secrets:
- 'docker_username'
- 'docker_password'
deploy:
image: 'curlimages/curl'
secrets:
- 'webhook_url'
commands:
- 'curl -XPOST -s --fail $WEBHOOK_URL'

View File

@ -11,5 +11,5 @@ if [ "$1" = add ]; then
"$URL/$3"
elif [ "$1" = get ]; then
curl -is "$URL/$2" | grep -Po '(?<=location: ).*'
curl -is "$URL/$2" | sed -En 's/^location: (.*)/\1/p'
fi

View File

@ -5,15 +5,16 @@ extern "C" {
}
#define ENV(var, env_var) \
const char *var = getenv(env_var); \
if (var == NULL) { \
const char* _##var = getenv(env_var); \
if (_##var == NULL) { \
printf("Missing environment variable %s.\n", env_var); \
return 1; \
}
} \
const std::string var = std::string(_##var);
#define AUTH() \
std::string provided_api_key = req.get_header_value("X-Api-Key"); \
if (strcmp(api_key, provided_api_key.c_str()) != 0) { \
if (api_key.compare(provided_api_key) != 0) { \
return crow::response(crow::status::UNAUTHORIZED); \
}
@ -24,12 +25,21 @@ int main() {
ENV(api_key, "LANDER_API_KEY");
ENV(base_url, "LANDER_BASE_URL");
const size_t base_url_len = strlen(base_url);
TernaryTrie *trie = ternarytrie_init();
std::string file_path = "lander.data";
ternarytrie_populate(trie, file_path.c_str());
std::cout << "Populating trie from file '" << file_path << "'..." << std::endl;
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;
}
crow::SimpleApp app;
@ -49,7 +59,7 @@ int main() {
res.end();
});
CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)(
[api_key, base_url, base_url_len, trie](const crow::request req) {
[api_key, base_url, trie](const crow::request req) {
AUTH();
char *key = ternarytrie_add_random(trie, req.body.c_str());
@ -58,31 +68,25 @@ int main() {
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
}
// Concatenate base URL & key
char *res = (char *)malloc(base_url_len + RANDOM_KEY_LENGTH + 1);
memcpy(res, base_url, base_url_len);
memcpy(res + base_url_len, key, RANDOM_KEY_LENGTH + 1);
std::string res = base_url + key;
free(key);
return crow::response(res);
});
CROW_ROUTE(app, "/<string>")
.methods(crow::HTTPMethod::Post)(
[api_key, base_url, base_url_len, trie](const crow::request &req,
[api_key, base_url, trie](const crow::request &req,
std::string s) {
AUTH();
std::string key = req.body;
bool added = ternarytrie_add(trie, s.c_str(), req.body.c_str());
if (!added) {
return crow::response(crow::status::CONFLICT);
}
// Concatenate base URL & key
char *res = (char *)malloc(base_url_len + RANDOM_KEY_LENGTH + 1);
memcpy(res, base_url, base_url_len);
memcpy(res + base_url_len, s.c_str(), RANDOM_KEY_LENGTH + 1);
return crow::response(res);
return crow::response(base_url + key);
});
app.port(18080).multithreaded().run();

View File

@ -30,7 +30,7 @@ typedef struct ttrie TernaryTrie;
*/
TernaryTrie* ternarytrie_init();
void ternarytrie_populate(TernaryTrie* trie, const char* file_path);
int ternarytrie_populate(TernaryTrie* trie, const char* file_path);
/**
* De-allocate a trie by freeing the memory occupied by this trie.

View File

@ -39,22 +39,22 @@ void ternarytrie_free(TernaryTrie *trie) {
bool ternarytrie_add_internal(TernaryTrie *trie, const char *string, const char *payload);
void ternarytrie_populate(TernaryTrie *trie, const char *file_path) {
int ternarytrie_populate(TernaryTrie *trie, const char *file_path) {
trie->file_path = my_strdup(file_path);
FILE* fp = fopen(file_path, "r");
// TODO properly handle this
if (fp == NULL) {
return;
return -1;
}
// We read in lines of at most 8192 characters (sounds like enough)
char buffer[8192];
int i, j;
int entries = 0;
while (fgets(buffer, 8192, fp)) {
printf("%s", buffer);
// Find index of space character
i = 0;
@ -75,9 +75,12 @@ void ternarytrie_populate(TernaryTrie *trie, const char *file_path) {
buffer[j] = '\0';
ternarytrie_add_internal(trie, buffer, buffer + i + 1);
entries++;
}
fclose(fp);
return entries;
}
typedef struct searchresult {