From 0cd3f3868e2672992fc32b043d598290562bdcfc Mon Sep 17 00:00:00 2001 From: electron128 Date: Sun, 22 Mar 2015 12:29:30 +0000 Subject: [PATCH] added command line parameters for webui port and base directory. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8069 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libresapi/src/api/ApiServerMHD.cpp | 12 ++++++--- libretroshare/src/util/argstream.h | 2 +- retroshare-nogui/src/retroshare.cc | 43 +++++++++++++++++------------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 6b711eece..a572dec90 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -80,7 +80,7 @@ public: if(strcmp(method, "POST") == 0) { mState = WAITING_DATA; - // first time there is no data, do nohtin and return + // first time there is no data, do nothing and return return MHD_YES; } } @@ -223,7 +223,11 @@ ApiServerMHD::ApiServerMHD(std::string root_dir, uint16_t port): mRootDir(root_dir), mPort(port), mDaemon(0) { - + // make sure the docroot dir ends with a slash + if(mRootDir.empty()) + mRootDir = "./"; + else if (mRootDir[mRootDir.size()-1] != '/' && mRootDir[mRootDir.size()-1] != '\\') + mRootDir += "/"; } ApiServerMHD::~ApiServerMHD() @@ -245,7 +249,7 @@ bool ApiServerMHD::start() MHD_OPTION_END); if(mDaemon) { - std::cerr << "ApiServerMHD::start() SUCCESS. Started server on port " << mPort << ". mRootDir=\"" << mRootDir << "\"" << std::endl; + std::cerr << "ApiServerMHD::start() SUCCESS. Started server on port " << mPort << ". Serving files from \"" << mRootDir << "\" at /" << std::endl; return true; } else @@ -348,7 +352,7 @@ int ApiServerMHD::accessHandlerCallback(MHD_Connection *connection, } // else server static files - std::string filename = std::string(".") + url; + std::string filename = mRootDir + url; // important: binary open mode, // else libmicrohttpd will replace crlf with lf and add garbage at the end of the file FILE* fd = fopen(filename.c_str(), "rb"); diff --git a/libretroshare/src/util/argstream.h b/libretroshare/src/util/argstream.h index 77f4012cf..c239398d7 100644 --- a/libretroshare/src/util/argstream.h +++ b/libretroshare/src/util/argstream.h @@ -116,7 +116,7 @@ namespace const char* desc="", bool mandatory = true) { - return ValueHolder(l,b,desc,mandatory); + return ValueHolder(l,b,valueName,desc,mandatory); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Interface of OptionHolder diff --git a/retroshare-nogui/src/retroshare.cc b/retroshare-nogui/src/retroshare.cc index 6abb31276..35c59cf7b 100644 --- a/retroshare-nogui/src/retroshare.cc +++ b/retroshare-nogui/src/retroshare.cc @@ -74,29 +74,36 @@ int main(int argc, char **argv) { #ifdef ENABLE_WEBUI - std::string docroot; - bool is_portable = false; -#ifdef WINDOWS_SYS - // test for portable version - if (GetFileAttributes(L"portable") != (DWORD) -1) { - is_portable = true; - } -#endif + std::string docroot = "./"; + uint16_t httpPort = 0; - resource_api::ApiServerMHD httpd(docroot, 9090); + argstream args(argc, argv); + args >> parameter("webinterface", httpPort, "port", "Enable webinterface on the specified port", false); + args >> parameter("docroot", docroot, "path", "Serve static files from this path.", false); + args >> help(); - resource_api::RsControlModule ctrl_mod(argc, argv, httpd.getApiServer().getStateTokenServer(), &httpd.getApiServer()); - httpd.getApiServer().addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); - - httpd.start(); - - while(ctrl_mod.processShouldExit() == false) + if (args.helpRequested()) { - usleep(20*1000); + std::cerr << args.usage() << std::endl; } - httpd.stop(); - return 1; + if(httpPort != 0) + { + resource_api::ApiServerMHD httpd(docroot, httpPort); + + resource_api::RsControlModule ctrl_mod(argc, argv, httpd.getApiServer().getStateTokenServer(), &httpd.getApiServer()); + httpd.getApiServer().addResourceHandler("control", dynamic_cast(&ctrl_mod), &resource_api::RsControlModule::handleRequest); + + httpd.start(); + + while(ctrl_mod.processShouldExit() == false) + { + usleep(20*1000); + } + httpd.stop(); + + return 1; + } #endif /* Retroshare startup is configured using an RsInit object.