Skip to content

Commit

Permalink
[#28][WIP] cpprest
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
  • Loading branch information
Clovel committed Apr 5, 2020
1 parent ba932c2 commit d0271bc
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 1 deletion.
5 changes: 4 additions & 1 deletion generator/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#
# -----------------------------------------------------------------------------

# Subdirectories ------------------------------------------
add_subdirectory(rest)

# Header files --------------------------------------------
file(GLOB PUBLIC_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/../inc/*.h
Expand Down Expand Up @@ -54,7 +57,7 @@ add_dependencies(${CMAKE_PROJECT_NAME}
target_link_libraries(${CMAKE_PROJECT_NAME}
initools
)
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
)

Expand Down
7 changes: 7 additions & 0 deletions generator/src/rest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Copyright (C) 2020 Clovis Durand
#
# -----------------------------------------------------------------------------

# Sub-directories -----------------------------------------
add_subdirectory(src)
41 changes: 41 additions & 0 deletions generator/src/rest/inc/RESTServer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @brief RESTServer class
*
* @file RESTServer.hpp
*/

#ifndef RESTSERVER_HPP
#define RESTSERVER_HPP

/* Includes -------------------------------------------- */
#include <cpprest/http_listener.h>

/* Defines --------------------------------------------- */

/* Type definitions ------------------------------------ */

/* Forward declarations -------------------------------- */

/* RESTServer class ------------------------------------ */
class RESTServer {
public:
/* Contructors */
RESTServer();
RESTServer(utility::string_t pURL);

pplx::task<void> open(void) {
return mListener.open();
}
pplx::task<void> close(void) {
return mListener.close();
}
protected:
static void handleGet(web::http::http_request pMsg);
static void handlePut(web::http::http_request pMsg);
static void handlePost(web::http::http_request pMsg);
static void handleDelete(web::http::http_request pMsg);
private:
web::http::experimental::listener::http_listener mListener;
};

#endif /* RESTSERVER_HPP */
69 changes: 69 additions & 0 deletions generator/src/rest/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# Copyright (C) 2020 Clovis Durand
#
# -----------------------------------------------------------------------------

# Header files --------------------------------------------
file(GLOB PUBLIC_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/../inc/*.h
${CMAKE_CURRENT_SOURCE_DIR}/../inc/*.hpp
)

file(GLOB_RECURSE MODULE_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/*.hpp
)

set(HEADERS
${PUBLIC_HEADERS}
${MODULE_HEADERS}
)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../inc/)

# Source files --------------------------------------------
file(GLOB SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/*.c
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
)

# Link directories ----------------------------------------

# Target definition ---------------------------------------
list(REMOVE_ITEM SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)

add_library(cpprest-api SHARED
${SOURCES}
)
target_link_libraries(cpprest-api
cpprestsdk::cpprest
)
set_target_properties(cpprest-api PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
)

add_executable(cpprest-api-test
main.cpp
)
add_dependencies(cpprest-api-test
cpprest-api
)
target_link_libraries(cpprest-api-test
cpprestsdk::cpprest
cpprest-api
)

#----------------------------------------------------------------------------
# The installation is prepended by the CMAKE_INSTALL_PREFIX variable
install(TARGETS cpprest-api
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
install(TARGETS cpprest-api-test
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
110 changes: 110 additions & 0 deletions generator/src/rest/src/RESTServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @brief RESTServer class implementation
*
* @file RESTServer.cpp
*/

/* Includes -------------------------------------------- */
#include "RESTServer.hpp"

/* C++ system */
#include <iostream>

/* Defines --------------------------------------------- */

/* Type definitions ------------------------------------ */

/* Forward declarations -------------------------------- */

/* RESTServer class implementation --------------------- */
/* Contructors */
RESTServer::RESTServer() {
/* Empty */
}

RESTServer::RESTServer(utility::string_t pURL) : mListener(pURL)
{
std::function<void(web::http::http_request)> lGetFct = &RESTServer::handleGet;
mListener.support(web::http::methods::GET, lGetFct);

std::function<void(web::http::http_request)> lPutFct = &RESTServer::handlePut;
mListener.support(web::http::methods::PUT, lPutFct);

std::function<void(web::http::http_request)> lPostFct = &RESTServer::handlePost;
mListener.support(web::http::methods::POST, lPostFct);

std::function<void(web::http::http_request)> lDelFct = &RESTServer::handleDelete;
mListener.support(web::http::methods::DEL, lDelFct);
}

void RESTServer::handleGet(web::http::http_request pMsg) {
std::cout << "[INFO ] <RESTServer::handleGet> Msg : " << std::endl << pMsg.to_string() << std::endl;
std::cout << "[INFO ] <RESTServer::handleGet> Relative URI : " << std::endl << pMsg.relative_uri().to_string() << std::endl;
std::cout << "[INFO ] <RESTServer::handleGet> Decoded relative URI : " << std::endl << web::uri::decode(pMsg.relative_uri().path()) << std::endl;

std::cout << "[DEBUG] <RESTServer::handleGet> pMsg.relative_uri().path() = " << pMsg.relative_uri().path() << std::endl;
std::cout << "[DEBUG] <RESTServer::handleGet> Decoded pMsg.relative_uri().path() = " << web::uri::decode(pMsg.relative_uri().path()) << std::endl;
auto lPaths = web::uri::split_path(web::uri::decode(pMsg.relative_uri().path()));
if(lPaths.empty()) {
std::cout << "[DEBUG] <RESTServer::handleGet> lPaths is empty" << std::endl;
// pMsg.reply(web::http::status_codes::OK);
// return;
} else {
for(auto lIt1 = lPaths.begin(); lIt1 != lPaths.end(); lIt1++) {
std::cout << U("Path") << U(" ") << *lIt1 << std::endl;
}
}

std::cout << "[DEBUG] <RESTServer::handleGet> pMsg.relative_uri().query() = " << pMsg.relative_uri().query() << std::endl;
std::cout << "[DEBUG] <RESTServer::handleGet> Decoded pMsg.relative_uri().query() = " << web::uri::decode(pMsg.relative_uri().query()) << std::endl;
#if 0
auto lQuery = web::uri::split_query(web::uri::decode(pMsg.relative_uri().query()));
if(lQuery.empty()) {
std::cout << "[DEBUG] <RESTServer::handleGet> lQuery is empty" << std::endl;
pMsg.reply(web::http::status_codes::OK);
return;
} else {
for(auto lIt2 = lQuery.begin(); lIt2 != lQuery.end(); lIt2++) {
std::cout << U("Query") << U(" ") << lIt2->second << std::endl;
}
}

auto lQueryLtr = lQuery.find(U("request"));
std::cout << U("lQueryLtr") << U(" ") << lQueryLtr->first << std::endl;
utility::string_t lRequest = lQueryLtr->second;
std::cout << U("Request") << U(" ") << lRequest << std::endl;
#else
utility::string_t lRequest = web::uri::decode(pMsg.relative_uri().query());
#endif

if(U("get_developers") == lRequest) {
/* Build JSON */
web::json::value lResult = web::json::value::object();
lResult[U("name")] = web::json::value::string(U("Clovis Durand"));
lResult[U("age")] = web::json::value::string(U("24"));

/* Serialize JSON */
utility::string_t lResponse = lResult.serialize();
std::cout << "[DEBUG] <RESTServer::handleGet> Response is : " << std::endl << lResponse << std::endl;

pMsg.reply(web::http::status_codes::OK, lResult);
return;
}

pMsg.reply(web::http::status_codes::OK);
}

void RESTServer::handlePut(web::http::http_request pMsg) {
std::cout << pMsg.to_string() << std::endl;
pMsg.reply(web::http::status_codes::OK);
}

void RESTServer::handlePost(web::http::http_request pMsg) {
std::cout << pMsg.to_string() << std::endl;
pMsg.reply(web::http::status_codes::OK);
}

void RESTServer::handleDelete(web::http::http_request pMsg) {
std::cout << pMsg.to_string() << std::endl;
pMsg.reply(web::http::status_codes::OK);
}
64 changes: 64 additions & 0 deletions generator/src/rest/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @brief C++ REST API test main source file
*
* @file main.cpp
*/

/* Includes -------------------------------------------- */
#include "RESTServer.hpp"

/* C++ system */
#include <iostream>
#include <string>

/* C system */
#include <cstring>

/* Defines --------------------------------------------- */
#define ADDRESS "http://localhost"

/* Notes ----------------------------------------------- */

/* Variable declaration -------------------------------- */
std::unique_ptr<RESTServer> gRESTServer;

/* Type definitions ------------------------------------ */

/* Support functions ----------------------------------- */
static void printUsage(const char * const pProgName)
{
std::cout << "[USAGE] %s" << pProgName << std::endl;
std::cout << " <arg1> : Localhost port number" << std::endl;
}

/* ----------------------------------------------------- */
/* Main tests ------------------------------------------ */
/* ----------------------------------------------------- */
int main(const int argc, const char * const * const argv) {
if ((argc < 2) || (std::strcmp(argv[1U], "--help") == 0)) {
printUsage(argv[0]);
return EXIT_FAILURE;
}

utility::string_t lPort = U(argv[1U]);
utility::string_t lAddr = U(ADDRESS);
lAddr.append(U(":"));
lAddr.append(lPort);

web::uri_builder lURI(lAddr);
lURI.append_path(U("OSCO-OD-Gen/Action"));

lAddr = lURI.to_uri().to_string();

gRESTServer = std::unique_ptr<RESTServer>(new RESTServer(lAddr));
gRESTServer->open().wait();

std::cout << "[INFO] Listening for request at: " << lAddr << std::endl;

std::string lLine;
std::getline(std::cin, lLine);

gRESTServer->close().wait();

return EXIT_SUCCESS;
}

0 comments on commit d0271bc

Please sign in to comment.