-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
- Loading branch information
Showing
6 changed files
with
295 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# | ||
# Copyright (C) 2020 Clovis Durand | ||
# | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Sub-directories ----------------------------------------- | ||
add_subdirectory(src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |