From d19fb7b82d874de586a0af4984b28e32624d68ce Mon Sep 17 00:00:00 2001 From: lns Date: Sun, 8 Sep 2019 15:50:47 +0200 Subject: basic cpp web example using httplib and (j)inja --- CMakeLists.txt | 22 ++++++++++++++++++ src/main.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4967c8a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 2.8.9) +project(cpp-web) + +set(CMAKE_CXX_FLAGS "-Wall -Wextra") +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-O3 -fomit-frame-pointer -flto") + +set(CPP_HTTPLIB_SRCDIR "${PROJECT_SOURCE_DIR}/deps/cpp-httplib" CACHE STRING "Path to the cpp-httplib source directory.") +if(NOT EXISTS "${CPP_HTTPLIB_SRCDIR}/httplib.h") + message(FATAL_ERROR "cpp-httplib missing") +endif() + +set(INJA_SRCDIR "${PROJECT_SOURCE_DIR}/deps/inja" CACHE STRING "Path to the inja source directory.") +if(NOT EXISTS "${INJA_SRCDIR}/single_include/inja/inja.hpp") + message(FATAL_ERROR "inja missing") +endif() + +include_directories(${CPP_HTTPLIB_SRCDIR} ${INJA_SRCDIR}/single_include ${INJA_SRCDIR}/third_party/include) +file(GLOB SOURCES "src/*.cpp") +add_executable(cpp-web ${SOURCES}) +#target_compile_definitions(cpp-web PUBLIC CPPHTTPLIB_THREAD_POOL_COUNT=4) +target_link_libraries(cpp-web pthread) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..3bcd0dd --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,71 @@ +#include + +#include +#include + + +void example_inja_render(const httplib::Request& req, httplib::Response& res) +{ + inja::Environment env; + nlohmann::json data; + data["name"] = "Peter"; + data["city"] = "Brunswick"; + data["age"] = 29; + data["names"] = {"Jeff", "Seb"}; + data["brother"]["name"] = "Chris"; + data["brother"]["daughters"] = {"Maria", "Helen"}; + data["brother"]["daughter0"] = { { "name", "Maria" } }; + data["is_happy"] = true; + data["is_sad"] = false; + data["relatives"]["mother"] = "Maria"; + data["relatives"]["brother"] = "Chris"; + data["relatives"]["sister"] = "Jenny"; + data["vars"] = {2, 3, 4, 0, -1, -2, -3}; + + res.set_content( + env.render("\n" + "Hello {{ name }}! I come from {{ city }}.
\n" + "Hello {{ names.1 }}!
\n" + "Hello {{ brother.name }}!
\n" + "Hello {{ brother.daughter0.name }}!
\n" + "{{ \"{{ no_value }}\" }}
\n" + "Hello{# This is a comment #}!
\n" + "{# --- #Todo --- #}
\n" + "{% for name in names %}a{% endfor %}
\n" + "Hello {% for name in names %}{{ name }} {% endfor %}!
\n" + "Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% endfor %}!
\n" + "{% for type, name in relatives %}{{ type }}: {{ name }}, {% endfor %}
\n" + "{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}
\n" + "{% for name in names %}{{ loop.index }}: {{ name }}{% if not loop.is_last %}, {% endif %}{% endfor %}!
\n" + "{% for name in names %}{{ loop.index }}: {{ name }}{% if loop.is_last == false %}, {% endif %}{% endfor %}!
\n" + "{% for name in {} %}a{% endfor %}
\n" + "\n", + data), "text/html"); +} + +bool setup_httplib(const char * const addr, uint16_t port) +{ + using namespace httplib; + + Server svr; + + svr.Get("/", [](const Request& req, Response& res) { + example_inja_render(req, res); + }); + + svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { + auto numbers = req.matches[1]; + res.set_content(numbers, "text/plain"); + }); + + svr.Get("/stop", [&](const Request& req, Response& res) { + svr.stop(); + }); + + return svr.listen(addr, port); +} + +int main(int argc, char **argv) +{ + setup_httplib("127.0.0.1", 8080); +} -- cgit v1.2.3