Implement DatabaseBackend, Verschiedenes
This commit is contained in:
67
src/databasebackend.h
Normal file
67
src/databasebackend.h
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user