Add configuration options to the configuration file for simple controller mapping

This commit is contained in:
antao 2018-10-21 14:52:41 +08:00
parent 2fcbc5600f
commit af53337a6a
12 changed files with 241 additions and 26 deletions

View File

@ -25,6 +25,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules/)
#jsoncpp
find_package (Jsoncpp REQUIRED)
include_directories(${JSONCPP_INCLUDE_DIRS})
link_libraries(${JSONCPP_LIBRARIES})
message(STATUS "jsoncpp inc path:" ${JSONCPP_INCLUDE_DIRS})
#message(STATUS ${JSONCPP_LIBRARIES})
@ -41,11 +43,12 @@ endif()
find_package (UUID REQUIRED)
include_directories(${UUID_INCLUDE_DIR})
message(STATUS "uuid inc path:" ${UUID_INCLUDE_DIR})
link_libraries(${UUID_LIBRARIES})
find_package (OpenSSL)
if(OpenSSL_FOUND)
include_directories(${OPENSSL_INCLUDE_DIR})
link_libraries(${OPENSSL_LIBRARIES})
message(STATUS "openssl inc path:" ${OPENSSL_INCLUDE_DIR})
else()
aux_source_directory(${PROJECT_SOURCE_DIR}/lib/src/ssl_funcs DIR_SRCS)
@ -54,6 +57,8 @@ endif()
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})
link_libraries(${ZLIB_LIBRARIES})
message(STATUS "zlib inc path:" ${ZLIB_INCLUDE_DIR})
if(CMAKE_BUILD_TYPE STREQUAL "")

View File

@ -89,6 +89,15 @@
"use_gzip": true,
//static_files_cache_time:5 by default,the time in which static file response is cached,
//0 means cache forever,the negative value means no cache
"static_files_cache_time":5
"static_files_cache_time": 5,
//simple_controllers_map:Configuring mapping from path to simple controller
"simple_controllers_map": [
{
"path": "/path/name",
"controller": "controllerClassName",
"http_methods": ["get","post"],
"filters": ["FilterClassName"]
}
]
}
}
}

View File

@ -1,7 +1,4 @@
link_libraries(drogon trantor uuid pthread jsoncpp dl z)
if(OpenSSL_FOUND)
link_libraries(ssl crypto)
endif()
link_libraries(drogon trantor pthread dl)
AUX_SOURCE_DIRECTORY(. SRC_DIR)
add_executable(_drogon_ctl main.cc cmd.cc create.cc create_view.cc)

View File

@ -49,6 +49,13 @@ static void newGitIgFile(std::ofstream &gitFile)
auto templ=DrTemplateBase::newTemplate("gitignore.csp");
gitFile << templ->genText();
}
static void newUuidFindFile(std::ofstream &uuidFile)
{
auto templ=DrTemplateBase::newTemplate("FindUUID.csp");
uuidFile << templ->genText();
}
static void newJsonFindFile(std::ofstream &jsonFile)
{
auto templ=DrTemplateBase::newTemplate("FindJsoncpp.csp");
@ -84,6 +91,9 @@ void create_project::createProject(const std::string &projectName)
mkdir("cmake_modules", 0755);
std::ofstream jsonFile("cmake_modules/FindJsoncpp.cmake", std::ofstream::out);
newJsonFindFile(jsonFile);
std::ofstream uuidFile("cmake_modules/FindUUID.cmake", std::ofstream::out);
newUuidFindFile(uuidFile);
std::ofstream gitFile(".gitignore", std::ofstream::out);
newGitIgFile(gitFile);
std::ofstream configFile("config.json", std::ofstream::out);

View File

@ -75,9 +75,9 @@ static void outputVal(std::ofstream &oSrcFile, const std::string &streamName, co
static void outputSubView(std::ofstream &oSrcFile, const std::string &streamName, const std::string &viewDataName, const std::string &keyName)
{
oSrcFile << "{\n";
oSrcFile << " auto templ=DrTemplateBase::newTemplate(\""<<keyName<<"\");\n";
oSrcFile << " auto templ=DrTemplateBase::newTemplate(\"" << keyName << "\");\n";
oSrcFile << " if(templ){\n";
oSrcFile << " "<<streamName<<"<< templ->genText("<<viewDataName<<");\n";
oSrcFile << " " << streamName << "<< templ->genText(" << viewDataName << ");\n";
oSrcFile << " }\n";
oSrcFile << "}\n";
}
@ -159,7 +159,11 @@ static void parseLine(std::ofstream &oSrcFile, std::string &line, const std::str
else
{
if (line.length() > 0)
oSrcFile << "\t" << streamName << " << \"" << replace_all(line, "\"", "\\\"");
{
replace_all(line, "\\", "\\\\");
replace_all(line, "\"", "\\\"");
oSrcFile << "\t" << streamName << " << \"" << line;
}
if (returnFlag)
oSrcFile << "\\n\";\n";
else

119
drogon_ctl/templates/FindUUID.csp Executable file
View File

@ -0,0 +1,119 @@
# - Try to find UUID
# Once done this will define
#
# UUID_FOUND - system has UUID
# UUID_INCLUDE_DIRS - the UUID include directory
# UUID_LIBRARIES - Link these to use UUID
# UUID_DEFINITIONS - Compiler switches required for using UUID
#
# Copyright (c) 2006 Andreas Schneider <mail@cynapses.org>
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
if (UUID_LIBRARIES AND UUID_INCLUDE_DIRS)
# in cache already
set(UUID_FOUND TRUE)
else (UUID_LIBRARIES AND UUID_INCLUDE_DIRS)
find_path(UUID_INCLUDE_DIR
NAMES
uuid.h
PATH_SUFFIXES
uuid
HINTS
${UUID_DIR}/include
$ENV{UUID_DIR}/include
$ENV{UUID_DIR}
${DELTA3D_EXT_DIR}/inc
$ENV{DELTA_ROOT}/ext/inc
$ENV{DELTA_ROOT}
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local/include
/usr/include
/usr/include/gdal
/sw/include # Fink
/opt/local/include # DarwinPorts
/opt/csw/include # Blastwave
/opt/include
[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session\ Manager\\Environment;OSG_ROOT]/include
/usr/freeware/include
)
find_library(UUID_LIBRARY
NAMES
uuid ossp-uuid
HINTS
${UUID_DIR}/lib
$ENV{UUID_DIR}/lib
$ENV{UUID_DIR}
${DELTA3D_EXT_DIR}/lib
$ENV{DELTA_ROOT}/ext/lib
$ENV{DELTA_ROOT}
$ENV{OSG_ROOT}/lib
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local/lib
/usr/lib
/sw/lib
/opt/local/lib
/opt/csw/lib
/opt/lib
/usr/freeware/lib64
)
find_library(UUID_LIBRARY_DEBUG
NAMES
uuidd
HINTS
${UUID_DIR}/lib
$ENV{UUID_DIR}/lib
$ENV{UUID_DIR}
${DELTA3D_EXT_DIR}/lib
$ENV{DELTA_ROOT}/ext/lib
$ENV{DELTA_ROOT}
$ENV{OSG_ROOT}/lib
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local/lib
/usr/lib
/sw/lib
/opt/local/lib
/opt/csw/lib
/opt/lib
/usr/freeware/lib64
)
if (NOT UUID_LIBRARY AND BSD)
set(UUID_LIBRARY "")
endif(NOT UUID_LIBRARY AND BSD)
set(UUID_INCLUDE_DIRS ${UUID_INCLUDE_DIR})
set(UUID_LIBRARIES ${UUID_LIBRARY})
if (UUID_INCLUDE_DIRS)
if (BSD OR UUID_LIBRARIES)
set(UUID_FOUND TRUE)
endif (BSD OR UUID_LIBRARIES)
endif (UUID_INCLUDE_DIRS)
if (UUID_FOUND)
if (NOT UUID_FIND_QUIETLY)
message(STATUS "Found UUID: ${UUID_LIBRARIES}")
endif (NOT UUID_FIND_QUIETLY)
else (UUID_FOUND)
if (UUID_FIND_REQUIRED)
message(FATAL_ERROR "Could not find UUID")
endif (UUID_FIND_REQUIRED)
endif (UUID_FOUND)
# show the UUID_INCLUDE_DIRS and UUID_LIBRARIES variables only in the advanced view
mark_as_advanced(UUID_INCLUDE_DIRS UUID_LIBRARIES)
endif (UUID_LIBRARIES AND UUID_INCLUDE_DIRS)

View File

@ -1,10 +1,8 @@
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()
link_directories(/usr/local/lib)
link_libraries(drogon trantor pthread dl)
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)
@ -32,9 +30,27 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -std=${CMAKE_CXX_STD_F
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})
link_libraries(${JSONCPP_LIBRARIES})
#uuid
find_package (UUID REQUIRED)
include_directories(${UUID_INCLUDE_DIR})
link_libraries(${UUID_LIBRARIES})
#OpenSSL
find_package (OpenSSL)
if(OpenSSL_FOUND)
link_libraries(${OPENSSL_LIBRARIES})
endif()
#zlib
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIR})
link_libraries(${ZLIB_LIBRARIES})
AUX_SOURCE_DIRECTORY(./ SRC_DIR)
AUX_SOURCE_DIRECTORY(controllers CTL_SRC)

View File

@ -89,6 +89,15 @@
"use_gzip": true,
//static_files_cache_time:5 (seconds) by default,the time in which static file response is cached,
//0 means cache forever,the negative value means no cache
"static_files_cache_time":5
"static_files_cache_time":5,
//simple_controllers_map:Configuring mapping from path to simple controller
"simple_controllers_map": [
{
"path": "/path/name",
"controller": "controllerClassName",
"http_methods": ["get","post"],
"filters": ["FilterClassName"]
}
]
}
}

View File

@ -1,7 +1,5 @@
link_libraries(drogon trantor uuid pthread jsoncpp dl z)
if(OpenSSL_FOUND)
link_libraries(ssl crypto)
endif()
link_libraries(drogon trantor pthread dl)
FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/simple_example/*.csp)
foreach(cspFile ${SCP_LIST})
message(STATUS "cspFile:" ${cspFile})

View File

@ -81,6 +81,55 @@ static void loadLogSetting(const Json::Value &log)
trantor::Logger::setLogLevel(trantor::Logger::WARN);
}
}
static void loadControllers(const Json::Value &controllers)
{
if (!controllers)
return;
for (auto controller : controllers)
{
auto path = controller.get("path", "").asString();
auto ctrlName = controller.get("controller", "").asString();
if (path == "" || ctrlName == "")
continue;
std::vector<any> constraints;
if (!controller["http_methods"].isNull())
{
for (auto method : controller["http_methods"])
{
auto strMethod = method.asString();
std::transform(strMethod.begin(), strMethod.end(), strMethod.begin(), tolower);
if (strMethod == "get")
{
constraints.push_back(Get);
}
else if (strMethod == "post")
{
constraints.push_back(Post);
}
else if (strMethod == "head") //The branch nerver work
{
constraints.push_back(Head);
}
else if (strMethod == "put")
{
constraints.push_back(Put);
}
else if (strMethod == "delete")
{
constraints.push_back(Delete);
}
}
}
if (!controller["filters"].isNull())
{
for (auto filter : controller["filters"])
{
constraints.push_back(filter.asString());
}
}
HttpAppFramework::instance().registerHttpSimpleController(path,ctrlName,constraints);
}
}
static void loadApp(const Json::Value &app)
{
if (!app)
@ -160,8 +209,9 @@ static void loadApp(const Json::Value &app)
HttpAppFramework::instance().enableSendfile(useSendfile);
auto useGzip = app.get("use_gzip", true).asBool();
HttpAppFramework::instance().enableGzip(useGzip);
auto staticFilesCacheTime=app.get("static_files_cache_time",5).asInt();
auto staticFilesCacheTime = app.get("static_files_cache_time", 5).asInt();
HttpAppFramework::instance().setStaticFilesCacheTime(staticFilesCacheTime);
loadControllers(app["simple_controllers_map"]);
}
static void loadListeners(const Json::Value &listeners)
{

View File

@ -1,7 +1,5 @@
link_libraries(drogon trantor uuid pthread jsoncpp dl z)
if(OpenSSL_FOUND)
link_libraries(ssl crypto)
endif()
link_libraries(drogon trantor pthread dl)
add_executable(cache_map_test CacheMapTest.cc)
add_executable(cache_map_test2 CacheMapTest2.cc)
add_executable(cookies_test CookiesTest.cc)

@ -1 +1 @@
Subproject commit d9252c3032468a1c2a8b8db5dc83142084d50fc0
Subproject commit 531fa9df4b81adfcd7d202f37455f31f0915728d