forked from ignacioelizeche/Backend-Delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
73 lines (61 loc) · 2.37 KB
/
Copy pathapi.cpp
File metadata and controls
73 lines (61 loc) · 2.37 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
#include "api.h"
#include "auth_routes.h"
#include "dashboard_routes.h"
#include "calendar_routes.h"
#include "problems_routes.h"
#include "forum_routes.h"
#include "exams_routes.h"
#include "notebooks_routes.h"
#include "response_utils.h"
#include "visualizations_routes.h"
#include "ranking_routes.h"
#include <QHttpServerResponse>
#include <QHostAddress>
#include <QDebug>
// Constructor con la configuración y arranque del servidor
API::API(QObject *parent) : QObject(parent) {
m_httpServer = new QHttpServer(this);
m_tcpServer = new QTcpServer(this);
// Ruta OPTIONS para CORS general
m_httpServer->route("/<QString>", QHttpServerRequest::Method::Options, [](const QHttpServerRequest &) {
QHttpServerResponse response(QHttpServerResponse::StatusCode::Ok);
QHttpHeaders headers;
headers.append("Access-Control-Allow-Origin", "*");
headers.append("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
headers.append("Access-Control-Allow-Headers", "Content-Type, Authorization");
response.setHeaders(headers);
return response;
});
// Ruta /health
m_httpServer->route("/health", QHttpServerRequest::Method::Get,
[]() {
QJsonObject json;
json["status"] = "OK";
json["message"] = "Server is running";
// Create the CORS response and send it using the responder
return createCorsResponse(json);
});
// Configurar rutas de módulos
AuthRoutes::setupRoutes(m_httpServer);
DashboardRoutes::setupRoutes(m_httpServer);
CalendarRoutes::setupRoutes(m_httpServer);
ProblemsRoutes::setupRoutes(m_httpServer);
ForumRoutes::setupRoutes(m_httpServer);
VisualizationsRoutes::setupRoutes(m_httpServer);
ExamsRoutes::setupRoutes(m_httpServer);
NotebooksRoutes::setupRoutes(m_httpServer);
// Iniciar TCP server en puerto 8080
if (!m_tcpServer->listen(QHostAddress::Any, 8080)) {
qDebug() << "Failed to start server on port 8080, trying random port...";
if (!m_tcpServer->listen()) {
qDebug() << "Failed to start TCP server completely";
return;
}
}
// Bind HTTP server a TCP server
if (!m_httpServer->bind(m_tcpServer)) {
qDebug() << "Failed to bind HTTP server to TCP server";
return;
}
qDebug() << "Server listening on port" << m_tcpServer->serverPort();
}