2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include "file_tmpl.h"
|
|
|
|
|
#include "http_creator.h"
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool set_cookies(tpl_t* tpl)
|
|
|
|
|
{
|
|
|
|
|
printf("Please enter cookie name: "); fflush(stdout);
|
|
|
|
|
char line[256];
|
|
|
|
|
int n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, line, sizeof(line));
|
2020-08-21 14:51:09 +08:00
|
|
|
|
if (n == ACL_VSTREAM_EOF) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("enter cookie name error!\r\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string buf;
|
|
|
|
|
buf.format("const char* %s = req.getCookieValue(\"%s\");\r\n", line, line);
|
2020-08-21 14:51:09 +08:00
|
|
|
|
buf.format_append("\tif (%s == NULL) {\r\n\t\tres.addCookie(\"%s\", \"{xxx}\");\r\n\t}\r\n", line, line);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
tpl_set_field_fmt_global(tpl, "GET_COOKIES", "%s", buf.c_str());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool create_threads(file_tmpl& tmpl)
|
|
|
|
|
{
|
|
|
|
|
string file(tmpl.get_project_name());
|
|
|
|
|
file << ".cf";
|
2020-08-21 14:51:09 +08:00
|
|
|
|
if (tmpl.copy_and_replace("master_threads.cf", file.c_str()) == false) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return false;
|
2020-08-21 14:51:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
const char* name = "master_threads";
|
|
|
|
|
const FILE_FROM_TO tab[] = {
|
2020-06-29 14:34:16 +08:00
|
|
|
|
{ "main_threads.cpp", "main.cpp" },
|
2020-06-29 17:17:56 +08:00
|
|
|
|
{ "master_threads.h", "master_service.h" },
|
|
|
|
|
{ "master_threads.cpp", "master_service.cpp" },
|
|
|
|
|
{ "http_service.h", "http_service.h" },
|
2020-08-21 14:51:09 +08:00
|
|
|
|
{ "http_service.cpp", "http_service.cpp" },
|
|
|
|
|
{ "http_servlet.h", "http_servlet.h" },
|
2023-03-25 22:52:19 +08:00
|
|
|
|
{ "websocket.cpp", "websocket.cpp" },
|
|
|
|
|
{ "websocket.h", "websocket.h" },
|
2020-06-30 15:55:52 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return tmpl.files_copy(name, tab);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-23 13:34:54 +08:00
|
|
|
|
static bool create_fiber(file_tmpl& tmpl)
|
|
|
|
|
{
|
|
|
|
|
string file(tmpl.get_project_name());
|
|
|
|
|
file << ".cf";
|
|
|
|
|
if (tmpl.copy_and_replace("master_fiber.cf", file.c_str()) == false)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const char* name = "master_fiber";
|
|
|
|
|
const FILE_FROM_TO tab[] = {
|
2020-06-29 14:34:16 +08:00
|
|
|
|
{ "stdafx_fiber.h", "stdafx.h" },
|
2020-06-29 17:17:56 +08:00
|
|
|
|
{ "main_fiber.cpp", "main.cpp" },
|
|
|
|
|
{ "master_fiber.h", "master_service.h" },
|
|
|
|
|
{ "master_fiber.cpp", "master_service.cpp" },
|
|
|
|
|
{ "http_service.h", "http_service.h" },
|
2020-08-21 14:51:09 +08:00
|
|
|
|
{ "http_service.cpp", "http_service.cpp" },
|
|
|
|
|
{ "http_servlet.h", "http_servlet.h" },
|
2023-03-25 22:52:19 +08:00
|
|
|
|
{ "websocket.cpp", "websocket.cpp" },
|
|
|
|
|
{ "websocket.h", "websocket.h" },
|
2020-06-30 15:55:52 +08:00
|
|
|
|
|
2016-06-23 13:34:54 +08:00
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return tmpl.files_copy(name, tab)
|
|
|
|
|
&& tmpl.copy_and_replace("Makefile_fiber", "Makefile")
|
|
|
|
|
&& tmpl.file_copy("tmpl/Makefile_fiber.in",
|
|
|
|
|
"Makefile.in");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
static bool create_proc(file_tmpl& tmpl)
|
|
|
|
|
{
|
|
|
|
|
string file(tmpl.get_project_name());
|
|
|
|
|
file << ".cf";
|
2020-08-21 14:51:09 +08:00
|
|
|
|
if (tmpl.copy_and_replace("master_proc.cf", file.c_str()) == false) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return false;
|
2020-08-21 14:51:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
const char* name = "master_proc";
|
|
|
|
|
const FILE_FROM_TO tab[] = {
|
2020-06-29 14:34:16 +08:00
|
|
|
|
{ "main_proc.cpp", "main.cpp" },
|
2020-06-29 17:17:56 +08:00
|
|
|
|
{ "master_proc.h", "master_service.h" },
|
|
|
|
|
{ "master_proc.cpp", "master_service.cpp" },
|
|
|
|
|
{ "http_service.h", "http_service.h" },
|
2020-08-21 14:51:09 +08:00
|
|
|
|
{ "http_service.cpp", "http_service.cpp" },
|
|
|
|
|
{ "http_servlet.h", "http_servlet.h" },
|
2023-03-25 22:52:19 +08:00
|
|
|
|
{ "websocket.cpp", "websocket.cpp" },
|
|
|
|
|
{ "websocket.h", "websocket.h" },
|
2020-06-30 15:55:52 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return tmpl.files_copy(name, tab);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
static bool create_service(file_tmpl& tmpl, const char* type)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
2023-06-17 12:05:57 +08:00
|
|
|
|
if (type == NULL || *type == 0) {
|
|
|
|
|
printf("choose master_service type:\r\n");
|
|
|
|
|
printf(" t: for master_threads\r\n"
|
|
|
|
|
" f: for master_fiber\r\n"
|
|
|
|
|
" p: for master_proc\r\n");
|
|
|
|
|
printf(">");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
int n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
|
|
|
|
if (n == ACL_VSTREAM_EOF) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type = buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcasecmp(type, "t") == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
create_threads(tmpl);
|
2023-06-17 12:05:57 +08:00
|
|
|
|
} else if (strcasecmp(type, "p") == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
create_proc(tmpl);
|
2023-06-17 12:05:57 +08:00
|
|
|
|
} else if (strcasecmp(type, "f") == 0) {
|
2016-06-23 13:34:54 +08:00
|
|
|
|
create_fiber(tmpl);
|
2020-08-21 14:51:09 +08:00
|
|
|
|
} else {
|
2023-06-17 12:05:57 +08:00
|
|
|
|
printf("invalid type: %s\r\n", type);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
static bool create_http_servlet(file_tmpl& tmpl, const char* type)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2020-08-21 14:51:09 +08:00
|
|
|
|
tpl_t* tpl = tmpl.open_tpl("http_servlet.cpp");
|
|
|
|
|
if (tpl == NULL) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
return false;
|
2020-08-21 14:51:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("Do you want add cookie? [y/n]: ");
|
|
|
|
|
fflush(stdout);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2020-08-21 14:51:09 +08:00
|
|
|
|
char buf[64];
|
|
|
|
|
int n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
|
|
|
|
if (n != ACL_VSTREAM_EOF && strcasecmp(buf, "y") == 0) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
set_cookies(tpl);
|
2020-08-21 14:51:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
string filepath;
|
2020-08-21 14:51:09 +08:00
|
|
|
|
filepath.format("%s/http_servlet.cpp", tmpl.get_project_name());
|
|
|
|
|
if (tpl_save_as(tpl, filepath.c_str()) != TPL_OK) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
printf("save to %s error: %s\r\n", filepath.c_str(),
|
|
|
|
|
last_serror());
|
|
|
|
|
tpl_free(tpl);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("create %s ok.\r\n", filepath.c_str());
|
|
|
|
|
tpl_free(tpl);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2023-06-17 12:05:57 +08:00
|
|
|
|
return create_service(tmpl, type);
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
void http_creator(const char* name, const char* type)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2023-06-17 12:05:57 +08:00
|
|
|
|
bool loop;
|
2014-11-19 00:25:21 +08:00
|
|
|
|
file_tmpl tmpl;
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
if (name && *name && type && *type) {
|
|
|
|
|
loop = true;
|
|
|
|
|
} else {
|
|
|
|
|
loop = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
2014-11-19 00:25:21 +08:00
|
|
|
|
tmpl.set_path_from("tmpl/http");
|
|
|
|
|
|
2020-08-21 14:51:09 +08:00
|
|
|
|
while (true) {
|
2014-11-19 00:25:21 +08:00
|
|
|
|
char buf[256];
|
|
|
|
|
int n;
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
if (name == NULL || *name == 0) {
|
|
|
|
|
printf("please input your program name: ");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
|
|
|
|
if (n == ACL_VSTREAM_EOF) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
|
acl::safe_snprintf(buf, sizeof(buf), "http_demo");
|
|
|
|
|
}
|
2022-01-01 11:28:01 +08:00
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
name = buf;
|
2020-08-21 14:51:09 +08:00
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
tmpl.set_project_name(name);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ŀ¼
|
2014-11-19 00:25:21 +08:00
|
|
|
|
tmpl.create_dirs();
|
|
|
|
|
|
2023-06-17 12:05:57 +08:00
|
|
|
|
tmpl.create_common();
|
|
|
|
|
create_http_servlet(tmpl, type);
|
2023-06-22 19:38:36 +08:00
|
|
|
|
|
|
|
|
|
if (!loop) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|