2018-05-31 09:00:31 +00:00
|
|
|
/**
|
|
|
|
*
|
2018-12-07 07:50:18 +00:00
|
|
|
* create_controller.cc
|
|
|
|
* An Tao
|
2018-05-31 09:00:31 +00:00
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
2018-12-07 07:50:18 +00:00
|
|
|
* https://github.com/an-tao/drogon
|
2018-05-31 09:00:31 +00:00
|
|
|
* Use of this source code is governed by a MIT license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
2018-11-16 05:26:14 +00:00
|
|
|
* Drogon
|
2018-05-31 09:00:31 +00:00
|
|
|
*
|
|
|
|
*/
|
2018-05-29 05:49:26 +00:00
|
|
|
|
|
|
|
#include "create_controller.h"
|
|
|
|
#include "cmd.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2018-06-06 13:40:48 +00:00
|
|
|
#include <regex>
|
|
|
|
|
2018-05-29 05:49:26 +00:00
|
|
|
using namespace drogon_ctl;
|
|
|
|
|
|
|
|
void create_controller::handleCommand(std::vector<std::string> ¶meters)
|
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
// std::cout<<"create!"<<std::endl;
|
2018-10-14 07:56:54 +00:00
|
|
|
ControllerType type = Simple;
|
|
|
|
for (auto iter = parameters.begin(); iter != parameters.end(); iter++)
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if ((*iter)[0] == '-')
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
if (*iter == "-s" || (*iter == "--simple"))
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
|
|
|
parameters.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
2018-11-15 06:31:10 +00:00
|
|
|
else if (*iter == "-a" || *iter == "-h" || *iter == "--http")
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-11-15 06:31:10 +00:00
|
|
|
type = Http;
|
2018-05-29 05:49:26 +00:00
|
|
|
parameters.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-14 07:56:54 +00:00
|
|
|
else if (*iter == "-w" || *iter == "--websocket")
|
2018-08-22 06:27:45 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
type = WebSocket;
|
2018-08-22 06:27:45 +00:00
|
|
|
parameters.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
2018-05-29 05:49:26 +00:00
|
|
|
else
|
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
std::cout << ARGS_ERROR_STR << std::endl;
|
2018-05-29 05:49:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-15 13:21:39 +00:00
|
|
|
createController(parameters, type);
|
2018-05-29 05:49:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newSimpleControllerHeaderFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className)
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "#pragma once\n";
|
|
|
|
file << "#include <drogon/HttpSimpleController.h>\n";
|
|
|
|
file << "using namespace drogon;\n";
|
|
|
|
std::string indent = "";
|
2018-12-15 13:21:39 +00:00
|
|
|
std::string class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
|
|
|
while (pos != std::string::npos)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2018-12-15 13:21:39 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
|
|
|
file << indent << "namespace " << namespaceName << "\n";
|
|
|
|
namepace_path.append(namespaceName).append("/");
|
|
|
|
file << indent << "{\n";
|
|
|
|
indent.append(" ");
|
|
|
|
pos = class_name.find("::");
|
2018-05-29 05:57:10 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent << "class " << class_name
|
|
|
|
<< ":public drogon::HttpSimpleController<" << class_name << ">\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "{\n";
|
|
|
|
file << indent << "public:\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< " virtual void asyncHandleHttpRequest(const HttpRequestPtr& "
|
|
|
|
"req, std::function<void (const HttpResponsePtr &)> &&callback) "
|
|
|
|
"override;\n";
|
2018-05-29 05:49:26 +00:00
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << " PATH_LIST_BEGIN\n";
|
|
|
|
file << indent << " //list path definitions here;\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< " "
|
|
|
|
"//PATH_ADD(\"/"
|
|
|
|
"path\",\"filter1\",\"filter2\",HttpMethod1,HttpMethod2...);\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << " PATH_LIST_END\n";
|
|
|
|
file << indent << "};\n";
|
|
|
|
if (indent == "")
|
2018-08-22 06:27:45 +00:00
|
|
|
return;
|
2018-10-14 07:56:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
indent.resize(indent.length() - 4);
|
|
|
|
file << indent << "}\n";
|
|
|
|
} while (indent != "");
|
2018-05-29 05:49:26 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newSimpleControllerSourceFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className,
|
|
|
|
const std::string &filename)
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-12-15 13:21:39 +00:00
|
|
|
file << "#include \"" << filename << ".h\"\n";
|
|
|
|
auto pos = className.rfind("::");
|
|
|
|
auto class_name = className;
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
auto namespacename = className.substr(0, pos);
|
|
|
|
file << "using namespace " << namespacename << ";\n";
|
|
|
|
class_name = className.substr(pos + 2);
|
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "void " << class_name
|
|
|
|
<< "::asyncHandleHttpRequest(const HttpRequestPtr& req, "
|
|
|
|
"std::function<void (const HttpResponsePtr &)> &&callback)\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "{\n";
|
|
|
|
file << " //write your application logic here\n";
|
|
|
|
file << "}";
|
2018-05-29 05:49:26 +00:00
|
|
|
}
|
2018-08-22 06:27:45 +00:00
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newWebsockControllerHeaderFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className)
|
2018-08-22 06:27:45 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "#pragma once\n";
|
|
|
|
file << "#include <drogon/WebSocketController.h>\n";
|
|
|
|
file << "using namespace drogon;\n";
|
|
|
|
std::string indent = "";
|
2018-12-15 13:21:39 +00:00
|
|
|
std::string class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
|
|
|
while (pos != std::string::npos)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2018-12-15 13:21:39 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
|
|
|
file << indent << "namespace " << namespaceName << "\n";
|
|
|
|
namepace_path.append(namespaceName).append("/");
|
|
|
|
file << indent << "{\n";
|
|
|
|
indent.append(" ");
|
|
|
|
pos = class_name.find("::");
|
2018-08-22 06:27:45 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent << "class " << class_name
|
|
|
|
<< ":public drogon::WebSocketController<" << class_name << ">\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "{\n";
|
|
|
|
file << indent << "public:\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file
|
|
|
|
<< indent
|
|
|
|
<< " virtual void handleNewMessage(const WebSocketConnectionPtr&,\n";
|
2019-04-06 15:06:38 +00:00
|
|
|
file << indent << " std::string &&,\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< " const WebSocketMessageType &) "
|
|
|
|
"override;\n";
|
|
|
|
file << indent
|
|
|
|
<< " virtual void handleNewConnection(const HttpRequestPtr &,\n";
|
|
|
|
file << indent
|
|
|
|
<< " const "
|
|
|
|
"WebSocketConnectionPtr&)override;\n";
|
|
|
|
file << indent
|
|
|
|
<< " virtual void handleConnectionClosed(const "
|
|
|
|
"WebSocketConnectionPtr&)override;\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << " WS_PATH_LIST_BEGIN\n";
|
|
|
|
file << indent << " //list path definitions here;\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< " //WS_PATH_ADD(\"/path\",\"filter1\",\"filter2\",...);\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << " WS_PATH_LIST_END\n";
|
|
|
|
file << indent << "};\n";
|
|
|
|
if (indent == "")
|
2018-08-22 06:27:45 +00:00
|
|
|
return;
|
2018-10-14 07:56:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
indent.resize(indent.length() - 4);
|
|
|
|
file << indent << "}\n";
|
|
|
|
} while (indent != "");
|
2018-08-22 06:27:45 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newWebsockControllerSourceFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className,
|
|
|
|
const std::string &filename)
|
2018-08-22 06:27:45 +00:00
|
|
|
{
|
2018-12-15 13:21:39 +00:00
|
|
|
file << "#include \"" << filename << ".h\"\n";
|
|
|
|
auto pos = className.rfind("::");
|
|
|
|
auto class_name = className;
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
auto namespacename = className.substr(0, pos);
|
|
|
|
file << "using namespace " << namespacename << ";\n";
|
|
|
|
class_name = className.substr(pos + 2);
|
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "void " << class_name
|
|
|
|
<< "::handleNewMessage(const WebSocketConnectionPtr& wsConnPtr, "
|
|
|
|
"std::string &&message, const WebSocketMessageType &type)\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "{\n";
|
|
|
|
file << " //write your application logic here\n";
|
|
|
|
file << "}\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "void " << class_name
|
|
|
|
<< "::handleNewConnection(const HttpRequestPtr &req,const "
|
|
|
|
"WebSocketConnectionPtr& wsConnPtr)\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "{\n";
|
|
|
|
file << " //write your application logic here\n";
|
|
|
|
file << "}\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << "void " << class_name
|
|
|
|
<< "::handleConnectionClosed(const WebSocketConnectionPtr& "
|
|
|
|
"wsConnPtr)\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "{\n";
|
|
|
|
file << " //write your application logic here\n";
|
|
|
|
file << "}\n";
|
2018-08-22 06:27:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newHttpControllerHeaderFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className)
|
2018-06-06 10:29:05 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "#pragma once\n";
|
2018-11-15 06:31:10 +00:00
|
|
|
file << "#include <drogon/HttpController.h>\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "using namespace drogon;\n";
|
|
|
|
std::string indent = "";
|
|
|
|
std::string class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
|
|
|
while (pos != std::string::npos)
|
2018-06-06 13:40:48 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
|
|
|
file << indent << "namespace " << namespaceName << "\n";
|
2018-06-06 13:40:48 +00:00
|
|
|
namepace_path.append(namespaceName).append("/");
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "{\n";
|
2018-06-06 13:40:48 +00:00
|
|
|
indent.append(" ");
|
2018-10-14 07:56:54 +00:00
|
|
|
pos = class_name.find("::");
|
2018-06-06 13:40:48 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent << "class " << class_name
|
|
|
|
<< ":public drogon::HttpController<" << class_name << ">\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "{\n";
|
|
|
|
file << indent << "public:\n";
|
2018-06-06 13:40:48 +00:00
|
|
|
indent.append(" ");
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "METHOD_LIST_BEGIN\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< "//use METHOD_ADD to add your custom processing function here;\n";
|
|
|
|
file << indent << "//METHOD_ADD(" << class_name
|
|
|
|
<< "::get,\"/get/{2}/{1}\",Get);"
|
|
|
|
"//path is "
|
2018-10-14 07:56:54 +00:00
|
|
|
<< namepace_path << class_name << "/get/{arg2}/{arg1}\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent << "//METHOD_ADD(" << class_name
|
|
|
|
<< "::your_method_name,\"/{1}/{2}/list\",Get);"
|
|
|
|
"//path is "
|
2018-10-14 07:56:54 +00:00
|
|
|
<< namepace_path << class_name << "/{arg1}/{arg2}/list\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent << "//ADD_METHOD_TO(" << class_name
|
|
|
|
<< "::your_method_name,\"/absolute/path/{1}/{2}/list\",Get);"
|
|
|
|
"//path is "
|
2019-04-04 17:50:29 +00:00
|
|
|
<< namepace_path << "/absolute/path/{arg1}/{arg2}/list\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
file << indent << "\n";
|
|
|
|
file << indent << "METHOD_LIST_END\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file << indent
|
|
|
|
<< "//your declaration of processing function maybe like this:\n";
|
|
|
|
file << indent
|
|
|
|
<< "//void get(const HttpRequestPtr& req,"
|
|
|
|
"std::function<void (const HttpResponsePtr &)> &&callback,int "
|
|
|
|
"p1,std::string p2);\n";
|
|
|
|
file << indent
|
|
|
|
<< "//void your_method_name(const HttpRequestPtr& req,"
|
|
|
|
"std::function<void (const HttpResponsePtr &)> &&callback,double "
|
|
|
|
"p1,int p2) const;\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
indent.resize(indent.length() - 4);
|
|
|
|
file << indent << "};\n";
|
|
|
|
if (indent == "")
|
2018-06-06 13:40:48 +00:00
|
|
|
return;
|
2018-10-14 07:56:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
indent.resize(indent.length() - 4);
|
|
|
|
file << indent << "}\n";
|
|
|
|
} while (indent != "");
|
2018-06-06 10:29:05 +00:00
|
|
|
}
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::newHttpControllerSourceFile(
|
|
|
|
std::ofstream &file,
|
|
|
|
const std::string &className,
|
|
|
|
const std::string &filename)
|
2018-05-29 05:49:26 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "#include \"" << filename << ".h\"\n";
|
|
|
|
auto pos = className.rfind("::");
|
|
|
|
auto class_name = className;
|
|
|
|
if (pos != std::string::npos)
|
2018-06-06 13:40:48 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
auto namespacename = className.substr(0, pos);
|
|
|
|
file << "using namespace " << namespacename << ";\n";
|
|
|
|
class_name = className.substr(pos + 2);
|
2018-06-06 13:40:48 +00:00
|
|
|
}
|
2018-05-29 05:49:26 +00:00
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
file << "//add definition of your processing function here\n";
|
2018-06-23 11:22:20 +00:00
|
|
|
}
|
2018-12-15 13:21:39 +00:00
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::createController(std::vector<std::string> &httpClasses,
|
|
|
|
ControllerType type)
|
2018-12-15 13:21:39 +00:00
|
|
|
{
|
|
|
|
for (auto iter = httpClasses.begin(); iter != httpClasses.end(); iter++)
|
|
|
|
{
|
|
|
|
if ((*iter)[0] == '-')
|
|
|
|
{
|
|
|
|
std::cout << ARGS_ERROR_STR << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 09:32:49 +00:00
|
|
|
for (auto const &className : httpClasses)
|
2018-12-15 13:21:39 +00:00
|
|
|
{
|
|
|
|
createController(className, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void create_controller::createController(const std::string &className,
|
|
|
|
ControllerType type)
|
2018-12-15 13:21:39 +00:00
|
|
|
{
|
|
|
|
std::regex regex("::");
|
2019-05-18 12:39:57 +00:00
|
|
|
std::string ctlName =
|
|
|
|
std::regex_replace(className, regex, std::string("_"));
|
2018-12-15 13:21:39 +00:00
|
|
|
|
|
|
|
std::string headFileName = ctlName + ".h";
|
|
|
|
std::string sourceFilename = ctlName + ".cc";
|
|
|
|
{
|
|
|
|
std::ifstream iHeadFile(headFileName.c_str(), std::ifstream::in);
|
|
|
|
std::ifstream iSourceFile(sourceFilename.c_str(), std::ifstream::in);
|
|
|
|
|
|
|
|
if (iHeadFile || iSourceFile)
|
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
std::cout << "The file you want to create already exists, "
|
|
|
|
"overwrite it(y/n)?"
|
|
|
|
<< std::endl;
|
2018-12-15 13:21:39 +00:00
|
|
|
auto in = getchar();
|
2019-05-18 12:39:57 +00:00
|
|
|
(void)getchar(); // get the return key
|
2018-12-15 13:21:39 +00:00
|
|
|
if (in != 'Y' && in != 'y')
|
|
|
|
{
|
|
|
|
std::cout << "Abort!" << std::endl;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::ofstream oHeadFile(headFileName.c_str(), std::ofstream::out);
|
|
|
|
std::ofstream oSourceFile(sourceFilename.c_str(), std::ofstream::out);
|
|
|
|
if (!oHeadFile || !oSourceFile)
|
|
|
|
{
|
|
|
|
perror("");
|
2019-07-26 14:22:12 +00:00
|
|
|
exit(1);
|
2018-12-15 13:21:39 +00:00
|
|
|
}
|
|
|
|
if (type == Http)
|
|
|
|
{
|
|
|
|
std::cout << "create a http controller:" << className << std::endl;
|
|
|
|
newHttpControllerHeaderFile(oHeadFile, className);
|
|
|
|
newHttpControllerSourceFile(oSourceFile, className, ctlName);
|
|
|
|
}
|
|
|
|
else if (type == Simple)
|
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
std::cout << "create a http simple controller:" << className
|
|
|
|
<< std::endl;
|
2018-12-15 13:21:39 +00:00
|
|
|
newSimpleControllerHeaderFile(oHeadFile, className);
|
|
|
|
newSimpleControllerSourceFile(oSourceFile, className, ctlName);
|
|
|
|
}
|
|
|
|
else if (type == WebSocket)
|
|
|
|
{
|
|
|
|
std::cout << "create a websocket controller:" << className << std::endl;
|
|
|
|
newWebsockControllerHeaderFile(oHeadFile, className);
|
|
|
|
newWebsockControllerSourceFile(oSourceFile, className, ctlName);
|
|
|
|
}
|
|
|
|
}
|