Merge pull request #221 from kiwix/kiwix-serve

kiwix-serve integration in kiwix-lib
This commit is contained in:
Matthieu Gautier 2019-06-11 10:54:35 +02:00 committed by GitHub
commit 7c1d051305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 1 deletions

33
include/kiwixserve.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef KIWIXLIB_KIWIX_SERVE_H_
#define KIWIXLIB_KIWIX_SERVE_H_
#ifdef _WIN32
// winsock2.h need to be included before windows.h (included by curl.h)
# include <winsock2.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#include <memory>
#include "tools/pathTools.h"
class Subprocess;
namespace kiwix {
class KiwixServe
{
public:
KiwixServe();
~KiwixServe();
void run();
void shutDown();
private:
std::unique_ptr<Subprocess> mp_kiwixServe;
int m_port;
};
}; //end namespace kiwix
#endif // KIWIXLIB_KIWIX_SERVE_H_

View File

@ -9,7 +9,8 @@ headers = [
'downloader.h',
'reader.h',
'entry.h',
'searcher.h'
'searcher.h',
'kiwixserve.h'
]
install_headers(headers, subdir:'kiwix')

57
src/kiwixserve.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "kiwixserve.h"
#include "subprocess.h"
#ifdef _WIN32
# define KIWIXSERVE_CMD "kiwix-serve.exe"
#else
# define KIWIXSERVE_CMD "kiwix-serve"
#endif
namespace kiwix {
KiwixServe::KiwixServe() : m_port(8181)
{
}
KiwixServe::~KiwixServe()
{
shutDown();
}
void KiwixServe::run()
{
#ifdef _WIN32
int pid = GetCurrentProcessId();
#else
pid_t pid = getpid();
#endif
std::vector<const char*> callCmd;
std::string kiwixServeCmd = appendToDirectory(
removeLastPathElement(getExecutablePath(), true, true),
KIWIXSERVE_CMD);
if (fileExists(kiwixServeCmd)) {
// A local kiwix-serve exe exists (packaged with kiwix-desktop), use it.
callCmd.push_back(kiwixServeCmd.c_str());
} else {
// Try to use a potential installed kiwix-serve.
callCmd.push_back(KIWIXSERVE_CMD);
}
std::string libraryPath = getDataDirectory() + "/library.xml";
std::string attachProcessOpt = "-a" + to_string(pid);
std::string portOpt = "-p" + to_string(m_port);
callCmd.push_back(attachProcessOpt.c_str());
callCmd.push_back(portOpt.c_str());
callCmd.push_back("-l");
callCmd.push_back(libraryPath.c_str());
mp_kiwixServe = Subprocess::run(callCmd);
}
void KiwixServe::shutDown()
{
if (mp_kiwixServe)
mp_kiwixServe->kill();
}
}

View File

@ -17,6 +17,7 @@ kiwix_sources = [
'tools/stringTools.cpp',
'tools/networkTools.cpp',
'tools/otherTools.cpp',
'kiwixserve.cpp',
]
kiwix_sources += lib_resources