Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Last active September 9, 2019 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngerakines/acb68f26e0eb67652651cde039671c09 to your computer and use it in GitHub Desktop.
Save ngerakines/acb68f26e0eb67652651cde039671c09 to your computer and use it in GitHub Desktop.
cpprestsdk demo
cmake_minimum_required(VERSION 3.7)
project(main)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(cpprestsdk_DIR /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/cmake/)
find_package(cpprestsdk CONFIG REQUIRED)
find_package(Boost COMPONENTS system REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE cpprestsdk::cpprest ${Boost_SYSTEM_LIBRARY})
FROM ubuntu as build
RUN apt-get update && apt-get install -y build-essential git cmake autoconf libtool pkg-config libcpprest-dev libcpprest
WORKDIR /src
COPY CMakeLists.txt main.cpp ./
RUN cmake . && make
FROM ubuntu
RUN apt-get update && apt-get install -y libcpprest
WORKDIR /app
COPY --from=build /src/main ./
CMD ["/app/main"]
// (c) 2019 Nick Gerakines
// This code is licensed under MIT license
#include <stdio.h>
#include <signal.h>
#include <iostream>
#include <thread>
#include <chrono>
#include "cpprest/asyncrt_utils.h"
#include "cpprest/http_listener.h"
using namespace std;
using namespace web;
using namespace http;
using namespace utility;
using namespace http::experimental::listener;
class Server
{
public:
Server();
pplx::task<void> open() { return m_listener.open(); }
pplx::task<void> close() { return m_listener.close(); }
private:
void handle_get(http_request message);
http_listener m_listener;
};
unique_ptr<Server> g_httpServer;
Server::Server() : m_listener(U("http://0.0.0.0:8080/"))
{
m_listener.support(methods::GET, bind(&Server::handle_get, this, placeholders::_1));
}
void Server::handle_get(http_request message)
{
message.reply(status_codes::OK, U("Hello world!"));
}
void on_initialize()
{
g_httpServer = unique_ptr<Server>(new Server());
g_httpServer->open().wait();
return;
}
void on_shutdown()
{
g_httpServer->close().wait();
return;
}
void signalHandler(int signum)
{
on_shutdown();
exit(signum);
}
int main()
{
on_initialize();
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
while (1)
{
this_thread::sleep_for(chrono::seconds(1));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment