move handler to special function
This commit is contained in:
parent
4f37cf043f
commit
965123bafe
|
@ -35,6 +35,8 @@ add_executable(hello_async
|
|||
src/content_type.h
|
||||
src/handlers/GetMapByIdHandler.cpp
|
||||
src/handlers/GetMapByIdHandler.h
|
||||
src/handlers/GetStaticAssetHandler.cpp
|
||||
src/handlers/GetStaticAssetHandler.h
|
||||
src/routes.h)
|
||||
target_include_directories(hello_async PUBLIC ${Boost_INCLUDE_DIR})
|
||||
target_link_libraries(hello_async PRIVATE Threads::Threads Boost::filesystem Boost::json)
|
||||
|
|
6
main.cpp
6
main.cpp
|
@ -13,6 +13,7 @@
|
|||
#include "src/routes.h"
|
||||
#include "src/handlers/GetMapsHandler.h"
|
||||
#include "src/handlers/GetMapByIdHandler.h"
|
||||
#include "src/handlers/GetStaticAssetHandler.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -34,6 +35,11 @@ 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(),
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>My title</title>
|
||||
</head>
|
||||
<body>
|
||||
123412341324
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,27 @@
|
|||
#include "GetMapsHandler.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
using namespace std;
|
||||
|
||||
namespace http_server
|
||||
{
|
||||
StringResponse 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);
|
||||
res.set(http::field::content_type, content_type::k_JSON);
|
||||
res.body() = "INVALID";
|
||||
res.content_length(res.body().size());
|
||||
res.keep_alive(true);
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#include "../content_type.h"
|
||||
|
||||
namespace http_server
|
||||
{
|
||||
namespace net = boost::asio;
|
||||
using namespace std::literals;
|
||||
namespace sys = boost::system;
|
||||
namespace json = boost::json;
|
||||
namespace http = boost::beast::http;
|
||||
using namespace std;
|
||||
using net::ip::tcp;
|
||||
|
||||
using StringRequest = http::request<http::string_body>;
|
||||
using StringResponse = http::response<http::string_body>;
|
||||
|
||||
StringResponse GetStaticAssetHandler(const StringRequest&);
|
||||
}
|
Loading…
Reference in New Issue