Modify drogon_ctl

This commit is contained in:
an-tao 2018-10-15 22:46:53 +08:00
parent a5bdadc3a3
commit e3ee8873c5
7 changed files with 183 additions and 170 deletions

View File

@ -20,184 +20,25 @@ void create_project::handleCommand(std::vector<std::string> &parameters)
}
static void newCmakeFile(std::ofstream &cmakeFile, const std::string &projectName)
{
cmakeFile << "cmake_minimum_required(VERSION 3.2)\n"
"Project("
<< projectName
<< ")\n"
"link_libraries(drogon trantor uuid pthread jsoncpp dl z)\n"
"find_package (OpenSSL)\n"
"if(OpenSSL_FOUND)\n"
"link_libraries(ssl crypto)\n"
"endif()\n"
"\n"
"IF (CMAKE_SYSTEM_NAME MATCHES \"Linux\")\n"
" EXEC_PROGRAM (gcc ARGS \"--version | grep '^gcc'|awk '{print $3}' | sed s'/)//g' | sed s'/-.*//g'\" OUTPUT_VARIABLE version)\n"
" MESSAGE(STATUS \"This is gcc version:: \" ${version})\n"
" if(version LESS 4.9.0)\n"
" MESSAGE(STATUS \"gcc is too old\")\n"
" stop()\n"
" elseif (version LESS 6.1.0)\n"
" MESSAGE(STATUS \"c++11\")\n"
" set(CMAKE_CXX_STD_FLAGS c++11)\n"
" elseif(version LESS 7.1.0)\n"
" set(CMAKE_CXX_STD_FLAGS c++14)\n"
" MESSAGE(STATUS \"c++14\")\n"
" else()\n"
" set(CMAKE_CXX_STD_FLAGS c++17)\n"
" set(DR_DEFS \"USE_STD_ANY\")\n"
" MESSAGE(STATUS \"c++17\")\n"
" endif()\n"
"else()\n"
" set(CMAKE_CXX_STD_FLAGS c++11)\n"
"endif()\n"
"\n"
"if(CMAKE_BUILD_TYPE STREQUAL \"\")\n"
" set(CMAKE_BUILD_TYPE Release)\n"
"endif()\n"
"\n"
"set(CMAKE_CXX_FLAGS_DEBUG \"${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=${CMAKE_CXX_STD_FLAGS}\")\n"
"set(CMAKE_CXX_FLAGS_RELEASE \"${CMAKE_CXX_FLAGS_RELEASE} -Wall -std=${CMAKE_CXX_STD_FLAGS}\")\n"
"\n"
"set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules/)\n"
"#jsoncpp\n"
"find_package (Jsoncpp REQUIRED)\n"
"include_directories(${JSONCPP_INCLUDE_DIRS})\n"
"\n"
"AUX_SOURCE_DIRECTORY(./ SRC_DIR)\n"
"AUX_SOURCE_DIRECTORY(controllers CTL_SRC)\n"
"AUX_SOURCE_DIRECTORY(filters FILTER_SRC)\n"
"\n"
"FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/views/*.csp)\n"
"foreach(cspFile ${SCP_LIST})\n"
" message(STATUS \"cspFile:\" ${cspFile})\n"
" EXEC_PROGRAM(basename ARGS \"-s .csp ${cspFile}\" OUTPUT_VARIABLE classname)\n"
" message(STATUS \"view classname:\" ${classname})\n"
" add_custom_command(OUTPUT ${classname}.h ${classname}.cc\n"
" COMMAND drogon_ctl\n"
" ARGS create view ${cspFile}\n"
" DEPENDS ${cspFile}\n"
" VERBATIM )\n"
" set(VIEWSRC ${VIEWSRC} ${classname}.cc)\n"
"endforeach()\n"
"\n"
"add_executable("
<< projectName << " ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC})\n";
HttpViewData data;
data.insert("ProjectName",projectName);
auto resp=HttpResponse::newHttpViewResponse("cmake.csp",data);
cmakeFile << resp->getBody();
}
static void newMainFile(std::ofstream &mainFile)
{
mainFile << "#include <drogon/HttpAppFramework.h>\n"
"int main() {\n"
" //Set HTTP listener address and port\n"
" drogon::HttpAppFramework::instance().addListener(\"0.0.0.0\",80);\n"
" //Run HTTP framework,the method will block in the inner event loop\n"
" drogon::HttpAppFramework::instance().run();\n"
" return 0;\n"
"}";
auto resp=HttpResponse::newHttpViewResponse("demoMain");
mainFile << resp->getBody();
}
static void newGitIgFile(std::ofstream &gitFile)
{
gitFile << "# Prerequisites\n"
"*.d\n"
"\n"
"# Compiled Object files\n"
"*.slo\n"
"*.lo\n"
"*.o\n"
"*.obj\n"
"\n"
"# Precompiled Headers\n"
"*.gch\n"
"*.pch\n"
"\n"
"# Compiled Dynamic libraries\n"
"*.so\n"
"*.dylib\n"
"*.dll\n"
"\n"
"# Fortran module files\n"
"*.mod\n"
"*.smod\n"
"\n"
"# Compiled Static libraries\n"
"*.lai\n"
"*.la\n"
"*.a\n"
"*.lib\n"
"\n"
"# Executables\n"
"*.exe\n"
"*.out\n"
"*.app\n"
"\n"
"build\n"
"cmake-build-debug\n"
".idea\n";
auto resp=HttpResponse::newHttpViewResponse("gitignore.csp");
gitFile << resp->getBody();
}
static void newJsonFindFile(std::ofstream &jsonFile)
{
jsonFile << "# Find jsoncpp\n"
"#\n"
"# Find the jsoncpp includes and library\n"
"# \n"
"# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH \n"
"# \n"
"# This module defines\n"
"# JSONCPP_INCLUDE_DIRS, where to find header, etc.\n"
"# JSONCPP_LIBRARIES, the libraries needed to use jsoncpp.\n"
"# JSONCPP_FOUND, If false, do not try to use jsoncpp.\n"
"# JSONCPP_INCLUDE_PREFIX, include prefix for jsoncpp\n"
"\n"
"# only look in default directories\n"
"find_path(\n"
"\tJSONCPP_INCLUDE_DIR \n"
"\tNAMES jsoncpp/json/json.h json/json.h\n"
"\tDOC \"jsoncpp include dir\"\n"
")\n"
"\n"
"find_library(\n"
"\tJSONCPP_LIBRARY\n"
"\tNAMES jsoncpp\n"
"\tDOC \"jsoncpp library\"\n"
")\n"
"\n"
"set(JSONCPP_INCLUDE_DIRS ${JSONCPP_INCLUDE_DIR})\n"
"set(JSONCPP_LIBRARIES ${JSONCPP_LIBRARY})\n"
"\n"
"# debug library on windows\n"
"# same naming convention as in qt (appending debug library with d)\n"
"# boost is using the same \"hack\" as us with \"optimized\" and \"debug\"\n"
"if (\"${CMAKE_CXX_COMPILER_ID}\" STREQUAL \"MSVC\")\n"
"\tfind_library(\n"
"\t\tJSONCPP_LIBRARY_DEBUG\n"
"\t\tNAMES jsoncppd\n"
"\t\tDOC \"jsoncpp debug library\"\n"
"\t)\n"
"\t\n"
"\tset(JSONCPP_LIBRARIES optimized ${JSONCPP_LIBRARIES} debug ${JSONCPP_LIBRARY_DEBUG})\n"
"\n"
"endif()\n"
"\n"
"# find JSONCPP_INCLUDE_PREFIX\n"
"find_path(\n"
"\tJSONCPP_INCLUDE_PREFIX\n"
"\tNAMES json.h\n"
"\tPATH_SUFFIXES jsoncpp/json json\n"
")\n"
"\n"
"if (${JSONCPP_INCLUDE_PREFIX} MATCHES \"jsoncpp\")\n"
"\tset(JSONCPP_INCLUDE_PREFIX \"jsoncpp\")\n"
"\tset(JSONCPP_INCLUDE_DIRS \"${JSONCPP_INCLUDE_DIRS}/jsoncpp\")\n"
"else()\n"
"\tset(JSONCPP_INCLUDE_PREFIX \"\")\n"
"endif()\n"
"\n"
"\n"
"# handle the QUIETLY and REQUIRED arguments and set JSONCPP_FOUND to TRUE\n"
"# if all listed variables are TRUE, hide their existence from configuration view\n"
"include(FindPackageHandleStandardArgs)\n"
"find_package_handle_standard_args(jsoncpp DEFAULT_MSG\n"
"\tJSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)\n"
"mark_as_advanced (JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)\n";
auto resp=HttpResponse::newHttpViewResponse("FindJsoncpp.csp");
jsonFile << resp->getBody();
}
static void newConfigFile(std::ofstream &configFile)

View File

@ -72,8 +72,14 @@ static void outputVal(std::ofstream &oSrcFile, const std::string &streamName, co
static void parseLine(std::ofstream &oSrcFile, std::string &line, const std::string &streamName, const std::string &viewDataName, int &cxx_flag, int returnFlag = 1)
{
std::string::size_type pos(0);
// std::cout<<line<<"("<<line.length()<<")\n";
if (line.length() == 0)
{
// std::cout<<"blank line!"<<std::endl;
// std::cout<<streamName<<"<<\"\\n\";\n";
oSrcFile << streamName << "<<\"\\n\";\n";
return;
}
if (cxx_flag == 0)
{
//find cxx lang begin

View File

@ -0,0 +1,63 @@
# Find jsoncpp
#
# Find the jsoncpp includes and library
#
# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH
#
# This module defines
# JSONCPP_INCLUDE_DIRS, where to find header, etc.
# JSONCPP_LIBRARIES, the libraries needed to use jsoncpp.
# JSONCPP_FOUND, If false, do not try to use jsoncpp.
# JSONCPP_INCLUDE_PREFIX, include prefix for jsoncpp
# only look in default directories
find_path(
JSONCPP_INCLUDE_DIR
NAMES jsoncpp/json/json.h json/json.h
DOC "jsoncpp include dir"
)
find_library(
JSONCPP_LIBRARY
NAMES jsoncpp
DOC "jsoncpp library"
)
set(JSONCPP_INCLUDE_DIRS ${JSONCPP_INCLUDE_DIR})
set(JSONCPP_LIBRARIES ${JSONCPP_LIBRARY})
# debug library on windows
# same naming convention as in qt (appending debug library with d)
# boost is using the same "hack" as us with "optimized" and "debug"
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
find_library(
JSONCPP_LIBRARY_DEBUG
NAMES jsoncppd
DOC "jsoncpp debug library"
)
set(JSONCPP_LIBRARIES optimized ${JSONCPP_LIBRARIES} debug ${JSONCPP_LIBRARY_DEBUG})
endif()
# find JSONCPP_INCLUDE_PREFIX
find_path(
JSONCPP_INCLUDE_PREFIX
NAMES json.h
PATH_SUFFIXES jsoncpp/json json
)
if (${JSONCPP_INCLUDE_PREFIX} MATCHES "jsoncpp")
set(JSONCPP_INCLUDE_PREFIX "jsoncpp")
set(JSONCPP_INCLUDE_DIRS "${JSONCPP_INCLUDE_DIRS}/jsoncpp")
else()
set(JSONCPP_INCLUDE_PREFIX "")
endif()
# handle the QUIETLY and REQUIRED arguments and set JSONCPP_FOUND to TRUE
# if all listed variables are TRUE, hide their existence from configuration view
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(jsoncpp DEFAULT_MSG
JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)
mark_as_advanced (JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)

View File

@ -0,0 +1,59 @@
cmake_minimum_required(VERSION 3.2)
Project({{ProjectName}})
link_libraries(drogon trantor uuid pthread jsoncpp dl z)
find_package (OpenSSL)
if(OpenSSL_FOUND)
link_libraries(ssl crypto)
endif()
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
EXEC_PROGRAM (gcc ARGS "--version | grep '^gcc'|awk '{print $3}' | sed s'/)//g' | sed s'/-.*//g'" OUTPUT_VARIABLE version)
MESSAGE(STATUS "This is gcc version:: " ${version})
if(version LESS 4.9.0)
MESSAGE(STATUS "gcc is too old")
stop()
elseif (version LESS 6.1.0)
MESSAGE(STATUS "c++11")
set(CMAKE_CXX_STD_FLAGS c++11)
elseif(version LESS 7.1.0)
set(CMAKE_CXX_STD_FLAGS c++14)
MESSAGE(STATUS "c++14")
else()
set(CMAKE_CXX_STD_FLAGS c++17)
set(DR_DEFS "USE_STD_ANY")
MESSAGE(STATUS "c++17")
endif()
else()
set(CMAKE_CXX_STD_FLAGS c++11)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=${CMAKE_CXX_STD_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -std=${CMAKE_CXX_STD_FLAGS}")
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules/)
#jsoncpp
find_package (Jsoncpp REQUIRED)
include_directories(${JSONCPP_INCLUDE_DIRS})
AUX_SOURCE_DIRECTORY(./ SRC_DIR)
AUX_SOURCE_DIRECTORY(controllers CTL_SRC)
AUX_SOURCE_DIRECTORY(filters FILTER_SRC)
FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/views/*.csp)
foreach(cspFile ${SCP_LIST})
message(STATUS "cspFile:" ${cspFile})
EXEC_PROGRAM(basename ARGS "-s .csp ${cspFile}" OUTPUT_VARIABLE classname)
message(STATUS "view classname:" ${classname})
add_custom_command(OUTPUT ${classname}.h ${classname}.cc
COMMAND drogon_ctl
ARGS create view ${cspFile}
DEPENDS ${cspFile}
VERBATIM )
set(VIEWSRC ${VIEWSRC} ${classname}.cc)
endforeach()
add_executable({{ProjectName}} ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${VIEWSRC})

View File

@ -0,0 +1,8 @@
#include <drogon/HttpAppFramework.h>
int main() {
//Set HTTP listener address and port
drogon::HttpAppFramework::instance().addListener("0.0.0.0",80);
//Run HTTP framework,the method will block in the inner event loop
drogon::HttpAppFramework::instance().run();
return 0;
}

View File

@ -0,0 +1,36 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
build
cmake-build-debug
.idea

View File

@ -165,7 +165,7 @@ class HttpResponse
static HttpResponsePtr newHttpResponse();
static HttpResponsePtr newNotFoundResponse();
static HttpResponsePtr newHttpJsonResponse(const Json::Value &data);
static HttpResponsePtr newHttpViewResponse(const std::string &viewName, const HttpViewData &data);
static HttpResponsePtr newHttpViewResponse(const std::string &viewName, const HttpViewData &data = HttpViewData());
static HttpResponsePtr newLocationRedirectResponse(const std::string &path);
virtual ~HttpResponse() {}