Make the server take a pointer to the library instead of a reference.

This commit is contained in:
Matthieu Gautier 2019-08-10 20:29:19 +02:00
parent ce09375c6c
commit 635d4438e5
2 changed files with 21 additions and 21 deletions

View File

@ -36,7 +36,7 @@ namespace kiwix
* *
* @param library The library to serve. * @param library The library to serve.
*/ */
Server(Library& library, NameMapper* nameMapper=nullptr); Server(Library* library, NameMapper* nameMapper=nullptr);
virtual ~Server(); virtual ~Server();
@ -59,7 +59,7 @@ namespace kiwix
{ m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; } { m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; }
protected: protected:
Library& m_library; Library* mp_library;
NameMapper* mp_nameMapper; NameMapper* mp_nameMapper;
std::string m_root = ""; std::string m_root = "";
std::string m_addr = ""; std::string m_addr = "";

View File

@ -87,7 +87,7 @@ static int staticHandlerCallback(void* cls,
class InternalServer { class InternalServer {
public: public:
InternalServer(Library& library, InternalServer(Library* library,
NameMapper* nameMapper, NameMapper* nameMapper,
std::string addr, std::string addr,
int port, int port,
@ -132,13 +132,13 @@ class InternalServer {
bool m_withLibraryButton; bool m_withLibraryButton;
struct MHD_Daemon* mp_daemon; struct MHD_Daemon* mp_daemon;
Library& m_library; Library* mp_library;
NameMapper* mp_nameMapper; NameMapper* mp_nameMapper;
}; };
Server::Server(Library& library, NameMapper* nameMapper) : Server::Server(Library* library, NameMapper* nameMapper) :
m_library(library), mp_library(library),
mp_nameMapper(nameMapper), mp_nameMapper(nameMapper),
mp_server(nullptr) mp_server(nullptr)
{ {
@ -148,7 +148,7 @@ Server::~Server() = default;
bool Server::start() { bool Server::start() {
mp_server.reset(new InternalServer( mp_server.reset(new InternalServer(
m_library, mp_library,
mp_nameMapper, mp_nameMapper,
m_addr, m_addr,
m_port, m_port,
@ -177,7 +177,7 @@ void Server::set_root(const std::string& root)
} }
InternalServer::InternalServer(Library& library, InternalServer::InternalServer(Library* library,
NameMapper* nameMapper, NameMapper* nameMapper,
std::string addr, std::string addr,
int port, int port,
@ -194,7 +194,7 @@ InternalServer::InternalServer(Library& library,
m_withTaskbar(withTaskbar), m_withTaskbar(withTaskbar),
m_withLibraryButton(withLibraryButton), m_withLibraryButton(withLibraryButton),
mp_daemon(nullptr), mp_daemon(nullptr),
m_library(library), mp_library(library),
mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper) mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper)
{} {}
@ -366,8 +366,8 @@ Response InternalServer::build_homepage(const RequestContext& request)
auto data = get_default_data(); auto data = get_default_data();
kainjow::mustache::data books{kainjow::mustache::data::type::list}; kainjow::mustache::data books{kainjow::mustache::data::type::list};
for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) { for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) {
auto& currentBook = m_library.getBookById(bookId); auto& currentBook = mp_library->getBookById(bookId);
kainjow::mustache::data book; kainjow::mustache::data book;
book.set("name", mp_nameMapper->getNameForId(bookId)); book.set("name", mp_nameMapper->getNameForId(bookId));
@ -398,7 +398,7 @@ Response InternalServer::handle_meta(const RequestContext& request)
bookName = request.get_argument("content"); bookName = request.get_argument("content");
bookId = mp_nameMapper->getIdForName(bookName); bookId = mp_nameMapper->getIdForName(bookName);
meta_name = request.get_argument("name"); meta_name = request.get_argument("name");
reader = m_library.getReaderById(bookId); reader = mp_library->getReaderById(bookId);
} catch (const std::out_of_range& e) { } catch (const std::out_of_range& e) {
return build_404(request, bookName); return build_404(request, bookName);
} }
@ -460,7 +460,7 @@ Response InternalServer::handle_suggest(const RequestContext& request)
bookName = request.get_argument("content"); bookName = request.get_argument("content");
bookId = mp_nameMapper->getIdForName(bookName); bookId = mp_nameMapper->getIdForName(bookName);
term = request.get_argument("term"); term = request.get_argument("term");
reader = m_library.getReaderById(bookId); reader = mp_library->getReaderById(bookId);
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
return build_404(request, bookName); return build_404(request, bookName);
} }
@ -557,7 +557,7 @@ Response InternalServer::handle_search(const RequestContext& request)
std::shared_ptr<Reader> reader(nullptr); std::shared_ptr<Reader> reader(nullptr);
try { try {
reader = m_library.getReaderById(bookId); reader = mp_library->getReaderById(bookId);
} catch (const std::out_of_range&) {} } catch (const std::out_of_range&) {}
/* Try first to load directly the article */ /* Try first to load directly the article */
@ -604,8 +604,8 @@ Response InternalServer::handle_search(const RequestContext& request)
if (reader) { if (reader) {
searcher.add_reader(reader.get()); searcher.add_reader(reader.get());
} else { } else {
for (auto& bookId: m_library.filter(kiwix::Filter().local(true).valid(true))) { for (auto& bookId: mp_library->filter(kiwix::Filter().local(true).valid(true))) {
auto currentReader = m_library.getReaderById(bookId); auto currentReader = mp_library->getReaderById(bookId);
if (currentReader) { if (currentReader) {
searcher.add_reader(currentReader.get()); searcher.add_reader(currentReader.get());
} }
@ -662,7 +662,7 @@ Response InternalServer::handle_random(const RequestContext& request)
try { try {
bookName = request.get_argument("content"); bookName = request.get_argument("content");
bookId = mp_nameMapper->getIdForName(bookName); bookId = mp_nameMapper->getIdForName(bookName);
reader = m_library.getReaderById(bookId); reader = mp_library->getReaderById(bookId);
} catch (const std::out_of_range&) { } catch (const std::out_of_range&) {
return build_404(request, bookName); return build_404(request, bookName);
} }
@ -714,13 +714,13 @@ Response InternalServer::handle_catalog(const RequestContext& request)
opdsDumper.setRootLocation(m_root); opdsDumper.setRootLocation(m_root);
opdsDumper.setSearchDescriptionUrl("catalog/searchdescription.xml"); opdsDumper.setSearchDescriptionUrl("catalog/searchdescription.xml");
opdsDumper.setId(kiwix::to_string(uuid)); opdsDumper.setId(kiwix::to_string(uuid));
opdsDumper.setLibrary(&m_library); opdsDumper.setLibrary(mp_library);
response.set_mimeType("application/atom+xml;profile=opds-catalog;kind=acquisition; charset=utf-8"); response.set_mimeType("application/atom+xml;profile=opds-catalog;kind=acquisition; charset=utf-8");
std::vector<std::string> bookIdsToDump; std::vector<std::string> bookIdsToDump;
if (url == "root.xml") { if (url == "root.xml") {
opdsDumper.setTitle("All zims"); opdsDumper.setTitle("All zims");
uuid = zim::Uuid::generate(host); uuid = zim::Uuid::generate(host);
bookIdsToDump = m_library.filter(kiwix::Filter().valid(true).local(true).remote(true)); bookIdsToDump = mp_library->filter(kiwix::Filter().valid(true).local(true).remote(true));
} else if (url == "search") { } else if (url == "search") {
std::string query; std::string query;
std::string language; std::string language;
@ -748,7 +748,7 @@ Response InternalServer::handle_catalog(const RequestContext& request)
} catch (...) {} } catch (...) {}
opdsDumper.setTitle("Search result for " + query); opdsDumper.setTitle("Search result for " + query);
uuid = zim::Uuid::generate(); uuid = zim::Uuid::generate();
bookIdsToDump = m_library.filter( bookIdsToDump = mp_library->filter(
kiwix::Filter().valid(true).local(true).remote(true) kiwix::Filter().valid(true).local(true).remote(true)
.query(query) .query(query)
.lang(language) .lang(language)
@ -792,7 +792,7 @@ Response InternalServer::handle_content(const RequestContext& request)
std::shared_ptr<Reader> reader; std::shared_ptr<Reader> reader;
try { try {
bookId = mp_nameMapper->getIdForName(bookName); bookId = mp_nameMapper->getIdForName(bookName);
reader = m_library.getReaderById(bookId); reader = mp_library->getReaderById(bookId);
} catch (const std::out_of_range& e) { } catch (const std::out_of_range& e) {
return build_404(request, bookName); return build_404(request, bookName);
} }