This commit is contained in:
radi0dev 2026-01-30 16:48:11 +03:00
commit bd58ea66de
10 changed files with 395 additions and 0 deletions

74
src/main.cpp Normal file
View file

@ -0,0 +1,74 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <sstream>
#include "srv.h"
#include "tplr.h"
void loadQuestions(const std::string& filename, std::vector<std::string>& questions) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Ошибка: Не удалось открыть файл " << filename << std::endl;
exit(EXIT_FAILURE);
}
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
questions.push_back(line);
}
}
}
std::map<std::string, std::string> loadTemplates(const std::vector<std::string>& templateFiles) {
std::map<std::string, std::string> templates;
for (const auto& filePath : templateFiles) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Ошибка: Не удалось открыть шаблон " << filePath << std::endl;
exit(EXIT_FAILURE);
}
std::stringstream buffer;
buffer << file.rdbuf();
templates[filePath] = buffer.str();
}
return templates;
}
int main(int argc, char* argv[]) {
std::string questionsFile = "questions.list";
int n = 3;
int port = 8080;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-q" && (i + 1) < argc) {
questionsFile = argv[++i];
} else if (arg == "-n" && (i + 1) < argc) {
n = std::stoi(argv[++i]);
} else if (arg == "-p" && (i + 1) < argc) {
port = std::stoi(argv[++i]);
}
}
std::vector<std::string> questions;
loadQuestions(questionsFile, questions);
if (n > questions.size()) {
std::cerr << "Ошибка: Параметр n превышает количество вопросов в файле." << std::endl;
exit(EXIT_FAILURE);
}
std::vector<std::string> templateFiles = {"tpl/tpl_q.html", "tpl/tpl_about.html"};
auto templates = loadTemplates(templateFiles);
Server srv(port, &questions, n, &templates);
srv.run();
return 0;
}

97
src/srv.cpp Normal file
View file

@ -0,0 +1,97 @@
#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);
}
}

15
src/tplr.cpp Normal file
View file

@ -0,0 +1,15 @@
#include "tplr.h"
std::string renderTemplate(const std::string& templateContent, const std::map<std::string, std::string>& context) {
std::string content = templateContent;
for (const auto& pair : context) {
size_t pos;
while ((pos = content.find("{{" + pair.first + "}}")) != std::string::npos) {
content.replace(pos, pair.first.length() + 4, pair.second);
}
}
return content;
}