-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.h
123 lines (96 loc) · 3.88 KB
/
notifications.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once
#include <iostream>
#include <string>
#include <cstdlib>
#include <curl/curl.h>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include "logger.h"
#include "lb_config_struct.h"
using namespace logger;
string loadEnv(string which)
{
std::ifstream file(".env");
if (!file.is_open()) {
std::cerr << "Failed to open .env file." << std::endl;
return "error";
}
std::string value1, value2;
std::getline(file, value1);
std::getline(file, value2);
file.close();
return which == "RESEND" ? value1 : value2;
}
// TO BE IMPLEMENTED
bool add_notification_to_db(string event)
{
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl)
{
struct curl_slist* headers = NULL;
std::string apiKeyHeader = "Authorization: Bearer " + loadEnv("RESEND");
std::string contentTypeHeader = "Content-Type: application/json";
headers = curl_slist_append(headers, apiKeyHeader.c_str());
headers = curl_slist_append(headers, contentTypeHeader.c_str());
curl_easy_setopt(curl, CURLOPT_URL, "https://yakshit-chhipa-s-workspace-p8bvk5.eu-central-1.xata.sh/db/cheapcloud:main/tables/notifications/query");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, std::string("{\"event\":\"" + event + "\"}"));
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
ltf("Error logging event to database: " + std::string(curl_easy_strerror(res)));
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
return false;
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
//ltf("Event logged to database successfully!");
return true;
}
bool notify_via_email(const std::string subject, const std::string body)
{
std::string from_email = "Computeflow-LoadBalancer <[email protected]>";
std::string to_email = LB_CONFIG::mail_id;
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
struct curl_slist* headers = NULL;
std::string apiKeyHeader = "Authorization: Bearer " + loadEnv("RESEND");
std::string contentTypeHeader = "Content-Type: application/json";
headers = curl_slist_append(headers, apiKeyHeader.c_str());
headers = curl_slist_append(headers, contentTypeHeader.c_str());
rapidjson::Document email_data;
email_data.SetObject();
rapidjson::Document::AllocatorType& allocator = email_data.GetAllocator();
email_data.AddMember("from", rapidjson::Value(from_email.c_str(), allocator), allocator);
email_data.AddMember("to", rapidjson::Value(to_email.c_str(), allocator), allocator);
email_data.AddMember("subject", rapidjson::Value(subject.c_str(), allocator), allocator);
email_data.AddMember("text", rapidjson::Value(body.c_str(), allocator), allocator);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
email_data.Accept(writer);
std::string email_data_str = buffer.GetString();
std::cout << std::endl << email_data_str << std::endl;
curl_easy_setopt(curl, CURLOPT_URL, "https://api.resend.com/emails");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, email_data_str.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
ltf("UNBALE TO NOTIFY SCALING UPDATE: ", curl_easy_strerror(res));
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
curl_global_cleanup();
//add_notification_to_db(subject);
return true;
}