diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2021-04-27 13:45:54 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2021-04-27 13:45:54 +0200 |
commit | 51d779256c74cc541da306db5629ed510df5a944 (patch) | |
tree | ec23431d0b9b3dac2a2a8ac8f7201e9da9c58632 /src/main.cpp | |
parent | da307fb4c6b5028d50c09dec7265ce8715e035d5 (diff) |
Switched from slow blocking cpp-httplib to libevent2.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 126 |
1 files changed, 66 insertions, 60 deletions
diff --git a/src/main.cpp b/src/main.cpp index 3bcd0dd..9b38c38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,71 +1,77 @@ -#include <iostream> - -#include <httplib.h> -#include <inja/inja.hpp> +#include "EventManager.hpp" +#include <event2/buffer.h> -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}; +#include <inja/inja.hpp> +#include <iostream> - 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"); -} +static void example_inja_render(struct evhttp_request *const req, + EvUserData ud) { + (void)ud; -bool setup_httplib(const char * const addr, uint16_t port) -{ - using namespace httplib; + 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}; - Server svr; + auto reply = 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 names %}a{% endfor %}<br>\n" + "</body></html>\n", + data); - svr.Get("/", [](const Request& req, Response& res) { - example_inja_render(req, res); - }); + evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", + "text/html"); - svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) { - auto numbers = req.matches[1]; - res.set_content(numbers, "text/plain"); - }); + struct evbuffer *const output = evbuffer_new(); + if (output != nullptr) { + evbuffer_add(output, reply.c_str(), reply.size()); + evhttp_send_reply(req, 200, "OK", output); + evbuffer_free(output); + } +} - svr.Get("/stop", [&](const Request& req, Response& res) { - svr.stop(); - }); +int main(int argc, char **argv) { + char const *host = "127.0.0.1"; + uint16_t port = 9000; - return svr.listen(addr, port); -} + if (argc > 1) { + host = argv[1]; + } + if (argc > 2) { + port = atoi(argv[2]); + } -int main(int argc, char **argv) -{ - setup_httplib("127.0.0.1", 8080); + EventManager evmgr; + evmgr.setDefaultCallback(example_inja_render, {}); + evmgr.Init(host, port); } |