-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
86 lines (66 loc) · 2.84 KB
/
main.cpp
File metadata and controls
86 lines (66 loc) · 2.84 KB
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
#include <iostream>
#include <string>
#include <curl/curl.h>
#include "include/crow_all.h"
#include "include/config.h"
using namespace std;
// Construim comanda Cypher pentru crearea utilizatorului
string construiesteCypher(const string& nume,
const string& email,
const string& parola) {
return "CREATE (u:User {nume: '" + nume +
"', email: '" + email +
"', parola: '" + parola + "'})";
}
// Construim JSON-ul necesar pentru API-ul Neo4j
string construiesteJSON(const string& cypher) {
return "{\"statements\": [{\"statement\": \"" + cypher + "\"}]}";
}
// Trimitem datele către baza de date folosind libcurl
bool trimiteLaNeo4j(const string& json) {
CURL* curl = curl_easy_init();
if (!curl) return false;
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/json");
// Folosim token-ul din config.h pentru securitate
string authHeader = "Authorization: Basic " + NEO4J_AUTH_TOKEN;
headers = curl_slist_append(headers, authHeader.c_str());
curl_easy_setopt(curl, CURLOPT_URL, NEO4J_URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str());
CURLcode res = curl_easy_perform(curl);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return (res == CURLE_OK);
}
int main() {
crow::App<crow::CORSHandler> app;
// Configurare CORS pentru a permite cereri de la site-ul tău (Site_page)
auto& cors = app.get_middleware<crow::CORSHandler>();
cors.global()
.origin("*")
.methods("POST"_method, "GET"_method, "OPTIONS"_method)
.headers("Content-Type");
// Endpoint-ul POST care primește datele de la formularul web
CROW_ROUTE(app, "/create_user").methods("POST"_method)
([](const crow::request& req){
auto body = crow::json::load(req.body);
if (!body) return crow::response(400, "JSON invalid");
string nume = body["nume"].s();
string email = body["email"].s();
string parola = body["parola"].s();
cout << "Log: Inregistrare utilizator nou: " << nume << " (" << email << ")" << endl;
string cypher = construiesteCypher(nume, email, parola);
string json = construiesteJSON(cypher);
if (trimiteLaNeo4j(json)) {
return crow::response(200, "Utilizator salvat in Neo4j cu succes!");
} else {
return crow::response(500, "Eroare la comunicarea cu baza de date!");
}
});
cout << "-------------------------------------------" << endl;
cout << "Serverul C++ este activ pe http://localhost:18080" << endl;
cout << "Astept date de la Site_page..." << endl;
cout << "-------------------------------------------" << endl;
app.port(18080).multithreaded().run();
}