Skip to content

Commit 133fbc3

Browse files
committed
Added script generate.WsjcppLightWebHttpHandler
1 parent 256a67b commit 133fbc3

7 files changed

+128
-6
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ project(wsjcpp-light-web-server C CXX)
44

55
include(${CMAKE_CURRENT_SOURCE_DIR}/src.wsjcpp/CMakeLists.txt)
66

7+
#### BEGIN_WSJCPP_APPEND
8+
#### END_WSJCPP_APPEND
9+
710
set(CMAKE_CXX_STANDARD 11)
811
set(EXECUTABLE_OUTPUT_PATH ${wsjcpp-light-web-server_SOURCE_DIR})
912

@@ -37,3 +40,4 @@ install(
3740
/usr/bin
3841
)
3942

43+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Include this files:
2626
$ wsjcpp install https://github.com/wsjcpp/wsjcpp-light-web-server:master
2727
```
2828

29+
Fast generate handler:
30+
31+
```
32+
$ wsjcpp generate WsjcppLightWebHttpHandler SomePage
33+
```
34+
So will be generated sample class 'LightWebHttpHandlerSomePage' with TODO
35+
2936
## Examples
3037

3138
### Example for simular rewrute_rules
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/wsjcpp-safe-scripting
2+
3+
# log_info rootdir
4+
# log_info script_filename
5+
6+
make_dir "src"
7+
8+
var http_handler_name
9+
set_value http_handler_name arg1
10+
normalize_class_name http_handler_name
11+
convert_CamelCase_to_snake_case http_handler_name http_handler_name
12+
13+
var class_name
14+
concat class_name "LightWebHttpHandler" arg1
15+
normalize_class_name class_name
16+
17+
var base_filename
18+
convert_CamelCase_to_snake_case class_name base_filename
19+
# log_info base_filename
20+
21+
var filename_cpp
22+
concat filename_cpp "./src/" base_filename ".cpp"
23+
24+
var filename_h
25+
concat filename_h "./src/" base_filename ".h"
26+
27+
var ifndef_header
28+
set_value ifndef_header base_filename
29+
concat ifndef_header "_H"
30+
31+
to_upper_case ifndef_header
32+
33+
var content_header
34+
concat content_header "#ifndef " ifndef_header "
35+
#define " ifndef_header "
36+
37+
#include <wsjcpp_light_web_server.h>
38+
39+
class " class_name " : public WSJCppLightWebHttpHandlerBase {
40+
public:
41+
" class_name "();
42+
virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);
43+
virtual bool handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);
44+
45+
private:
46+
std::string TAG;
47+
};
48+
49+
#endif // " ifndef_header
50+
51+
52+
var content_source
53+
concat content_source "
54+
#include \"" base_filename ".h\"
55+
#include <wsjcpp_core.h>
56+
57+
// ---------------------------------------------------------------------
58+
// " class_name "
59+
60+
" class_name "::" class_name "()
61+
: WSJCppLightWebHttpHandlerBase(\"" http_handler_name "\") {
62+
TAG = \"" class_name "\";
63+
}
64+
65+
// ---------------------------------------------------------------------
66+
67+
bool " class_name "::canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
68+
std::string _tag = TAG + \"-\" + sWorkerId;
69+
std::string sRequestPath = pRequest->getRequestPath();
70+
WSJCppLog::warn(_tag, \"canHandle: \" + sRequestPath);
71+
72+
// TODO
73+
WSJCppLog::err(TAG, \"Not implemented\");
74+
return false;
75+
}
76+
77+
// ---------------------------------------------------------------------
78+
79+
bool " class_name "::handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
80+
std::string _tag = TAG + \"-\" + sWorkerId;
81+
std::string sRequestPath = pRequest->getRequestPath();
82+
WSJCppLog::warn(_tag, sRequestPath);
83+
WSJCppLightWebHttpResponse resp(pRequest->getSockFd());
84+
resp.noCache().notImplemented().sendEmpty();
85+
return true;
86+
}
87+
88+
"
89+
90+
var file_source
91+
concat file_source "src/" filename_cpp
92+
93+
write_file filename_h content_header
94+
write_file filename_cpp content_source
95+
96+
log_info "
97+
======
98+
Generated class:
99+
- " class_name "
100+
Generated files:
101+
- " filename_h "
102+
- " filename_cpp "
103+
======
104+
"
105+
106+
cmakelists_txt_append_wsjcpp filename_h
107+
cmakelists_txt_append_wsjcpp filename_cpp

src/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ int main(int argc, const char* argv[]) {
2929
httpServer.setPort(1234);
3030
httpServer.setMaxWorkers(4);
3131
if (sType == "folder") {
32-
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/app/", sDir));
33-
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/", sDir));
32+
httpServer.addHandler(new WSJCppLightWebHttpHandlerWebFolder("/app/", sDir));
33+
httpServer.addHandler(new WSJCppLightWebHttpHandlerWebFolder("/", sDir));
3434
} else if (sType == "rewrite") {
35-
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/app/", sDir));
36-
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir));
35+
httpServer.addHandler(new WSJCppLightWebHttpHandlerRewriteFolder("/app/", sDir));
36+
httpServer.addHandler(new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir));
3737
}
3838
httpServer.startSync();
3939
return 0;

src/wsjcpp_light_web_http_handler_rewrite_folder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <wsjcpp_light_web_server.h>
55

6-
class WSJCppLightWebHttpHandlerRewriteFolder : WSJCppLightWebHttpHandlerBase {
6+
class WSJCppLightWebHttpHandlerRewriteFolder : public WSJCppLightWebHttpHandlerBase {
77
public:
88
WSJCppLightWebHttpHandlerRewriteFolder(const std::string &sPrefixPath, const std::string &sWebFolder);
99
virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);

src/wsjcpp_light_web_http_handler_web_folder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <wsjcpp_light_web_server.h>
55

6-
class WSJCppLightWebHttpHandlerWebFolder : WSJCppLightWebHttpHandlerBase {
6+
class WSJCppLightWebHttpHandlerWebFolder : public WSJCppLightWebHttpHandlerBase {
77
public:
88
WSJCppLightWebHttpHandlerWebFolder(const std::string &sPrefixPath, const std::string &sWebFolder);
99
virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);

wsjcpp.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ distribution:
6666
- source-file: "src/wsjcpp_light_web_server.cpp"
6767
target-file: "wsjcpp_light_web_server.cpp"
6868
type: "source-code"
69+
- source-file: "scripts.wsjcpp/generate.WsjcppLightWebHttpHandler"
70+
target-file: "generate.WsjcppLightWebHttpHandler"
71+
type: "safe-scripting-generate"
72+
6973
unit-tests:
7074
cases:
7175
- name: "ParseHttpRequest"

0 commit comments

Comments
 (0)