Implement DatabaseBackend, Verschiedenes

This commit is contained in:
Carsten Graf
2025-06-06 22:07:47 +02:00
parent 4f4d0757d5
commit a1434347d8
10 changed files with 256 additions and 681 deletions

67
src/databasebackend.h Normal file
View File

@@ -0,0 +1,67 @@
#include <Arduino.h>
#include <HTTPClient.h>
#include "master.h"
const char* BACKEND_SERVER = "http://db.reptilfpv.de:3000";
const char* BACKEND_TOKEN = "a4514dc0-15f5-4299-8826-fffb3139d39c";
bool backendOnline() {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("No WiFi connection.");
return false;
}
HTTPClient http;
http.begin(String(BACKEND_SERVER) + "/api/health");
http.addHeader("Authorization", String("Bearer ") + BACKEND_TOKEN);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
return true;
Serial.println("Database server connection successful");
} else {
return false;
Serial.printf("Database server connection failed, error: %d\n", httpCode);
}
http.end();
}
bool userExists(const String& userId) {
if (!backendOnline()) {
Serial.println("No internet connection, cannot check user existence.");
return false;
}
HTTPClient http;
http.begin(String(BACKEND_SERVER) + "/api/users/" + userId);
http.addHeader("Authorization", String("Bearer ") + BACKEND_TOKEN);
//Post request to check if user exists
int httpCode = http.POST("");
}
void setupBackendRoutes(AsyncWebServer& server) {
server.on("/api/health", HTTP_GET, [](AsyncWebServerRequest *request) {
DynamicJsonDocument doc(64);
doc["status"] = backendOnline() ? "connected" : "disconnected";
String response;
serializeJson(doc, response);
request->send(200, "application/json", response);
});
server.on("/api/users", HTTP_GET, [](AsyncWebServerRequest *request) {
if (!backendOnline()) {
request->send(503, "application/json", "{\"error\":\"Database not connected\"}");
return;
}
// Handle user retrieval logic here
});
// Add more routes as needed
}