1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
// Copyright Maarten L. Hekkelman, 2022-2025
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//[ simple_http_server
#include <zeep/http/controller.hpp>
#include <zeep/http/reply.hpp>
#include <zeep/http/server.hpp>
#include <exception>
#include <iostream>
#include <string>
class hello_controller : public zeep::http::controller
{
public:
/* Specify the root path as prefix, will handle any request URI */
hello_controller()
: controller("/")
{
}
bool handle_request([[maybe_unused]] zeep::http::request &req, zeep::http::reply &rep) override
{
/* Construct a simple reply with status OK (200) and content string */
rep = zeep::http::reply::stock_reply(zeep::http::status_type::ok);
rep.set_content("Hello", "text/plain");
return true;
}
};
int main()
{
try
{
zeep::http::server srv;
srv.add_controller(new hello_controller());
srv.bind("::", 8080);
srv.run(2);
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << '\n';
}
return 0;
}
//]
|