/
ihtiandr9
/
httpserver
Обзор
Документация
Войти
/
ihtiandr9
/
httpserver
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
class-stylesheet
cpp/src/server.cpp
98 строк
4 KB
lomaster
greeting page params
14 фев 2026, 20:05
14 фев 2026, 20:05
7e6342c
Код
Авторство
О чём код?
#include <httplib.h> #include <iostream> #include <getopt.h> #include "server.h" #include "greetingpage.h" using namespace httplib; static void init_server_targets(Server& svr) { svr.Get("/textpage", [](const Request &req, Response &res) { // По запросу к адресу "/textpage" Вернуть текст как страницу std::cout << "Get /textpage of site req\n"; //FIXME debug res.set_content("Text Page!", "text/plain"); //Текст для возврата и формат for (auto p : req.params) { std::cout<<p.first<<":"<<p.second<<std::endl; } }); //////////////////////////////////////////////////////// svr.Get("/", [](const Request &req, Response &res) { // По запросу к адресу "/" Вернуть greetingpage std::cout << "Get Root page subcommand\n"; //FIXME debug std::string s = ""; res.set_content(greetingPage(req.params), "text/html"); }); //////////////////////////////////////////////////////// svr.Get("/[a-z]+\\.css", [](const Request &req, Response &res) { std::cout << "The file is requested try to get it in static\n"; //FIXME debug // Other unrecognized commands to root std::cout<<"Path: "<<req.path<<std::endl; //FIXME debug std::cout<<"Method: "<<req.method<<std::endl; //FIXME debug std::cout<<"Target: "<<req.target<<std::endl; //FIXME debug // try get static if (!get_static(req.target, res, "text/css")) { std::cout << "file not exists in static\n"; //FIXME debug res.set_content("file not exists in static!\n", "text/plain"); //Текст для возврата и формат } }); //////////////////////////////////////////////////////// svr.Get("/[a-z]+.[a-z]+", [](const Request &req, Response &res) { std::cout << "The file is requested try to get it in static\n"; //FIXME debug // Other unrecognized commands to root std::cout<<"Path: "<<req.path<<std::endl; //FIXME debug std::cout<<"Method: "<<req.method<<std::endl; //FIXME debug std::cout<<"Target: "<<req.target<<std::endl; //FIXME debug // try get static if (!get_static(req.target, res, "application/octet")) { std::cout << "file not exists in static\n"; //FIXME debug res.set_content("file not exists in static!\n", "text/plain"); //Текст для возврата и формат } }); //////////////////////////////////////////////////////// svr.Get("/[a-z]+", [](const Request &req, Response &res) { std::cout << "unrecognized command to root try to get static\n"; //FIXME debug // Other unrecognized commands to root for (auto p : req.params) { std::cout<<p.first<<":"<<p.second<<std::endl; } std::cout<<"Path: "<<req.path<<std::endl; //FIXME debug std::cout<<"Method: "<<req.method<<std::endl; //FIXME debug std::cout<<"Target: "<<req.target<<std::endl; //FIXME debug // try get static if (!get_static(req.target, res, "application/octet")) { std::cout << "unrecognized command to root page\n"; //FIXME debug res.set_content("Unrecognized command to root Page!", "text/plain"); //Текст для возврата и формат } }); } void run_server(Server& svr) { init_server_targets(svr); printf("Starting on %s: %d\n", ourip, port); svr.listen(ourip, port); // listen port 8080 } bool get_static(const std::string& target, httplib::Response& res, std::string mime) { bool result = true; res.set_file_content("static" + target, mime); std::cout<<"."<<target<<", "<<mime<<std::endl; return result; }