mirror of https://github.com/CrowCpp/Crow.git
source resturcturing + CMake
This commit is contained in:
parent
a0c93f5b84
commit
031615ac86
|
@ -0,0 +1,23 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
project (crow_all)
|
||||
|
||||
find_package( Boost 1.40 COMPONENTS date_time filesystem system thread REQUIRED )
|
||||
#find_package( Tcmalloc )
|
||||
include_directories( ${Boost_INCLUDE_DIR} )
|
||||
|
||||
#set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
|
||||
|
||||
#set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
|
||||
#set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
|
||||
|
||||
set(PROJECT_INCLUDE_DIR
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${PROJECT_SOURCE_DIR}/http-parser
|
||||
)
|
||||
|
||||
include_directories("${PROJECT_INCLUDE_DIR}")
|
||||
include_directories("${PROJECT_SOURCE_DIR}")
|
||||
|
||||
add_subdirectory(src)
|
||||
add_subdirectory(tests)
|
||||
add_subdirectory(examples)
|
42
Makefile
42
Makefile
|
@ -1,42 +0,0 @@
|
|||
PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"')
|
||||
|
||||
ifeq ($(PLATFORM),darwin)
|
||||
FLAGS_BOOST_THREAD = -lboost_thread-mt
|
||||
else
|
||||
FLAGS_BOOST_THREAD = -lboost_thread
|
||||
FLAGS_DEBUG = -g
|
||||
FLAGS_GCOV = -r
|
||||
endif
|
||||
|
||||
binaries=covtest example example_chat
|
||||
|
||||
all: covtest example example_chat
|
||||
|
||||
example_chat: example_chat.cpp settings.h crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h logging.h mustache.h
|
||||
${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o example_chat example_chat.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/
|
||||
|
||||
|
||||
example: example.cpp settings.h crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h logging.h mustache.h
|
||||
${CXX} -Wall $(FLAGS_DEBUG) -O3 -std=c++1y -o example example.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -ltcmalloc_minimal -I http-parser/
|
||||
|
||||
test: covtest
|
||||
|
||||
runtest: example
|
||||
pkill example || exit 0
|
||||
./example &
|
||||
python test.py || exit 0
|
||||
pkill example
|
||||
|
||||
unittest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h
|
||||
${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o unittest unittest.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/
|
||||
./unittest
|
||||
|
||||
covtest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h mustache.h
|
||||
${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o covtest --coverage unittest.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/
|
||||
./covtest
|
||||
gcov $(FLAGS_GCOV) unittest.cpp
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
rm -f $(binaries) *.o
|
|
@ -0,0 +1,21 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
project (crow_examples)
|
||||
|
||||
add_executable(example example.cpp)
|
||||
target_link_libraries(example crow)
|
||||
target_link_libraries(example ${Boost_LIBRARIES} )
|
||||
set_target_properties(example PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y")
|
||||
|
||||
add_executable(example_chat example_chat.cpp)
|
||||
target_link_libraries(example_chat crow)
|
||||
target_link_libraries(example_chat ${Boost_LIBRARIES} )
|
||||
set_target_properties(example_chat PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y")
|
||||
message(${CMAKE_CURRENT_BINARY_DIR})
|
||||
message(${PROJECT_SOURCE_DIR})
|
||||
add_custom_command(OUTPUT example_chat.html
|
||||
#TARGET example_chat
|
||||
COMMAND ${CMAKE_COMMAND} -E
|
||||
copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html
|
||||
DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html
|
||||
)
|
||||
add_custom_target(example_chat_copy ALL DEPENDS example_chat.html)
|
|
@ -3,11 +3,12 @@
|
|||
#include "mustache.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<string> msgs;
|
||||
vector<crow::response*> ress;
|
||||
vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> ress;
|
||||
|
||||
void broadcast(const string& msg)
|
||||
{
|
||||
|
@ -16,9 +17,10 @@ void broadcast(const string& msg)
|
|||
x["msgs"][0] = msgs.back();
|
||||
x["last"] = msgs.size();
|
||||
string body = crow::json::dump(x);
|
||||
for(auto* res:ress)
|
||||
for(auto p:ress)
|
||||
{
|
||||
CROW_LOG_DEBUG << res->p << " replied: " << body;
|
||||
auto* res = p.first;
|
||||
CROW_LOG_DEBUG << res << " replied: " << body;
|
||||
res->end(body);
|
||||
}
|
||||
ress.clear();
|
||||
|
@ -39,8 +41,9 @@ int main()
|
|||
([]{
|
||||
CROW_LOG_INFO << "logs requested";
|
||||
crow::json::wvalue x;
|
||||
for(int i = max(0, (int)msgs.size()-100); i < (int)msgs.size(); i++)
|
||||
x["msgs"][i] = msgs[i];
|
||||
int start = max(0, (int)msgs.size()-100);
|
||||
for(int i = start; i < (int)msgs.size(); i++)
|
||||
x["msgs"][i-start] = msgs[i];
|
||||
x["last"] = msgs.size();
|
||||
CROW_LOG_INFO << "logs completed";
|
||||
return x;
|
||||
|
@ -61,8 +64,17 @@ int main()
|
|||
}
|
||||
else
|
||||
{
|
||||
ress.push_back(&res);
|
||||
CROW_LOG_DEBUG << res.p << " stored";
|
||||
vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> filtered;
|
||||
for(auto p : ress)
|
||||
{
|
||||
if (p.first->is_alive() && chrono::steady_clock::now() - p.second < chrono::seconds(30))
|
||||
filtered.push_back(p);
|
||||
else
|
||||
p.first->end();
|
||||
}
|
||||
ress.swap(filtered);
|
||||
ress.push_back({&res, chrono::steady_clock::now()});
|
||||
CROW_LOG_DEBUG << &res << " stored " << ress.size();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -74,7 +86,7 @@ int main()
|
|||
return "";
|
||||
});
|
||||
|
||||
app.port(18080)
|
||||
app.port(40080)
|
||||
//.multithreaded()
|
||||
.run();
|
||||
}
|
|
@ -30,9 +30,15 @@ $(document).ready(function(){
|
|||
var lastLog = data.last*1;
|
||||
console.log("lastLog: " + lastLog);
|
||||
var s = "";
|
||||
function htmlEncode(s)
|
||||
{
|
||||
return s.replace(/&(?!\w+([;\s]|$))/g, "&")
|
||||
.replace(/</g, "<").replace(/>/g, ">");
|
||||
}
|
||||
for(var x in data.msgs)
|
||||
{
|
||||
s += data.msgs[x] + "<BR>";
|
||||
|
||||
s = htmlEncode(data.msgs[x]) + "<BR>" + s;
|
||||
}
|
||||
$("#logs").html(s+$("#logs").html());
|
||||
var failFunction;
|
|
@ -98,6 +98,7 @@ namespace crow
|
|||
deadline_.cancel();
|
||||
auto self = this->shared_from_this();
|
||||
res.complete_request_handler_ = [self]{ self->complete_request(); };
|
||||
res.is_alive_helper_ = [this]()->bool{ return socket_.is_open(); };
|
||||
handler_->handle(req, res);
|
||||
}
|
||||
else
|
|
@ -73,9 +73,14 @@ namespace crow
|
|||
end();
|
||||
}
|
||||
|
||||
void* p;
|
||||
bool is_alive()
|
||||
{
|
||||
return is_alive_helper_ && is_alive_helper_();
|
||||
}
|
||||
|
||||
private:
|
||||
bool completed_{};
|
||||
std::function<void()> complete_request_handler_;
|
||||
std::function<bool()> is_alive_helper_;
|
||||
};
|
||||
}
|
|
@ -29,10 +29,45 @@ namespace crow
|
|||
|
||||
namespace json
|
||||
{
|
||||
void escape(const std::string& str, std::string& ret)
|
||||
{
|
||||
ret.reserve(ret.size() + str.size()+str.size()/4);
|
||||
for(char c:str)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
case '"': ret += "\\\""; break;
|
||||
case '\\': ret += "\\\\"; break;
|
||||
case '\n': ret += "\\n"; break;
|
||||
case '\b': ret += "\\b"; break;
|
||||
case '\f': ret += "\\f"; break;
|
||||
case '\r': ret += "\\r"; break;
|
||||
case '\t': ret += "\\t"; break;
|
||||
default:
|
||||
if (0 <= c && c < 0x20)
|
||||
{
|
||||
ret += "\\u00";
|
||||
auto to_hex = [](char c)
|
||||
{
|
||||
c = c&0xf;
|
||||
if (c < 10)
|
||||
return '0' + c;
|
||||
return 'a'+c-10;
|
||||
};
|
||||
ret += to_hex(c/16);
|
||||
ret += to_hex(c%16);
|
||||
}
|
||||
else
|
||||
ret += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::string escape(const std::string& str)
|
||||
{
|
||||
// TODO
|
||||
return str;
|
||||
std::string ret;
|
||||
escape(str, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
enum class type : char
|
||||
|
@ -1265,9 +1300,8 @@ namespace crow
|
|||
|
||||
void dump_string(const std::string& str, std::string& out)
|
||||
{
|
||||
// TODO escaping
|
||||
out.push_back('"');
|
||||
out += str;
|
||||
escape(str, out);
|
||||
out.push_back('"');
|
||||
}
|
||||
void dump_internal(const wvalue& v, std::string& out)
|
|
@ -0,0 +1,18 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
project (crow)
|
||||
|
||||
set(CROW_SRCS
|
||||
#${PROJECT_SOURCE_DIR}/some.cpp
|
||||
#${PROJECT_SOURCE_DIR}/someother.cpp
|
||||
${PROJECT_SOURCE_DIR}/../http-parser/http_parser.c
|
||||
)
|
||||
|
||||
set_source_files_properties(${PROJECT_SOURCE_DIR}/../http-parser/http_parser.c PROPERTIES LANGUAGE C )
|
||||
|
||||
include_directories("${PROJECT_BINARY_DIR}")
|
||||
include_directories("${PROJECT_INCLUDE_DIR}")
|
||||
|
||||
|
||||
add_library(${PROJECT_NAME} SHARED ${CROW_SRCS})
|
||||
#target_link_libraries(${PROJECT_NAME} tcmalloc)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y")
|
|
@ -0,0 +1,17 @@
|
|||
cmake_minimum_required(VERSION 2.8)
|
||||
project (crow_test)
|
||||
|
||||
|
||||
set(PROJECT_INCLUDE_DIR
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
${PROJECT_SOURCE_DIR}/http-parser
|
||||
)
|
||||
|
||||
set(TEST_SRCS
|
||||
unittest.cpp
|
||||
)
|
||||
|
||||
add_executable(test ${TEST_SRCS})
|
||||
target_link_libraries(test crow)
|
||||
target_link_libraries( test ${Boost_LIBRARIES} )
|
||||
set_target_properties(test PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y")
|
Loading…
Reference in New Issue