add file support
This commit is contained in:
parent
2b61359e57
commit
6ddf982780
30
main.cpp
30
main.cpp
|
@ -30,16 +30,12 @@ using net::ip::tcp;
|
|||
using StringRequest = http::request<http::string_body>;
|
||||
// Ответ, тело которого представлено в виде строки
|
||||
using StringResponse = http::response<http::string_body>;
|
||||
using FileResponse = http::response<http::file_body>;
|
||||
|
||||
StringResponse HandleRequest(StringRequest&& req)
|
||||
{
|
||||
const auto route = req.target();
|
||||
|
||||
if (req.target() == "/"sv || find(route.begin(), route.end(), '.') != route.end())
|
||||
{
|
||||
return GetStaticAssetHandler(req);
|
||||
}
|
||||
|
||||
if (equal(k_MapsRoute.begin(),
|
||||
k_MapsRoute.end(),
|
||||
route.begin(),
|
||||
|
@ -70,6 +66,25 @@ StringResponse HandleRequest(StringRequest&& req)
|
|||
}
|
||||
}
|
||||
|
||||
FileResponse HandleRequestFile(StringRequest&& req)
|
||||
{
|
||||
const auto route = req.target();
|
||||
|
||||
if (req.target() == "/"sv || find(route.begin(), route.end(), '.') != route.end())
|
||||
{
|
||||
return GetStaticAssetHandler(req);
|
||||
}
|
||||
else
|
||||
{
|
||||
FileResponse res(http::status::not_found, 1);
|
||||
res.set(http::field::content_type, content_type::k_JSON);
|
||||
res.content_length(0);
|
||||
res.keep_alive(true);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
// Запускает функцию fn на n потоках, включая текущий
|
||||
template<typename Fn>
|
||||
void RunWorkers(unsigned n, const Fn& fn)
|
||||
|
@ -110,6 +125,11 @@ int main()
|
|||
const auto address = net::ip::make_address("0.0.0.0");
|
||||
constexpr net::ip::port_type port = 8080;
|
||||
http_server::ServeHttp(ioc, {address, port}, [](auto&& req, auto&& sender)
|
||||
{
|
||||
sender(HandleRequestFile(std::forward<decltype(req)>(req)));
|
||||
});
|
||||
|
||||
http_server::ServeHttp(ioc, {address, port + 1}, [](auto&& req, auto&& sender)
|
||||
{
|
||||
sender(HandleRequest(std::forward<decltype(req)>(req)));
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "GetMapsHandler.h"
|
||||
#include "GetStaticAssetHandler.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
@ -9,17 +9,16 @@ using namespace std;
|
|||
|
||||
namespace http_server
|
||||
{
|
||||
StringResponse GetStaticAssetHandler(const StringRequest& req)
|
||||
FileResponse GetStaticAssetHandler(const StringRequest& req)
|
||||
{
|
||||
string rel_path_str {req.target().begin(), req.target().end()};
|
||||
fs::path base_path("./public/"s);
|
||||
fs::path rel_path(rel_path_str);
|
||||
fs::path abs_path = fs::weakly_canonical(base_path / rel_path);
|
||||
|
||||
StringResponse res(http::status::ok, 1);
|
||||
FileResponse res(http::status::ok, 1);
|
||||
res.set(http::field::content_type, content_type::k_JSON);
|
||||
res.body() = "INVALID";
|
||||
res.content_length(res.body().size());
|
||||
res.content_length(0);
|
||||
res.keep_alive(true);
|
||||
|
||||
return res;
|
||||
|
|
|
@ -20,6 +20,7 @@ using net::ip::tcp;
|
|||
|
||||
using StringRequest = http::request<http::string_body>;
|
||||
using StringResponse = http::response<http::string_body>;
|
||||
using FileResponse = http::response<http::file_body>;
|
||||
|
||||
StringResponse GetStaticAssetHandler(const StringRequest&);
|
||||
FileResponse GetStaticAssetHandler(const StringRequest&);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue