-
Notifications
You must be signed in to change notification settings - Fork 1
/
filewebserver.cpp
101 lines (85 loc) · 2.75 KB
/
filewebserver.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "httpconnection.h"
#include "filewebserver.h"
#include "generator.h"
#include <boost/beast/version.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
namespace
{
const size_t SUCCESS = 0;
const size_t ERROR_UNHANDLED_EXCEPTION = 2;
}
FileWebServer::FileWebServer()
: Configurable()
, mServerIP{"127.0.0.1"}
, mPort{8080}
, mPath{"d://"}
, mGenerator{nullptr}
{
}
FileWebServer::~FileWebServer()
{
}
void http_server(boost::asio::ip::tcp::acceptor &acceptor, boost::asio::ip::tcp::socket &socket, const std::string &response_html, FileSystemModel &fileSystemModel, Generator &htmlContentGenerator)
{
acceptor.async_accept(socket, [&, response_html](boost::beast::error_code ec)
{
if (!ec)
{
auto connection=std::make_shared<http_connection>(std::move(socket), response_html);
connection->setFileSystemModel(&fileSystemModel);
connection->setHTMLContentGenerator(&htmlContentGenerator);
connection->start();
}
// std::cout << "IP address: " << socket.remote_endpoint().address().to_string() << std::endl;
http_server(acceptor, socket, response_html, fileSystemModel, htmlContentGenerator);
});
}
int FileWebServer::run()
{
try
{
try
{
auto const address = boost::asio::ip::make_address(mServerIP);
boost::asio::io_context ioc{1};
boost::asio::ip::tcp::acceptor acceptor{ioc, {address,
static_cast<unsigned short>(mPort)}};
boost::asio::ip::tcp::socket socket{ioc};
std::cout << "Server started on " << mServerIP << ":" << mPort <<std::endl;
std::cout << "Working directory is a " << mPath <<std::endl;
if(mGenerator)
{
http_server(acceptor, socket, mGenerator->generate(mPath), *mFileSysemModel, *mGenerator);
}
ioc.run();
}
catch (std::exception const &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
}
catch(std::exception& e)
{
std::cerr << "Unhandled Exception reached the top of main: "
<< e.what() << ", application will now exit" << std::endl;
return ERROR_UNHANDLED_EXCEPTION;
}
return SUCCESS;
}
void FileWebServer::setConfiguration(Configuration config)
{
mPath = config.dir();
mPort = static_cast<unsigned>(config.port());
}
void FileWebServer::setGenerator(Generator *generator)
{
mGenerator = generator;
}
void FileWebServer::setFileSysemModel(FileSystemModel *fileSysemModel)
{
mFileSysemModel = fileSysemModel;
}