add file support
This commit is contained in:
parent
540a635069
commit
6c0b34a199
23
main.cpp
23
main.cpp
|
@ -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)));
|
||||
});
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue