From 133fbc3499b26e0d30987b116ff8735433931bff Mon Sep 17 00:00:00 2001 From: Evgenii Sopov Date: Fri, 3 Apr 2020 18:24:27 +0700 Subject: [PATCH] Added script generate.WsjcppLightWebHttpHandler --- CMakeLists.txt | 4 + README.md | 7 ++ .../generate.WsjcppLightWebHttpHandler | 107 ++++++++++++++++++ src/main.cpp | 8 +- ...pp_light_web_http_handler_rewrite_folder.h | 2 +- ...wsjcpp_light_web_http_handler_web_folder.h | 2 +- wsjcpp.yml | 4 + 7 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 scripts.wsjcpp/generate.WsjcppLightWebHttpHandler diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e5245a..ce68286 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,9 @@ project(wsjcpp-light-web-server C CXX) include(${CMAKE_CURRENT_SOURCE_DIR}/src.wsjcpp/CMakeLists.txt) +#### BEGIN_WSJCPP_APPEND +#### END_WSJCPP_APPEND + set(CMAKE_CXX_STANDARD 11) set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-light-web-server_SOURCE_DIR}) @@ -37,3 +40,4 @@ install( /usr/bin ) + diff --git a/README.md b/README.md index 70d3cb4..4cf7344 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,13 @@ Include this files: $ wsjcpp install https://github.com/wsjcpp/wsjcpp-light-web-server:master ``` +Fast generate handler: + +``` +$ wsjcpp generate WsjcppLightWebHttpHandler SomePage +``` +So will be generated sample class 'LightWebHttpHandlerSomePage' with TODO + ## Examples ### Example for simular rewrute_rules diff --git a/scripts.wsjcpp/generate.WsjcppLightWebHttpHandler b/scripts.wsjcpp/generate.WsjcppLightWebHttpHandler new file mode 100644 index 0000000..5237982 --- /dev/null +++ b/scripts.wsjcpp/generate.WsjcppLightWebHttpHandler @@ -0,0 +1,107 @@ +#!/usr/bin/wsjcpp-safe-scripting + +# log_info rootdir +# log_info script_filename + +make_dir "src" + +var http_handler_name +set_value http_handler_name arg1 +normalize_class_name http_handler_name +convert_CamelCase_to_snake_case http_handler_name http_handler_name + +var class_name +concat class_name "LightWebHttpHandler" arg1 +normalize_class_name class_name + +var base_filename +convert_CamelCase_to_snake_case class_name base_filename +# log_info base_filename + +var filename_cpp +concat filename_cpp "./src/" base_filename ".cpp" + +var filename_h +concat filename_h "./src/" base_filename ".h" + +var ifndef_header +set_value ifndef_header base_filename +concat ifndef_header "_H" + +to_upper_case ifndef_header + +var content_header +concat content_header "#ifndef " ifndef_header " +#define " ifndef_header " + +#include + +class " class_name " : public WSJCppLightWebHttpHandlerBase { + public: + " class_name "(); + virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest); + virtual bool handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest); + + private: + std::string TAG; +}; + +#endif // " ifndef_header + + +var content_source +concat content_source " +#include \"" base_filename ".h\" +#include + +// --------------------------------------------------------------------- +// " class_name " + +" class_name "::" class_name "() +: WSJCppLightWebHttpHandlerBase(\"" http_handler_name "\") { + TAG = \"" class_name "\"; +} + +// --------------------------------------------------------------------- + +bool " class_name "::canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) { + std::string _tag = TAG + \"-\" + sWorkerId; + std::string sRequestPath = pRequest->getRequestPath(); + WSJCppLog::warn(_tag, \"canHandle: \" + sRequestPath); + + // TODO + WSJCppLog::err(TAG, \"Not implemented\"); + return false; +} + +// --------------------------------------------------------------------- + +bool " class_name "::handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) { + std::string _tag = TAG + \"-\" + sWorkerId; + std::string sRequestPath = pRequest->getRequestPath(); + WSJCppLog::warn(_tag, sRequestPath); + WSJCppLightWebHttpResponse resp(pRequest->getSockFd()); + resp.noCache().notImplemented().sendEmpty(); + return true; +} + +" + +var file_source +concat file_source "src/" filename_cpp + +write_file filename_h content_header +write_file filename_cpp content_source + +log_info " +====== +Generated class: + - " class_name " +Generated files: + - " filename_h " + - " filename_cpp " +====== +" + +cmakelists_txt_append_wsjcpp filename_h +cmakelists_txt_append_wsjcpp filename_cpp \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 019bf81..e65e0c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,11 +29,11 @@ int main(int argc, const char* argv[]) { httpServer.setPort(1234); httpServer.setMaxWorkers(4); if (sType == "folder") { - httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/app/", sDir)); - httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/", sDir)); + httpServer.addHandler(new WSJCppLightWebHttpHandlerWebFolder("/app/", sDir)); + httpServer.addHandler(new WSJCppLightWebHttpHandlerWebFolder("/", sDir)); } else if (sType == "rewrite") { - httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/app/", sDir)); - httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir)); + httpServer.addHandler(new WSJCppLightWebHttpHandlerRewriteFolder("/app/", sDir)); + httpServer.addHandler(new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir)); } httpServer.startSync(); return 0; diff --git a/src/wsjcpp_light_web_http_handler_rewrite_folder.h b/src/wsjcpp_light_web_http_handler_rewrite_folder.h index b185ae2..fb877cc 100644 --- a/src/wsjcpp_light_web_http_handler_rewrite_folder.h +++ b/src/wsjcpp_light_web_http_handler_rewrite_folder.h @@ -3,7 +3,7 @@ #include -class WSJCppLightWebHttpHandlerRewriteFolder : WSJCppLightWebHttpHandlerBase { +class WSJCppLightWebHttpHandlerRewriteFolder : public WSJCppLightWebHttpHandlerBase { public: WSJCppLightWebHttpHandlerRewriteFolder(const std::string &sPrefixPath, const std::string &sWebFolder); virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest); diff --git a/src/wsjcpp_light_web_http_handler_web_folder.h b/src/wsjcpp_light_web_http_handler_web_folder.h index 5572e0a..c98ff20 100644 --- a/src/wsjcpp_light_web_http_handler_web_folder.h +++ b/src/wsjcpp_light_web_http_handler_web_folder.h @@ -3,7 +3,7 @@ #include -class WSJCppLightWebHttpHandlerWebFolder : WSJCppLightWebHttpHandlerBase { +class WSJCppLightWebHttpHandlerWebFolder : public WSJCppLightWebHttpHandlerBase { public: WSJCppLightWebHttpHandlerWebFolder(const std::string &sPrefixPath, const std::string &sWebFolder); virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest); diff --git a/wsjcpp.yml b/wsjcpp.yml index 9a5ce3c..0886f15 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -66,6 +66,10 @@ distribution: - source-file: "src/wsjcpp_light_web_server.cpp" target-file: "wsjcpp_light_web_server.cpp" type: "source-code" + - source-file: "scripts.wsjcpp/generate.WsjcppLightWebHttpHandler" + target-file: "generate.WsjcppLightWebHttpHandler" + type: "safe-scripting-generate" + unit-tests: cases: - name: "ParseHttpRequest"