From b7b385d87b201220df3dbb489070145da6c752ec Mon Sep 17 00:00:00 2001 From: Manan Jethwani <2019282@iiitdmj.ac.in> Date: Tue, 12 Oct 2021 19:44:05 +0530 Subject: [PATCH] added custom index template --- README.md | 47 +++++++++++++++++++++++++++++++++++ include/server.h | 2 ++ src/server.cpp | 3 ++- src/server/internalServer.cpp | 6 +++-- src/server/internalServer.h | 4 ++- test/server.cpp | 42 +++++++++++++++++++++++++++---- 6 files changed, 95 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 484541493..e25a77678 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,53 @@ cp ninja ../bin cd .. ``` +Custom Index Page +----------------- + +to use custom welcome page mention `customIndexPage` argument in `kiwix::internalServer()` or use `kiwix::server->setCustomIndexTemplate()`. +(note - while using custom html file please mention all external links as absolute path.) + +to create a HTML template with custom JS you need to have a look at various OPDS based endpoints as mentioned [here](https://wiki.kiwix.org/wiki/OPDS) to load books. + +To use JS provided by kiwix-serve you can use the following template to start with -> + +``` + + + + + + <-- Custom Tittle --> + + + + + + + + + +``` + +- To get books listed using `index.js` add - `
` under body tag. +- To get number of books listed add - `

