diff options
author | lns <matzeton@googlemail.com> | 2019-09-08 15:50:47 +0200 |
---|---|---|
committer | lns <matzeton@googlemail.com> | 2019-09-08 15:50:47 +0200 |
commit | d19fb7b82d874de586a0af4984b28e32624d68ce (patch) | |
tree | 5fb4e997e1080dd59ed0bff75b266f30de64a655 | |
parent | 9c8fa0d4bf64138eb8a8f15a4e422d5b8081b9e3 (diff) |
basic cpp web example using httplib and (j)inja
-rw-r--r-- | CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/main.cpp | 71 |
2 files changed, 93 insertions, 0 deletions
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 <iostream> + +#include <httplib.h> +#include <inja/inja.hpp> + + +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("<html><body>\n" + "Hello {{ name }}! I come from {{ city }}.<br>\n" + "Hello {{ names.1 }}!<br>\n" + "Hello {{ brother.name }}!<br>\n" + "Hello {{ brother.daughter0.name }}!<br>\n" + "{{ \"{{ no_value }}\" }}<br>\n" + "Hello{# This is a comment #}!<br>\n" + "{# --- #Todo --- #}<br>\n" + "{% for name in names %}a{% endfor %}<br>\n" + "Hello {% for name in names %}{{ name }} {% endfor %}!<br>\n" + "Hello {% for name in names %}{{ loop.index }}: {{ name }}, {% endfor %}!<br>\n" + "{% for type, name in relatives %}{{ type }}: {{ name }}, {% endfor %}<br>\n" + "{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}<br>\n" + "{% for name in names %}{{ loop.index }}: {{ name }}{% if not loop.is_last %}, {% endif %}{% endfor %}!<br>\n" + "{% for name in names %}{{ loop.index }}: {{ name }}{% if loop.is_last == false %}, {% endif %}{% endfor %}!<br>\n" + "{% for name in {} %}a{% endfor %}<br>\n" + "</body></html>\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); +} |