Skip to content

Commit

Permalink
Added script generate.WsjcppLightWebHttpHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
sea5kg committed Apr 3, 2020
1 parent 256a67b commit 133fbc3
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down Expand Up @@ -37,3 +40,4 @@ install(
/usr/bin
)


7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions scripts.wsjcpp/generate.WsjcppLightWebHttpHandler
Original file line number Diff line number Diff line change
@@ -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 <wsjcpp_light_web_server.h>

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 <wsjcpp_core.h>

// ---------------------------------------------------------------------
// " 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
8 changes: 4 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/wsjcpp_light_web_http_handler_rewrite_folder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <wsjcpp_light_web_server.h>

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);
Expand Down
2 changes: 1 addition & 1 deletion src/wsjcpp_light_web_http_handler_web_folder.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <wsjcpp_light_web_server.h>

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);
Expand Down
4 changes: 4 additions & 0 deletions wsjcpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 133fbc3

Please sign in to comment.