only_the_truth/src/srv.cpp
radi0dev bd58ea66de 1
2026-01-30 16:48:11 +03:00

97 lines
2.8 KiB
C++

#include "srv.h"
#include "tplr.h"
#include <cstdlib>
#include <ctime>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <algorithm>
Server::Server(int port, const std::vector<std::string>* questions, size_t n, const std::map<std::string, std::string>* templates)
: port(port), questions(questions), n(n), templates(templates) {
std::srand(std::time(nullptr));
}
std::string Server::getRandomQuestion() {
if (questions->size() == recentQuestions.size()) {
recentQuestions.clear();
}
int index;
bool unique;
do {
index = std::rand() % questions->size();
unique = std::find(recentQuestions.begin(), recentQuestions.end(), index) == recentQuestions.end();
} while (!unique);
recentQuestions.push_back(index);
if (recentQuestions.size() > n) {
recentQuestions.pop_front();
}
return (*questions)[index];
}
void Server::handleRequest(int client_socket) {
char buffer[1024] = {0};
read(client_socket, buffer, sizeof(buffer));
std::string request(buffer);
std::string response;
if (request.find("GET /q") == 0) {
std::string question = getRandomQuestion();
std::map<std::string, std::string> context = {{"QUESTION", question}};
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + renderTemplate(templates->at("tpl/tpl_q.html"), context);
} else if (request.find("GET /about") == 0) {
response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" + templates->at("tpl/tpl_about.html");
} else {
response = "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found";
}
send(client_socket, response.c_str(), response.size(), 0);
close(client_socket);
}
void Server::run() {
int server_fd;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Ошибка создания сокета");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("Ошибка настройки сокета");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("Ошибка привязки сокета");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("Ошибка при прослушивании");
exit(EXIT_FAILURE);
}
while (true) {
int client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen);
if (client_socket < 0) {
perror("Ошибка при принятии соединения");
exit(EXIT_FAILURE);
}
handleRequest(client_socket);
}
}