add file support

This commit is contained in:
Антон 2024-03-27 08:50:50 +03:00
parent 540a635069
commit 6c0b34a199
2 changed files with 23 additions and 2 deletions

View File

@ -115,7 +115,28 @@ int main()
const auto route = req.target();
if (req.target() == "/"sv || find(route.begin(), route.end(), '.') != route.end())
sender(HandleRequestFile(std::forward<decltype(req)>(req)));
{
auto res = HandleRequestFile(std::forward<decltype(req)>(req));
if (res.result() == http::status::not_found)
{
StringResponse errres(http::status::bad_request, 1);
errres.set(http::field::content_type, content_type::k_JSON);
string body = "{\n"
" \"code\": \"badRequest\",\n"
" \"message\": \"Bad request\"\n"
"} "s;
errres.body() = body;
errres.content_length(body.size());
errres.keep_alive(true);
sender(errres);
}
else
{
sender(res);
}
}
else
sender(HandleRequest(std::forward<decltype(req)>(req)));
});

View File

@ -25,7 +25,7 @@ FileResponse GetStaticAssetHandler(const StringRequest& req)
if (sys::error_code ec; file.open(abs_path.string().c_str(), beast::file_mode::read, ec), ec)
{
res.result(http::status::not_found);
res.set(http::field::content_type, content_type::k_TextPlain);
res.content_length(0);
}
else
{