base of API is created

This commit is contained in:
Ivanov Matvey 2025-01-13 04:58:58 +10:00
parent b8325ceceb
commit b20ce7c4de
8 changed files with 135 additions and 35 deletions

View File

@ -1,2 +1,2 @@
default_target:
g++ socket_up.cpp socket_handler.cpp main.cpp -o main -lws2_32 && main.exe
g++ socket_up.cpp socket_handler.cpp routes.cpp main.cpp -o main -lws2_32 && main.exe

View File

@ -3,34 +3,14 @@
#include <iostream>
int main() {
SOCKET listenSocket = SocketUp::config_socket_windows();
SocketHandler::working_with_client_windows(listenSocket);
// 6. Принятие входящего соединения
sockaddr_in clientAddr;
int clientAddrSize = sizeof(clientAddr);
SOCKET clientSocket = accept(listenSocket, (struct sockaddr*)&clientAddr, &clientAddrSize);
if (clientSocket == INVALID_SOCKET) {
std::cerr << "Accept failed with error: " << WSAGetLastError() << "\n";
closesocket(listenSocket);
WSACleanup();
return 1;
}
std::cout << "Client connected!\n";
// 7. Отправка данных клиенту
const char* message = "new message from server!\n";
int bytesSent = send(clientSocket, message, strlen(message), 0);
if (bytesSent == SOCKET_ERROR) {
std::cerr << "Send failed with error: " << WSAGetLastError() << "\n";
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 1;
}
std::cout << "Message sent to client.\n";
return 0;
}

37
task_5/routes.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "routes.h"
std::string RouteHandler::get_response_by_request(const std::string& request) {
if (request.find("GET /current") != std::string::npos) {
return get_current_temperature();
}
return format_http_response("404 NOT FOUND", "404 Not Found");
}
std::string RouteHandler::get_current_temperature() {
std::string temperature_info = (
"{\"temperature\": \"22°C\", \"weather\": \"good\"}"
);
return format_http_response("200 OK", temperature_info);
}
std::string RouteHandler::format_http_response(std::string status, std::string text) {
return (
"HTTP/1.1 " + status + "\r\n"
"Content-Type: application/json\r\n"
"Content-Length: " + std::to_string(text.length()) + "\r\n"
"\r\n"
+ text
);
}

12
task_5/routes.h Normal file
View File

@ -0,0 +1,12 @@
#include <string>
class RouteHandler {
public:
static std::string get_response_by_request(const std::string& request);
protected:
static std::string format_http_response(std::string status, std::string text);
private:
static std::string get_current_temperature();
};

View File

@ -0,0 +1,54 @@
#include "socket_handler.h"
#include "routes.h"
const int BUFFER_SIZE = 4096;
int SocketHandler::working_with_client_windows(SOCKET listenSocket) {
while (1) {
// 6. Принятие входящего соединения
sockaddr_in clientAddr;
int clientAddrSize = sizeof(clientAddr);
SOCKET clientSocket = accept(listenSocket, (struct sockaddr*)&clientAddr, &clientAddrSize);
if (clientSocket == INVALID_SOCKET) {
std::cerr << "Accept failed with error: " << WSAGetLastError() << "\n";
continue;
}
std::cout << "Client connected!\n";
// 7. получение данных от юзера
char buffer[BUFFER_SIZE] = {0};
int bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE, 0);
if (bytesReceived > 0) {
std::string request(buffer, bytesReceived);
std::cout << "Received request:\n" << request << "\n";
// Обработка запроса
std::string response = RouteHandler::get_response_by_request(request);
std::cout << "Response:\n" << response << "\n\n";
// Отправка ответа
send(clientSocket, response.c_str(), response.size(), 0);
}
closesocket(clientSocket);
}
return 0;
}
int SocketHandler::working_with_client_unix() {
return 0;
}

View File

@ -0,0 +1,19 @@
#ifdef _WIN32
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <cstring>
#else
#include <sys/socket.h>
#endif
#include <iostream>
class SocketHandler {
public:
static int working_with_client_windows(SOCKET listenSocket);
static int working_with_client_unix();
};

View File

@ -49,15 +49,17 @@ SOCKET SocketUp::config_socket_windows() {
return listenSocket;
// // 8. Закрытие клиентского сокета
// 8. Закрытие клиентского сокета
// closesocket(clientSocket);
// // 9. Закрытие серверного сокета и завершение Winsock
// closesocket(listenSocket);
// WSACleanup();
// std::cout << "Server shut down.\n";
return 0;
}
int SocketUp::config_socket_unix() {
return 0;
}

View File

@ -14,11 +14,7 @@
class SocketUp {
public:
#ifdef _WIN32
static SOCKET config_socket_windows();
#else
// SOCKET?
static SOCKET config_socket_unix();
#endif
static int config_socket_unix();
};