diff --git a/drogon_ctl/create_view.cc b/drogon_ctl/create_view.cc index d32880e1..5d7eba9a 100755 --- a/drogon_ctl/create_view.cc +++ b/drogon_ctl/create_view.cc @@ -103,9 +103,20 @@ static void parseLine(std::ofstream &oSrcFile,std::string& line,const std::strin void create_view::handleCommand(std::vector ¶meters) { - for(auto file:parameters) + for(auto iter=parameters.begin();iter!=parameters.end();iter++) { - if(file[0]=='-') + auto file=*iter; + if(file=="-o"||file=="--output") + { + iter=parameters.erase(iter); + if(iter!=parameters.end()) + { + _outputPath=*iter; + iter=parameters.erase(iter); + } + break; + } + else if(file[0]=='-') { std::cout<\n"; file <<"#include \n"; file <<"#include \n"; + file <<"#include \n"; file <<"using namespace std;\n"; +// file <<"void __attribute__((constructor)) startup()\n"; +// file <<"{std::cout<<\"dynamic lib start to load!\"< &cspFileNames); int createViewFile(const std::string &script_filename); void newViewHeaderFile(std::ofstream &file,const std::string &className); diff --git a/examples/static_link_example/api_v1_ApiTest.cc b/examples/static_link_example/api_v1_ApiTest.cc index 9226eb5f..58e1d264 100755 --- a/examples/static_link_example/api_v1_ApiTest.cc +++ b/examples/static_link_example/api_v1_ApiTest.cc @@ -9,7 +9,7 @@ void ApiTest::get(const HttpRequest& req,const std::function&callback,double p1,int p2) const diff --git a/examples/static_link_example/main.cc b/examples/static_link_example/main.cc index 463482dc..cdde48f9 100755 --- a/examples/static_link_example/main.cc +++ b/examples/static_link_example/main.cc @@ -113,7 +113,7 @@ int main() drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4/{4}/{3}/{1}",func); //start app framework - //drogon::HttpAppFramework::instance().enableDynamicSharedLibLoading(); + drogon::HttpAppFramework::instance().enableDynamicSharedLibLoading({"/tmp/views"}); drogon::HttpAppFramework::instance().run(); } diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 5cd63fc0..0ed1e1fe 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -79,6 +79,6 @@ namespace drogon virtual const std::string & getDocumentRoot() const =0; virtual void setDocumentRoot(const std::string &rootPath)=0; virtual void setFileTypes(const std::vector &types)=0; - virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views")=0; + virtual void enableDynamicSharedLibLoading(const std::vector &libPaths)=0; }; } diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index f4a9d0c2..53d3ee17 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -52,7 +52,7 @@ namespace drogon virtual const std::string & getDocumentRoot() const override {return _rootPath;} virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;} virtual void setFileTypes(const std::vector &types) override; - virtual void enableDynamicSharedLibLoading(const std::string &viewPth="views") override; + virtual void enableDynamicSharedLibLoading(const std::vector &libPaths) override; ~HttpAppFrameworkImpl(){} private: std::vector> _listeners; @@ -111,7 +111,7 @@ namespace drogon size_t _threadNum=1; - std::string _viewFilePath; + std::vector _libFilePaths; std::unique_ptr_sharedLibManagerPtr; @@ -121,13 +121,21 @@ namespace drogon using namespace drogon; using namespace std::placeholders; -void HttpAppFrameworkImpl::enableDynamicSharedLibLoading(const std::string &viewPath) +void HttpAppFrameworkImpl::enableDynamicSharedLibLoading(const std::vector &libPaths) { assert(!_running); - if(_viewFilePath.empty()) + if(_libFilePaths.empty()) { - _viewFilePath=_rootPath+"/"+viewPath; - _sharedLibManagerPtr=std::unique_ptr(new SharedLibManager(&_loop,_viewFilePath)); + for(auto libpath:libPaths) + { + if(libpath[0]!='/') + { + _libFilePaths.push_back(_rootPath+"/"+libpath); + } else + _libFilePaths.push_back(libpath); + } + + _sharedLibManagerPtr=std::unique_ptr(new SharedLibManager(&_loop,_libFilePaths)); } } void HttpAppFrameworkImpl::setFileTypes(const std::vector &types) diff --git a/lib/src/SharedLibManager.cc b/lib/src/SharedLibManager.cc index 14d20957..d714b36a 100755 --- a/lib/src/SharedLibManager.cc +++ b/lib/src/SharedLibManager.cc @@ -14,14 +14,14 @@ #include "SharedLibManager.h" #include +#include #include #include #include #include #include - -void *dlopen(const char *filename, int flag); -static void forEachFileIn(const std::string &path,const std::function &cb) +#include +static void forEachFileIn(const std::string &path,const std::function &cb) { DIR* dp; struct dirent* dirp; @@ -55,16 +55,16 @@ static void forEachFileIn(const std::string &path,const std::function & libPaths): _loop(loop), -_viewPath(viewPath) +_libPaths(libPaths) { _loop->runEvery(5.0,[=](){ managerLibs(); @@ -72,30 +72,89 @@ _viewPath(viewPath) } void SharedLibManager::managerLibs() { - LOG_DEBUG<<"manager .so libs in path "<<_viewPath; - forEachFileIn(_viewPath,[=](const std::string &filename){ - LOG_DEBUG<_dlMap[filename].mTime.tv_sec) + { + LOG_DEBUG<<"new csp file:"<runAfter(3.5,[=](){ + LOG_DEBUG<<"remove file "< #include #include +#include namespace drogon{ class SharedLibManager:public trantor::NonCopyable { public: - SharedLibManager(trantor::EventLoop *loop,const std::string viewPath); + SharedLibManager(trantor::EventLoop *loop,const std::vector & libPaths); ~SharedLibManager(){} private: void managerLibs(); trantor::EventLoop *_loop; - std::string _viewPath; - std::unordered_map _dlMap; + std::vector _libPaths; + struct DLStat{ + void * handle; + struct timespec mTime; + }; + std::unordered_map _dlMap; + void* loadLibs(const std::string &sourceFile); }; } \ No newline at end of file