forked from Pradumnasaraf/c-plus-plus-docker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathok_api.cpp
44 lines (36 loc) · 1.12 KB
/
ok_api.cpp
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
#include <iostream>
#include <thread>
#include <chrono>
#include <cpprest/http_listener.h>
#include <cpprest/json.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
class OkAPI {
public:
OkAPI(const std::string& address) : m_listener(address) {
m_listener.support(methods::GET, std::bind(&OkAPI::handle_get, this, std::placeholders::_1));
}
void start() {
m_listener.open().then([this]() {
std::cout << "Listening on: " << m_listener.uri().to_string() << std::endl;
}).wait();
}
void handle_get(http_request request) {
ucout << "Received GET request" << std::endl;
json::value response;
response[U("message")] = json::value::string(U("OK"));
request.reply(status_codes::OK, response);
}
private:
http_listener m_listener;
};
int main() {
const std::string address = "http://0.0.0.0:8080"; // Listen on all interfaces
OkAPI api(address);
api.start();
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1)); // Sleep to prevent busy loop
}
return 0;
}