From b539b5231c4f05ce34698ad7e9f9f7b39458694f Mon Sep 17 00:00:00 2001 From: antao Date: Mon, 15 Oct 2018 13:34:38 +0800 Subject: [PATCH] Modify drogon_ctl --- config.example.json | 5 +- drogon_ctl/CMakeLists.txt | 23 +++++++- drogon_ctl/create_project.cc | 78 +------------------------ drogon_ctl/templates/config.csp | 94 +++++++++++++++++++++++++++++++ lib/inc/drogon/HttpAppFramework.h | 2 + lib/src/ConfigLoader.cc | 2 + lib/src/HttpAppFrameworkImpl.cc | 4 +- lib/src/HttpAppFrameworkImpl.h | 4 +- 8 files changed, 130 insertions(+), 82 deletions(-) create mode 100644 drogon_ctl/templates/config.csp diff --git a/config.example.json b/config.example.json index 1514148d..af61e473 100644 --- a/config.example.json +++ b/config.example.json @@ -86,6 +86,9 @@ //use sendfile() system-call to send static file to client; "use_sendfile": true, //use_gzip:true by default,use gzip to compress the response body's content; - "use_gzip": true + "use_gzip": true, + //static_files_cache_time:5 by default,the time in which static file response is cached, + //0 means cache forever,the negative value means no cache + "static_files_cache_time":5 } } diff --git a/drogon_ctl/CMakeLists.txt b/drogon_ctl/CMakeLists.txt index 23eace7a..94bcb8a7 100755 --- a/drogon_ctl/CMakeLists.txt +++ b/drogon_ctl/CMakeLists.txt @@ -1,5 +1,22 @@ -link_libraries(drogon) +link_libraries(drogon trantor uuid pthread jsoncpp dl z) +if(OpenSSL_FOUND) +link_libraries(ssl crypto) +endif() + AUX_SOURCE_DIRECTORY(. SRC_DIR) -add_executable(drogon_ctl ${SRC_DIR}) -add_dependencies(drogon_ctl trantor makeVersion) +add_executable(_drogon_ctl ${SRC_DIR}) +FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/templates/*.csp) +foreach(cspFile ${SCP_LIST}) + message(STATUS "cspFile:" ${cspFile}) + EXEC_PROGRAM(basename ARGS "-s .csp ${cspFile}" OUTPUT_VARIABLE classname) + message(STATUS "view classname:" ${classname}) + add_custom_command(OUTPUT ${classname}.h ${classname}.cc + COMMAND _drogon_ctl + ARGS create view ${cspFile} + DEPENDS ${cspFile} + VERBATIM ) + set(TEMPL_SRC ${TEMPL_SRC} ${classname}.cc) +endforeach() +add_executable(drogon_ctl ${SRC_DIR} ${TEMPL_SRC}) +add_dependencies(drogon_ctl trantor makeVersion _drogon_ctl) install(TARGETS drogon_ctl DESTINATION bin) diff --git a/drogon_ctl/create_project.cc b/drogon_ctl/create_project.cc index 9a5c4b44..2ffa4d59 100644 --- a/drogon_ctl/create_project.cc +++ b/drogon_ctl/create_project.cc @@ -1,4 +1,5 @@ #include "create_project.h" +#include #include #include #include @@ -201,81 +202,8 @@ static void newJsonFindFile(std::ofstream &jsonFile) static void newConfigFile(std::ofstream &configFile) { - configFile << "/* This is a JSON format configuration file\n" - "*/\n" - "{\n" - " //ssl:the global ssl files setting\n" - " /*\"ssl\": {\n" - " \"cert\": \"../../trantor/trantor/tests/server.pem\",\n" - " \"key\": \"../../trantor/trantor/tests/server.pem\"\n" - " },\n" - " \"listeners\": [\n" - " {\n" - " //address:ip address,0.0.0.0 by default\n" - " \"address\": \"0.0.0.0\",\n" - " //port:port number\n" - " \"port\": 80,\n" - " //https:if use https for security,false by default\n" - " \"https\": false\n" - " },\n" - " {\n" - " \"address\": \"0.0.0.0\",\n" - " \"port\": 443,\n" - " \"https\": true,\n" - " //cert,key:cert file path and key file path,empty by default,\n" - " //if empty,use global setting\n" - " \"cert\": \"\",\n" - " \"key\": \"\"\n" - " }\n" - " ],*/\n" - " \"app\": {\n" - " //threads_num:num of threads,1 by default\n" - " \"threads_num\": 16,\n" - " //enable_session:false by default\n" - " \"enable_session\": false,\n" - " \"session_timeout\": 0,\n" - " //document_root:Root path of HTTP document,defaut path is ./\n" - " \"document_root\": \"./\",\n" - " /* file_types:\n" - " * HTTP download file types,The file types supported by drogon\n" - " * by default are \"html\", \"js\", \"css\", \"xml\", \"xsl\", \"txt\", \"svg\",\n" - " * \"ttf\", \"otf\", \"woff2\", \"woff\" , \"eot\", \"png\", \"jpg\", \"jpeg\",\n" - " * \"gif\", \"bmp\", \"ico\", \"icns\", etc. */\n" - " \"file_types\": [\"gif\",\"png\",\"jpg\",\"js\",\"css\",\"html\",\"ico\",\"swf\",\"xap\",\"apk\",\"cur\",\"xml\"],\n" - " //max_connections:max connections number,100000 by default\n" - " \"max_connections\": 100000,\n" - " //Load_dynamic_views: false by default, when set to true, drogon will\n" - " //compile and load dynamically \"CSP View Files\" in directories defined\n" - " //by \"dynamic_views_path\"\n" - " \"load_dynamic_views\": true,\n" - " //dynamic_views_path: if the path isn't prefixed with / or ./,\n" - " //it will be relative path of document_root path\n" - " \"dynamic_views_path\": [\"./views\"],\n" - " //log:set log output,drogon output logs to stdout by default\n" - " \"log\": {\n" - " //log_path:log file path,empty by default,in which case,log will output to the stdout\n" - " \"log_path\": \"./\",\n" - " //logfile_base_name:log file base name,empty by default which means 'drogon' will name logfile as\n" - " //drogon.log ...\n" - " \"logfile_base_name\": \"\",\n" - " //log_size_limit:100000000 bytes by default,\n" - " //When the log file size reaches \"log_size_limit\", the log file is switched.\n" - " \"log_size_limit\": 100000000,\n" - " //log_level:\"DEBUG\" by default,options:\"TRACE\",\"DEBUG\",\"INFO\",\"WARN\"\n" - " //The TRACE level is only valid when built in DEBUG mode.\n" - " \"log_level\": \"DEBUG\"\n" - " },\n" - " //run_as_daemon:false by default\n" - " \"run_as_daemon\": false,\n" - " //relaunch_on_error:false by default,if true,the program will be restart by parent after exit;\n" - " \"relaunch_on_error\": false,\n" - " //use_sendfile:true by default,if ture,the program will\n" - " //use sendfile() system-call to send static file to client;\n" - " \"use_sendfile\": true,\n" - " //use_gzip:true by default,use gzip to compress the response body's content;\n" - " \"use_gzip\": true\n" - " }\n" - "}\n"; + auto resp=HttpResponse::newHttpViewResponse("config",drogon::HttpViewData()); + configFile << resp->getBody(); } void create_project::createProject(const std::string &projectName) { diff --git a/drogon_ctl/templates/config.csp b/drogon_ctl/templates/config.csp new file mode 100644 index 00000000..af61e473 --- /dev/null +++ b/drogon_ctl/templates/config.csp @@ -0,0 +1,94 @@ +/* This is a JSON format configuration file +*/ +{ + //ssl:the global ssl files setting + /* + "ssl": { + "cert": "../../trantor/trantor/tests/server.pem", + "key": "../../trantor/trantor/tests/server.pem" + }, + "listeners": [ + { + //address:ip address,0.0.0.0 by default + "address": "0.0.0.0", + //port:port number + "port": 80, + //https:if use https for security,false by default + "https": false + }, + { + "address": "0.0.0.0", + "port": 443, + "https": true, + //cert,key:cert file path and key file path,empty by default, + //if empty,use global setting + "cert": "", + "key": "" + } + ],*/ + "app": { + //threads_num:num of threads,1 by default + "threads_num": 16, + //enable_session:false by default + "enable_session": false, + "session_timeout": 0, + //document_root:Root path of HTTP document,defaut path is ./ + "document_root": "./", + /* file_types: + * HTTP download file types,The file types supported by drogon + * by default are "html", "js", "css", "xml", "xsl", "txt", "svg", + * "ttf", "otf", "woff2", "woff" , "eot", "png", "jpg", "jpeg", + * "gif", "bmp", "ico", "icns", etc. */ + "file_types": [ + "gif", + "png", + "jpg", + "js", + "css", + "html", + "ico", + "swf", + "xap", + "apk", + "cur", + "xml" + ], + //max_connections:max connections number,100000 by default + "max_connections": 100000, + //max_connections_per_ip:max connections number per clinet,0 by default which means no limit + "max_connections_per_ip": 0, + //Load_dynamic_views: false by default, when set to true, drogon will + //compile and load dynamically "CSP View Files" in directories defined + //by "dynamic_views_path" + //"load_dynamic_views":true, + //dynamic_views_path: if the path isn't prefixed with / or ./, + //it will be relative path of document_root path + //"dynamic_views_path":["./views"], + //log:set log output,drogon output logs to stdout by default + "log": { + //log_path:log file path,empty by default,in which case,log will output to the stdout + "log_path": "./", + //logfile_base_name:log file base name,empty by default which means drogon will name logfile as + //drogon.log ... + "logfile_base_name": "", + //log_size_limit:100000000 bytes by default, + //When the log file size reaches "log_size_limit", the log file will be switched. + "log_size_limit": 100000000, + //log_level:"DEBUG" by default,options:"TRACE","DEBUG","INFO","WARN" + //The TRACE level is only valid when built in DEBUG mode. + "log_level": "DEBUG" + }, + //run_as_daemon:false by default + "run_as_daemon": false, + //relaunch_on_error:false by default,if true,the program will be restart by parent after exit; + "relaunch_on_error": false, + //use_sendfile:true by default,if ture,the program will + //use sendfile() system-call to send static file to client; + "use_sendfile": true, + //use_gzip:true by default,use gzip to compress the response body's content; + "use_gzip": true, + //static_files_cache_time:5 by default,the time in which static file response is cached, + //0 means cache forever,the negative value means no cache + "static_files_cache_time":5 + } +} diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 3930d0c9..51467137 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -109,6 +109,8 @@ class HttpAppFramework : public trantor::NonCopyable virtual void enableSendfile(bool sendFile) = 0; virtual void enableGzip(bool useGzip) = 0; virtual bool useGzip() const = 0; + virtual void setStaticFilesCacheTime(int cacheTime) = 0; + virtual int staticFilesCacheTime() const = 0; private: virtual void registerHttpApiController(const std::string &pathPattern, diff --git a/lib/src/ConfigLoader.cc b/lib/src/ConfigLoader.cc index 5e603fd1..fba7ebfc 100644 --- a/lib/src/ConfigLoader.cc +++ b/lib/src/ConfigLoader.cc @@ -160,6 +160,8 @@ static void loadApp(const Json::Value &app) HttpAppFramework::instance().enableSendfile(useSendfile); auto useGzip = app.get("use_gzip", true).asBool(); HttpAppFramework::instance().enableGzip(useGzip); + auto staticFilesCacheTime=app.get("static_files_cache_time",5).asInt(); + HttpAppFramework::instance().setStaticFilesCacheTime(staticFilesCacheTime); } static void loadListeners(const Json::Value &listeners) { diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 3e0eb6c2..68227cad 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -1059,8 +1059,8 @@ void HttpAppFrameworkImpl::readSendFile(const std::string &filePath, const HttpR resp->setStatusCode(HttpResponse::k200OK); - //cache the response for 5 seconds - resp->setExpiredTime(5); + //cache the response for 5 seconds by default + resp->setExpiredTime(_staticFilesCacheTime); _responseCacheMap->insert(filePath, resp, resp->expiredTime(), [=]() { std::lock_guard guard(_staticFilesCacheMutex); _staticFilesCache.erase(filePath); diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index e9117e94..c1d39fd0 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -74,6 +74,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework virtual void enableSendfile(bool sendFile) override { _useSendfile = sendFile; } virtual void enableGzip(bool useGzip) override { _useGzip = useGzip; } virtual bool useGzip() const override { return _useGzip; } + virtual void setStaticFilesCacheTime(int cacheTime) override { _staticFilesCacheTime = cacheTime; } + virtual int staticFilesCacheTime() const override { return _staticFilesCacheTime; } virtual ~HttpAppFrameworkImpl() { //Destroy the following objects before _loop destruction @@ -191,7 +193,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework size_t _logfileSize = 100000000; bool _useSendfile = true; bool _useGzip = true; - + int _staticFilesCacheTime = 5; std::unordered_map> _staticFilesCache; std::mutex _staticFilesCacheMutex; };