` under body tag. +- To add language select box add - `` under body tag. +- To add language select box add - `` under body tag. +- To add search box for books use following form - + ``` +
+ + +
+ ``` + + If you compile manually Libmicrohttpd, you might need to compile it without GNU TLS, a bug here will empeach further compilation otherwise. diff --git a/include/server.h b/include/server.h index b38ad8c04..b1c3a4232 100644 --- a/include/server.h +++ b/include/server.h @@ -55,6 +55,7 @@ namespace kiwix void setPort(int port) { m_port = port; } void setNbThreads(int threads) { m_nbThreads = threads; } void setVerbose(bool verbose) { m_verbose = verbose; } + void setIndexTemplateString(const std::string& indexTemplateString) { m_indexTemplateString = indexTemplateString; } void setTaskbar(bool withTaskbar, bool withLibraryButton) { m_withTaskbar = withTaskbar; m_withLibraryButton = withLibraryButton; } void setBlockExternalLinks(bool blockExternalLinks) @@ -65,6 +66,7 @@ namespace kiwix NameMapper* mp_nameMapper; std::string m_root = ""; std::string m_addr = ""; + std::string m_indexTemplateString = ""; int m_port = 80; int m_nbThreads = 1; bool m_verbose = false; diff --git a/src/server.cpp b/src/server.cpp index e82bf3cee..255f88883 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -48,7 +48,8 @@ bool Server::start() { m_verbose, m_withTaskbar, m_withLibraryButton, - m_blockExternalLinks)); + m_blockExternalLinks, + m_indexTemplateString)); return mp_server->start(); } diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 0e474ad86..6ff949380 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -128,7 +128,8 @@ InternalServer::InternalServer(Library* library, bool verbose, bool withTaskbar, bool withLibraryButton, - bool blockExternalLinks) : + bool blockExternalLinks, + std::string indexTemplateString) : m_addr(addr), m_port(port), m_root(normalizeRootUrl(root)), @@ -137,6 +138,7 @@ InternalServer::InternalServer(Library* library, m_withTaskbar(withTaskbar), m_withLibraryButton(withLibraryButton), m_blockExternalLinks(blockExternalLinks), + m_indexTemplateString(indexTemplateString.empty() ? RESOURCE::templates::index_html : indexTemplateString), mp_daemon(nullptr), mp_library(library), mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper) @@ -336,7 +338,7 @@ InternalServer::get_matching_if_none_match_etag(const RequestContext& r) const std::unique_ptr InternalServer::build_homepage(const RequestContext& request) { - return ContentResponse::build(*this, RESOURCE::templates::index_html, get_default_data(), "text/html; charset=utf-8", true); + return ContentResponse::build(*this, m_indexTemplateString, get_default_data(), "text/html; charset=utf-8", true); } /** diff --git a/src/server/internalServer.h b/src/server/internalServer.h index cfaceb9ec..898ff0390 100644 --- a/src/server/internalServer.h +++ b/src/server/internalServer.h @@ -54,7 +54,8 @@ class InternalServer { bool verbose, bool withTaskbar, bool withLibraryButton, - bool blockExternalLinks); + bool blockExternalLinks, + std::string indexTemplateString); virtual ~InternalServer() = default; MHD_Result handlerCallback(struct MHD_Connection* connection, @@ -103,6 +104,7 @@ class InternalServer { bool m_withTaskbar; bool m_withLibraryButton; bool m_blockExternalLinks; + std::string m_indexTemplateString; struct MHD_Daemon* mp_daemon; Library* mp_library; diff --git a/test/server.cpp b/test/server.cpp index 85f553ce0..afffee224 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -54,7 +54,7 @@ public: // types public: // functions ZimFileServer(int serverPort, std::string libraryFilePath); - ZimFileServer(int serverPort, const FilePathCollection& zimpaths); + ZimFileServer(int serverPort, const FilePathCollection& zimpaths, std::string indexTemplateString = ""); ~ZimFileServer(); Response GET(const char* path, const Headers& headers = Headers()) @@ -68,7 +68,7 @@ public: // functions } private: - void run(int serverPort); + void run(int serverPort, std::string indexTemplateString = ""); private: // data kiwix::Library library; @@ -88,7 +88,7 @@ ZimFileServer::ZimFileServer(int serverPort, std::string libraryFilePath) run(serverPort); } -ZimFileServer::ZimFileServer(int serverPort, const FilePathCollection& zimpaths) +ZimFileServer::ZimFileServer(int serverPort, const FilePathCollection& zimpaths, std::string indexTemplateString) : manager(&this->library) { for ( const auto& zimpath : zimpaths ) { @@ -96,10 +96,10 @@ ZimFileServer::ZimFileServer(int serverPort, const FilePathCollection& zimpaths) throw std::runtime_error("Unable to add the ZIM file '" + zimpath + "'"); } - run(serverPort); + run(serverPort, indexTemplateString); } -void ZimFileServer::run(int serverPort) +void ZimFileServer::run(int serverPort, std::string indexTemplateString) { const std::string address = "127.0.0.1"; nameMapper.reset(new kiwix::HumanReadableNameMapper(library, false)); @@ -108,6 +108,9 @@ void ZimFileServer::run(int serverPort) server->setPort(serverPort); server->setNbThreads(2); server->setVerbose(false); + if (!indexTemplateString.empty()) { + server->setIndexTemplateString(indexTemplateString); + } if ( !server->start() ) throw std::runtime_error("ZimFileServer failed to start"); @@ -211,6 +214,35 @@ ResourceCollection all200Resources() return concat(resources200Compressible, resources200Uncompressible); } +TEST(indexTemplateStringTest, emptyIndexTemplate) { + const int PORT = 8001; + const ZimFileServer::FilePathCollection ZIMFILES { + "./test/zimfile.zim", + "./test/corner_cases.zim" + }; + + ZimFileServer zfs(PORT, ZIMFILES, ""); + EXPECT_EQ(200, zfs.GET("/")->status); +} + +TEST(indexTemplateStringTest, indexTemplateCheck) { + const int PORT = 8001; + const ZimFileServer::FilePathCollection ZIMFILES { + "./test/zimfile.zim", + "./test/corner_cases.zim" + }; + + ZimFileServer zfs(PORT, ZIMFILES, "" + "Welcome to kiwix library" + "" + ""); + EXPECT_EQ("" + "Welcome to kiwix library" + "" + "" + "", zfs.GET("/")->body); +} + TEST_F(ServerTest, 200) { for ( const Resource& res : all200Resources() )