Implement HTTP server using libevent #include "fmt/format.h"
#include &lt;cstdint&gt;
#include &lt;cstdio&gt;
#include &lt;event2/event-config.h&gt;
#include &lt;event2/util.h&gt;
#include &lt;evhttp.h&gt;
#include &lt;memory&gt;
#include &lt;type_traits&gt;
auto
main() -&gt; int
{
    constexpr char host[] = "127.0.0.1";
    std::uint16_t port = 5555;

    using event_base_new_type
        = std::pointer_traits&lt;decltype(event_base_new())&gt;::element_type;
    using event_base_deleter_type = decltype(&amp;event_base_free);
    auto event_base
        = std::unique_ptr&lt;event_base_new_type, event_base_deleter_type&gt;(
            event_base_new(), &amp;event_base_free);

    if(!(event_base))
    {
        fmt::print(stderr, "Failed to init libevent.\n");
        return -1;
    }

    using evhttp_new_type
        = std::pointer_traits&lt;decltype(evhttp_new(event_base.get()))&gt;::element_type;
    using evhttp_deleter_type = decltype(&amp;evhttp_free);
    auto http = std::unique_ptr&lt;evhttp_new_type, evhttp_deleter_type&gt;(evhttp_new(event_base.get()), &amp;evhttp_free);

    auto handle = evhttp_bind_socket_with_handle(http.get(), host, port);
    if(!handle)
    {
        fmt::print(stderr, "Failed to init http server.\n");
        return -1;
    }
    auto callback = [](evhttp_request* req, void*) {
        auto* OutBuf = evhttp_request_get_output_buffer(req);
        if(!OutBuf)
            return;
        evbuffer_add_printf(OutBuf,
            "&lt;html&gt;&lt;body&gt;&lt;center&gt;&lt;h1&gt;Hello World!&lt;/h1&gt;&lt;/center&gt;&lt;/body&gt;&lt;/html&gt;");
        evhttp_send_reply(req, HTTP_OK, "", OutBuf);
    };
    evhttp_set_gencb(http.get(), callback, nullptr);
    if(event_base_dispatch(event_base.get()) == -1)
    {
        fmt::print(stderr, "Failed to run messahe loop.\n");
        return -1;
    }

    return 0;
}


Compile

g++ a.cpp -lfmt -levent


Run

$ ./a.out
$ ab -c 1000 -k -r -t 10 http://127.0.0.1:5555/


Open http://127.0.0.1:5555/ in browser

What I want to know:


Is my unique_ptr necessary? valgrind reports no memory leak even though I don't use unique_ptr nor free manually.
more elegant/correct code.
Not too much to review as the code uses evhttp to handle HTTP and provide a basic, static reply for each request.

Why is host constexpr, but not the port?

All those using statements, combined with traits and decltype one after the other makes the code very hard to read, almost obscuring the fact that evhttp is being initialized at that point. I would try to rewrite that part in a simpler way.

Why auto main() -&gt; int instead of the simple, classic int main()?