@@ -12,7 +12,8 @@ namespace odr {
1212
1313class HttpServer ::Impl {
1414public:
15- explicit Impl (HttpServer::Config config) : m_config{std::move (config)} {
15+ Impl (HttpServer::Config config, std::shared_ptr<Logger> logger)
16+ : m_config{std::move (config)}, m_logger{std::move (logger)} {
1617 m_server.Get (" /" ,
1718 [](const httplib::Request & /* req*/ , httplib::Response &res) {
1819 res.set_content (" Hello World!" , " text/plain" );
@@ -30,13 +31,16 @@ class HttpServer::Impl {
3031
3132 const HttpServer::Config &config () const { return m_config; }
3233
34+ const std::shared_ptr<Logger> &logger () const { return m_logger; }
35+
3336 void serve_file (const httplib::Request &req, httplib::Response &res) {
3437 std::string id = req.matches [1 ].str ();
3538 std::string path = req.matches .size () > 1 ? req.matches [2 ].str () : " " ;
3639
3740 std::unique_lock lock{m_mutex};
3841 auto it = m_content.find (id);
3942 if (it == m_content.end ()) {
43+ ODR_ERROR (*m_logger, " Content not found for ID: " << id);
4044 res.status = 404 ;
4145 return ;
4246 }
@@ -46,13 +50,16 @@ class HttpServer::Impl {
4650 serve_file (res, content.service , path);
4751 }
4852
49- static void serve_file (httplib::Response &res, const HtmlService &service,
50- const std::string &path) {
53+ void serve_file (httplib::Response &res, const HtmlService &service,
54+ const std::string &path) {
5155 if (!service.exists (path)) {
56+ ODR_ERROR (*m_logger, " File not found: " << path);
5257 res.status = 404 ;
5358 return ;
5459 }
5560
61+ ODR_VERBOSE (*m_logger, " Serving file: " << path);
62+
5663 httplib::ContentProviderWithoutLength content_provider =
5764 [service = service, path = path](std::size_t offset,
5865 httplib::DataSink &sink) -> bool {
@@ -67,6 +74,8 @@ class HttpServer::Impl {
6774 }
6875
6976 void connect_service (HtmlService service, const std::string &prefix) {
77+ ODR_VERBOSE (*m_logger, " Connecting service with prefix: " << prefix);
78+
7079 std::unique_lock lock{m_mutex};
7180
7281 if (m_content.contains (prefix)) {
@@ -77,10 +86,14 @@ class HttpServer::Impl {
7786 }
7887
7988 void listen (const std::string &host, std::uint32_t port) {
89+ ODR_VERBOSE (*m_logger, " Listening on " << host << " :" << port);
90+
8091 m_server.listen (host, static_cast <int >(port));
8192 }
8293
8394 void clear () {
95+ ODR_VERBOSE (*m_logger, " Clearing HTTP server cache..." );
96+
8497 std::unique_lock lock{m_mutex};
8598
8699 m_content.clear ();
@@ -92,6 +105,8 @@ class HttpServer::Impl {
92105 }
93106
94107 void stop () {
108+ ODR_VERBOSE (*m_logger, " Stopping HTTP server..." );
109+
95110 clear ();
96111
97112 m_server.stop ();
@@ -100,6 +115,8 @@ class HttpServer::Impl {
100115private:
101116 HttpServer::Config m_config;
102117
118+ std::shared_ptr<Logger> m_logger;
119+
103120 httplib::Server m_server;
104121
105122 struct Content {
@@ -111,40 +128,41 @@ class HttpServer::Impl {
111128 std::unordered_map<std::string, Content> m_content;
112129};
113130
114- HttpServer::HttpServer (const Config &config)
115- : m_impl{std::make_unique<Impl>(config)} {}
131+ HttpServer::HttpServer (const Config &config, std::shared_ptr<Logger> logger )
132+ : m_impl{std::make_unique<Impl>(config, std::move (logger) )} {}
116133
117134const HttpServer::Config &HttpServer::config () const {
118135 return m_impl->config ();
119136}
120137
121138void HttpServer::connect_service (HtmlService service,
122139 const std::string &prefix) {
123- m_impl->connect_service (std::move (service), prefix);
124- }
125-
126- HtmlViews HttpServer::serve_file (DecodedFile file, const std::string &prefix,
127- const HtmlConfig &config) {
128140 static std::regex prefix_regex (prefix_pattern);
129141 if (!std::regex_match (prefix, prefix_regex)) {
130142 throw InvalidPrefix (prefix);
131143 }
132144
133- if (config.relative_resource_paths ) {
145+ if (service. config () .relative_resource_paths ) {
134146 throw UnsupportedOption (
135147 " relative_resource_paths cannot be enabled in server mode" );
136148 }
137- if (!config.embed_shipped_resources ) {
149+ if (!service. config () .embed_shipped_resources ) {
138150 throw UnsupportedOption (
139151 " embed_shipped_resources must be enabled in server mode" );
140152 }
141153
154+ m_impl->connect_service (std::move (service), prefix);
155+ }
156+
157+ HtmlViews HttpServer::serve_file (DecodedFile file, const std::string &prefix,
158+ const HtmlConfig &config) {
142159 std::string cache_path = m_impl->config ().cache_path + " /" + prefix;
143160 std::filesystem::create_directories (cache_path);
144161
145- HtmlService service = html::translate (file, cache_path, config);
162+ HtmlService service =
163+ html::translate (file, cache_path, config, m_impl->logger ());
146164
147- m_impl-> connect_service (service, prefix);
165+ connect_service (service, prefix);
148166
149167 return service.list_views ();
150168}
0 commit comments