feat: use more std::string
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
parent
26318de22e
commit
494946d24a
|
@ -13,3 +13,10 @@ pipeline:
|
||||||
secrets:
|
secrets:
|
||||||
- 'docker_username'
|
- 'docker_username'
|
||||||
- 'docker_password'
|
- 'docker_password'
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
image: 'curlimages/curl'
|
||||||
|
secrets:
|
||||||
|
- 'webhook_url'
|
||||||
|
commands:
|
||||||
|
- 'curl -XPOST -s --fail $WEBHOOK_URL'
|
||||||
|
|
|
@ -11,5 +11,5 @@ if [ "$1" = add ]; then
|
||||||
"$URL/$3"
|
"$URL/$3"
|
||||||
|
|
||||||
elif [ "$1" = get ]; then
|
elif [ "$1" = get ]; then
|
||||||
curl -is "$URL/$2" | grep -Po '(?<=location: ).*'
|
curl -is "$URL/$2" | sed -En 's/^location: (.*)/\1/p'
|
||||||
fi
|
fi
|
||||||
|
|
42
src/main.cpp
42
src/main.cpp
|
@ -5,15 +5,16 @@ extern "C" {
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ENV(var, env_var) \
|
#define ENV(var, env_var) \
|
||||||
const char *var = getenv(env_var); \
|
const char* _##var = getenv(env_var); \
|
||||||
if (var == NULL) { \
|
if (_##var == NULL) { \
|
||||||
printf("Missing environment variable %s.\n", env_var); \
|
printf("Missing environment variable %s.\n", env_var); \
|
||||||
return 1; \
|
return 1; \
|
||||||
}
|
} \
|
||||||
|
const std::string var = std::string(_##var);
|
||||||
|
|
||||||
#define AUTH() \
|
#define AUTH() \
|
||||||
std::string provided_api_key = req.get_header_value("X-Api-Key"); \
|
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); \
|
return crow::response(crow::status::UNAUTHORIZED); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,12 +25,21 @@ int main() {
|
||||||
ENV(api_key, "LANDER_API_KEY");
|
ENV(api_key, "LANDER_API_KEY");
|
||||||
ENV(base_url, "LANDER_BASE_URL");
|
ENV(base_url, "LANDER_BASE_URL");
|
||||||
|
|
||||||
const size_t base_url_len = strlen(base_url);
|
|
||||||
|
|
||||||
TernaryTrie *trie = ternarytrie_init();
|
TernaryTrie *trie = ternarytrie_init();
|
||||||
|
|
||||||
std::string file_path = "lander.data";
|
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;
|
crow::SimpleApp app;
|
||||||
|
|
||||||
|
@ -49,7 +59,7 @@ int main() {
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
CROW_ROUTE(app, "/").methods(crow::HTTPMethod::Post)(
|
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();
|
AUTH();
|
||||||
|
|
||||||
char *key = ternarytrie_add_random(trie, req.body.c_str());
|
char *key = ternarytrie_add_random(trie, req.body.c_str());
|
||||||
|
@ -58,31 +68,25 @@ int main() {
|
||||||
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
return crow::response(crow::status::INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concatenate base URL & key
|
std::string res = base_url + key;
|
||||||
char *res = (char *)malloc(base_url_len + RANDOM_KEY_LENGTH + 1);
|
free(key);
|
||||||
memcpy(res, base_url, base_url_len);
|
|
||||||
memcpy(res + base_url_len, key, RANDOM_KEY_LENGTH + 1);
|
|
||||||
|
|
||||||
return crow::response(res);
|
return crow::response(res);
|
||||||
});
|
});
|
||||||
CROW_ROUTE(app, "/<string>")
|
CROW_ROUTE(app, "/<string>")
|
||||||
.methods(crow::HTTPMethod::Post)(
|
.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) {
|
std::string s) {
|
||||||
AUTH();
|
AUTH();
|
||||||
|
|
||||||
|
std::string key = req.body;
|
||||||
bool added = ternarytrie_add(trie, s.c_str(), req.body.c_str());
|
bool added = ternarytrie_add(trie, s.c_str(), req.body.c_str());
|
||||||
|
|
||||||
if (!added) {
|
if (!added) {
|
||||||
return crow::response(crow::status::CONFLICT);
|
return crow::response(crow::status::CONFLICT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concatenate base URL & key
|
return crow::response(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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.port(18080).multithreaded().run();
|
app.port(18080).multithreaded().run();
|
||||||
|
|
|
@ -30,7 +30,7 @@ typedef struct ttrie TernaryTrie;
|
||||||
*/
|
*/
|
||||||
TernaryTrie* ternarytrie_init();
|
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.
|
* De-allocate a trie by freeing the memory occupied by this trie.
|
||||||
|
|
|
@ -39,22 +39,22 @@ void ternarytrie_free(TernaryTrie *trie) {
|
||||||
|
|
||||||
bool ternarytrie_add_internal(TernaryTrie *trie, const char *string, const char *payload);
|
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);
|
trie->file_path = my_strdup(file_path);
|
||||||
|
|
||||||
FILE* fp = fopen(file_path, "r");
|
FILE* fp = fopen(file_path, "r");
|
||||||
|
|
||||||
// TODO properly handle this
|
// TODO properly handle this
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We read in lines of at most 8192 characters (sounds like enough)
|
// We read in lines of at most 8192 characters (sounds like enough)
|
||||||
char buffer[8192];
|
char buffer[8192];
|
||||||
int i, j;
|
int i, j;
|
||||||
|
int entries = 0;
|
||||||
|
|
||||||
while (fgets(buffer, 8192, fp)) {
|
while (fgets(buffer, 8192, fp)) {
|
||||||
printf("%s", buffer);
|
|
||||||
// Find index of space character
|
// Find index of space character
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
|
@ -75,9 +75,12 @@ void ternarytrie_populate(TernaryTrie *trie, const char *file_path) {
|
||||||
buffer[j] = '\0';
|
buffer[j] = '\0';
|
||||||
|
|
||||||
ternarytrie_add_internal(trie, buffer, buffer + i + 1);
|
ternarytrie_add_internal(trie, buffer, buffer + i + 1);
|
||||||
|
entries++;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct searchresult {
|
typedef struct searchresult {
|
||||||
|
|
Loading…
Reference in New Issue