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"
|
2019-09-30 13:34:30 +00:00
|
|
|
#include <drogon/DrTemplateBase.h>
|
|
|
|
#include <drogon/utils/Utilities.h>
|
2018-05-29 05:49:26 +00:00
|
|
|
#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;
|
2019-09-30 13:34:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
else if (*iter == "-r" || *iter == "--restful")
|
|
|
|
{
|
|
|
|
type = Restful;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
if (type != Restful)
|
|
|
|
createController(parameters, type);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string resource;
|
|
|
|
for (auto iter = parameters.begin(); iter != parameters.end(); ++iter)
|
|
|
|
{
|
|
|
|
if ((*iter).find("--resource=") == 0)
|
|
|
|
{
|
|
|
|
resource = (*iter).substr(strlen("--resource="));
|
|
|
|
parameters.erase(iter);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((*iter)[0] == '-')
|
|
|
|
{
|
|
|
|
std::cerr << "Error parameter for '" << (*iter) << "'"
|
|
|
|
<< std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parameters.size() > 1)
|
|
|
|
{
|
|
|
|
std::cerr << "Too many parameters" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
auto className = parameters[0];
|
|
|
|
createARestfulController(className, resource);
|
|
|
|
}
|
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";
|
2018-12-15 13:21:39 +00:00
|
|
|
std::string class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
2019-09-30 13:34:30 +00:00
|
|
|
size_t namespaceCount = 0;
|
2018-12-15 13:21:39 +00:00
|
|
|
while (pos != std::string::npos)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
++namespaceCount;
|
2018-12-15 13:21:39 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "namespace " << namespaceName << "\n";
|
2018-12-15 13:21:39 +00:00
|
|
|
namepace_path.append(namespaceName).append("/");
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "{\n";
|
2018-12-15 13:21:39 +00:00
|
|
|
pos = class_name.find("::");
|
2018-05-29 05:57:10 +00:00
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "class " << class_name << ":public drogon::HttpSimpleController<"
|
|
|
|
<< class_name << ">\n";
|
|
|
|
file << "{\n";
|
|
|
|
file << " public:\n";
|
|
|
|
file << " virtual void asyncHandleHttpRequest(const HttpRequestPtr& "
|
2019-05-18 12:39:57 +00:00
|
|
|
"req, std::function<void (const HttpResponsePtr &)> &&callback) "
|
|
|
|
"override;\n";
|
2018-05-29 05:49:26 +00:00
|
|
|
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " PATH_LIST_BEGIN\n";
|
|
|
|
file << " //list path definitions here;\n";
|
|
|
|
file << " "
|
2019-05-18 12:39:57 +00:00
|
|
|
"//PATH_ADD(\"/"
|
|
|
|
"path\",\"filter1\",\"filter2\",HttpMethod1,HttpMethod2...);\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " PATH_LIST_END\n";
|
|
|
|
file << "};\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
while (namespaceCount > 0)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
--namespaceCount;
|
|
|
|
file << "}\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
}
|
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";
|
2018-12-15 13:21:39 +00:00
|
|
|
std::string class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
2019-09-30 13:34:30 +00:00
|
|
|
size_t namespaceCount = 0;
|
2018-12-15 13:21:39 +00:00
|
|
|
while (pos != std::string::npos)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
++namespaceCount;
|
2018-12-15 13:21:39 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "namespace " << namespaceName << "\n";
|
2018-12-15 13:21:39 +00:00
|
|
|
namepace_path.append(namespaceName).append("/");
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "{\n";
|
2018-12-15 13:21:39 +00:00
|
|
|
pos = class_name.find("::");
|
2018-08-22 06:27:45 +00:00
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "class " << class_name << ":public drogon::WebSocketController<"
|
|
|
|
<< class_name << ">\n";
|
|
|
|
file << "{\n";
|
|
|
|
file << " public:\n";
|
2019-05-18 12:39:57 +00:00
|
|
|
file
|
|
|
|
<< " virtual void handleNewMessage(const WebSocketConnectionPtr&,\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " std::string &&,\n";
|
|
|
|
file << " const WebSocketMessageType &) "
|
2019-05-18 12:39:57 +00:00
|
|
|
"override;\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " virtual void handleNewConnection(const HttpRequestPtr &,\n";
|
|
|
|
file << " const "
|
2019-05-18 12:39:57 +00:00
|
|
|
"WebSocketConnectionPtr&)override;\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " virtual void handleConnectionClosed(const "
|
2019-05-18 12:39:57 +00:00
|
|
|
"WebSocketConnectionPtr&)override;\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " WS_PATH_LIST_BEGIN\n";
|
|
|
|
file << " //list path definitions here;\n";
|
|
|
|
file << " //WS_PATH_ADD(\"/path\",\"filter1\",\"filter2\",...);\n";
|
|
|
|
file << " WS_PATH_LIST_END\n";
|
|
|
|
file << "};\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
while (namespaceCount > 0)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
--namespaceCount;
|
|
|
|
file << "}\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
}
|
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 class_name = className;
|
|
|
|
std::string namepace_path = "/";
|
|
|
|
auto pos = class_name.find("::");
|
2019-09-30 13:34:30 +00:00
|
|
|
size_t namespaceCount = 0;
|
2018-10-14 07:56:54 +00:00
|
|
|
while (pos != std::string::npos)
|
2018-06-06 13:40:48 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
++namespaceCount;
|
2018-10-14 07:56:54 +00:00
|
|
|
auto namespaceName = class_name.substr(0, pos);
|
|
|
|
class_name = class_name.substr(pos + 2);
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "namespace " << namespaceName << "\n";
|
2018-06-06 13:40:48 +00:00
|
|
|
namepace_path.append(namespaceName).append("/");
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "{\n";
|
2018-10-14 07:56:54 +00:00
|
|
|
pos = class_name.find("::");
|
2018-06-06 13:40:48 +00:00
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "class " << class_name << ":public drogon::HttpController<"
|
|
|
|
<< class_name << ">\n";
|
|
|
|
file << "{\n";
|
|
|
|
file << " public:\n";
|
|
|
|
file << " METHOD_LIST_BEGIN\n";
|
|
|
|
file << " //use METHOD_ADD to add your custom processing function "
|
|
|
|
"here;\n";
|
|
|
|
file << " //METHOD_ADD(" << class_name
|
|
|
|
<< "::get,\"/{2}/{1}\",Get);"
|
2019-05-18 12:39:57 +00:00
|
|
|
"//path is "
|
2019-09-30 13:34:30 +00:00
|
|
|
<< namepace_path << class_name << "/{arg2}/{arg1}\n";
|
|
|
|
file << " //METHOD_ADD(" << class_name
|
2019-05-18 12:39:57 +00:00
|
|
|
<< "::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-09-30 13:34:30 +00:00
|
|
|
file << " //ADD_METHOD_TO(" << class_name
|
2019-05-18 12:39:57 +00:00
|
|
|
<< "::your_method_name,\"/absolute/path/{1}/{2}/list\",Get);"
|
2019-09-30 13:34:30 +00:00
|
|
|
"//path is /absolute/path/{arg1}/{arg2}/list\n";
|
|
|
|
file << "\n";
|
|
|
|
file << " METHOD_LIST_END\n";
|
|
|
|
file << " // your declaration of processing function maybe like this:\n";
|
|
|
|
file << " // void get(const HttpRequestPtr& req,"
|
2019-05-18 12:39:57 +00:00
|
|
|
"std::function<void (const HttpResponsePtr &)> &&callback,int "
|
|
|
|
"p1,std::string p2);\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << " // void your_method_name(const HttpRequestPtr& req,"
|
2019-05-18 12:39:57 +00:00
|
|
|
"std::function<void (const HttpResponsePtr &)> &&callback,double "
|
|
|
|
"p1,int p2) const;\n";
|
2019-09-30 13:34:30 +00:00
|
|
|
file << "};\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
while (namespaceCount > 0)
|
2018-10-14 07:56:54 +00:00
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
--namespaceCount;
|
|
|
|
file << "}\n";
|
2019-10-06 01:43:08 +00:00
|
|
|
}
|
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
|
|
|
{
|
2019-09-30 13:34:30 +00:00
|
|
|
for (auto iter = httpClasses.begin(); iter != httpClasses.end(); ++iter)
|
2018-12-15 13:21:39 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 13:34:30 +00:00
|
|
|
|
|
|
|
void create_controller::createARestfulController(const std::string &className,
|
|
|
|
const std::string &resource)
|
|
|
|
{
|
|
|
|
std::regex regex("::");
|
|
|
|
std::string ctlName =
|
|
|
|
std::regex_replace(className, regex, std::string("_"));
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::cout << "The file you want to create already exists, "
|
|
|
|
"overwrite it(y/n)?"
|
|
|
|
<< std::endl;
|
|
|
|
auto in = getchar();
|
|
|
|
(void)getchar(); // get the return key
|
|
|
|
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("");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
auto v = utils::splitString(className, "::");
|
|
|
|
drogon::DrTemplateData data;
|
|
|
|
data.insert("className", v[v.size() - 1]);
|
|
|
|
v.pop_back();
|
|
|
|
data.insert("namespaceVector", v);
|
|
|
|
data.insert("resource", resource);
|
|
|
|
data.insert("fileName", ctlName);
|
|
|
|
if (resource.empty())
|
|
|
|
{
|
|
|
|
data.insert("ctlCommand",
|
|
|
|
std::string("drogon_ctl create controller -r ") +
|
|
|
|
className);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.insert("ctlCommand",
|
|
|
|
std::string("drogon_ctl create controller -r ") +
|
|
|
|
className + " --resource=" + resource);
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto templ = DrTemplateBase::newTemplate("restful_controller_h.csp");
|
|
|
|
oHeadFile << templ->genText(data);
|
|
|
|
templ = DrTemplateBase::newTemplate("restful_controller_cc.csp");
|
|
|
|
oSourceFile << templ->genText(data);
|
|
|
|
}
|
|
|
|
catch (const std::exception &err)
|
|
|
|
{
|
|
|
|
std::cerr << err.what() << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
std::cout << "create a http restful API controller:" << className
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << "file name: " << ctlName << ".h and " << ctlName << ".cc"
|
|
|
|
<< std::endl;
|
|
|
|
}
|