-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cpp
More file actions
55 lines (40 loc) · 1.31 KB
/
Copy pathApp.cpp
File metadata and controls
55 lines (40 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "App.h"
#include <filesystem>
#include <fstream>
#include <iostream>
App::App()
: m_router()
, m_logger()
{
register_routes();
set_logger(m_logger);
}
void App::register_routes()
{
using WebCore::HttpMethod;
using WebCore::HttpStatus;
using WebCore::Utils::Logger;
m_router.set_logger(m_logger);
m_router.add_route(HttpMethod::Get, "/", [this](auto& req, auto& res) {
m_logger.info("Made a GET request to the '/' endpoint.");
// Set the current path to the root path of WebCore
std::filesystem::current_path(PROJECT_ROOT_DIR);
std::string path = "./example/index.html";
res.add_headers({
{ "Content-Type", "text/html" },
});
std::ifstream file_stream { path };
std::string body { std::istreambuf_iterator<char> { file_stream }, std::istreambuf_iterator<char> {} };
res.write(body);
});
m_router.add_route(HttpMethod::Get, "/users/:id", [this](auto& req, auto& res) {
m_logger.warn("Query paramters aren't properly implemented yet.");
auto params = req.get_params();
std::string id = params["id"];
res.write("User ID: " + id);
});
}
void App::handle_request(const WebCore::Request& req, WebCore::Response& res)
{
m_router.handle_request(req, res);
}