Skip to content

Instantly share code, notes, and snippets.

@Valken
Last active May 22, 2016 00:05
Show Gist options
  • Save Valken/e8482c98dfb78d010c2f8b073149368f to your computer and use it in GitHub Desktop.
Save Valken/e8482c98dfb78d010c2f8b073149368f to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 3.0)
project(CasablancaRestServer CXX)
find_package(Boost)
find_package(OpenSSL)
set(CASABLANCA_INCLUDE_DIR "" CACHE PATH "")
set(CASABLANCA_LIB "" CACHE FILEPATH "")
include_directories(
${Boost_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
${CASABLANCA_INCLUDE_DIR})
if (MSVC OR APPLE)
file(GLOB HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")
source_group("Header Files" FILES ${HEADERS})
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
add_definitions(-std=c++14)
endif()
add_executable(restserver ${HEADERS} RestServer.cpp)
target_link_libraries(restserver ${CASABLANCA_LIB})
#include "cpprest/http_listener.h"
#include <regex>
int main()
{
web::uri_builder url(U("http://localhost:9999/"));
web::http::experimental::listener::http_listener listener(url.to_uri().to_string());
listener.support([](web::http::http_request request)
{
std::regex queryRegex("/(\\w+)/(\\d+)");
std::smatch pieces_match;
auto path = web::http::uri::decode(request.relative_uri().path());
if (std::regex_match(path, pieces_match, queryRegex))
{
auto matches = web::json::value::array();
for (size_t i = 0; i < pieces_match.size(); ++i)
{
std::ssub_match sub_match = pieces_match[i];
std::string piece = sub_match.str();
matches[i] = web::json::value::string(piece);
}
auto result = web::json::value::object();
result[U("Status")] = web::json::value::string(utility::string_t(U("OK")));
result[U("Matches")] = matches;
request.reply(web::http::status_codes::OK, result);
}
else
{
auto notFound = web::json::value::object();
notFound[U("Status")] = web::json::value::string(U("NO OK"));
request.reply(web::http::status_codes::NotFound, notFound);
}
});
listener.open().wait();
std::cout << "Press ENTER to exit." << std::endl;
std::string line;
std::getline(std::cin, line);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment