7 #ifndef __WEBSERVICE_H__
8 #define __WEBSERVICE_H__
10 #include <microhttpd.h>
15 template <
typename Functor>
22 daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
30 throw std::runtime_error(
"MHD_start_daemon");
34 MHD_stop_daemon(daemon);
41 static int on_request(
void * cls,
struct MHD_Connection * connection,
const char * url,
const char * method,
const char * version,
42 const char * upload_data,
size_t * upload_data_size,
46 template <
typename Functor>
48 struct MHD_Connection * connection,
52 const char * upload_data,
53 size_t * upload_data_size,
58 if (std::string(
"GET") != method)
67 if (0 != *upload_data_size)
72 struct MHD_Response * response;
76 const std::string& data = webservice->functor(url);
78 response = MHD_create_response_from_buffer (data.size(),
79 const_cast<char*
>(data.c_str()),
80 MHD_RESPMEM_MUST_COPY);
81 MHD_add_response_header(response,
"Content-Type",
"application/json");
82 MHD_add_response_header(response,
"Access-Control-Allow-Origin",
"*");
84 ret = MHD_queue_response(connection,
87 }
catch (
const std::exception& e) {
88 const std::string& data = e.what();
89 response = MHD_create_response_from_buffer (data.size(),
90 const_cast<char*
>(data.c_str()),
91 MHD_RESPMEM_MUST_COPY);
92 MHD_add_response_header(response,
"Content-Type",
"text");
94 ret = MHD_queue_response(connection,
99 MHD_destroy_response(response);
Definition: webservice.hpp:16