Skip to content

Commit

Permalink
Actually working service
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-san committed Aug 11, 2020
1 parent 52a30c8 commit db594d4
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 22 deletions.
104 changes: 104 additions & 0 deletions Reploid/Reploid.vcxproj

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions Reploid/Reploid.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,29 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="rest_controller\common.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="rest_controller\controller.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="rest_controller\Endpoint.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="rest_controller\network_util.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="rest_controller\controller.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="rest_controller\network_util.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Reploid/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
<package id="boost_wserialization-vc142" version="1.72.0.0" targetFramework="native" />
<package id="boost_zlib-vc142" version="1.72.0.0" targetFramework="native" />
<package id="boost-vc142" version="1.72.0.0" targetFramework="native" />
<package id="cpprestsdk.v141" version="2.10.12.1" targetFramework="native" />
<package id="cpprestsdk.v142" version="2.10.15" targetFramework="native" />
</packages>
3 changes: 3 additions & 0 deletions Reploid/rest_controller/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>

#define _TURN_OFF_PLATFORM_STRING
#include <cpprest/asyncrt_utils.h>

#include <string>
#include <iostream>
#include <vector>
Expand Down
18 changes: 13 additions & 5 deletions Reploid/rest_controller/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace reploid {
{
}

/*void Controller::setEndpoint(const std::string& value) {
void Controller::setEndpoint(const utility::string_t& value) {
uri endpointURI(value);
uri_builder endpointBuilder;

endpointBuilder.set_scheme(endpointURI.scheme());
if (endpointURI.host() == "host_auto_ip4") {
if (endpointURI.host() == _XPLATSTR("host_auto_ip4")) {
endpointBuilder.set_host(NetworkUtils::hostIP4());
}
else if (endpointURI.host() == "host_auto_ip6") {
else if (endpointURI.host() == _XPLATSTR("host_auto_ip6")) {
endpointBuilder.set_host(NetworkUtils::hostIP6());
}
endpointBuilder.set_port(endpointURI.port());
Expand All @@ -28,9 +28,9 @@ namespace reploid {
_listener = http_listener(endpointBuilder.to_uri());
}

std::string Controller::endpoint() const {
utility::string_t Controller::endpoint() const {
return _listener.uri().to_string();
}*/
}

pplx::task<void> Controller::accept() {
initRestOpHandlers();
Expand All @@ -41,6 +41,14 @@ namespace reploid {
return _listener.close();
}

void Controller::handleGet(http_request message)
{
auto response = json::value::object();
response[_XPLATSTR("version")] = json::value::string(_XPLATSTR("0.1.1"));
response[_XPLATSTR("status")] = json::value::string(_XPLATSTR("ready!"));
message.reply(status_codes::OK, response);
}

std::vector<utility::string_t> Controller::requestPath(const http_request& message) {
auto relativePath = uri::decode(message.relative_uri().path());
return uri::split_path(relativePath);
Expand Down
13 changes: 11 additions & 2 deletions Reploid/rest_controller/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


#include <string>
#include "common.hpp"
#include <cpprest/http_listener.h>
#include <cpprest/http_msg.h>
#undef U

#include <pplx/pplxtasks.h>

using namespace web;
Expand All @@ -22,10 +23,18 @@ namespace reploid {
Controller();
~Controller();

void setEndpoint(const utility::string_t& value);
utility::string_t endpoint() const;

pplx::task<void> accept();
pplx::task<void> shutdown();

virtual void initRestOpHandlers() = 0;
virtual void initRestOpHandlers()
{
_listener.support(methods::GET, std::bind(&Controller::handleGet, this, std::placeholders::_1));
}

void handleGet(http_request message);


std::vector<utility::string_t> requestPath(const http_request& message);
Expand Down
15 changes: 8 additions & 7 deletions Reploid/rest_controller/network_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ HostInetInfo NetworkUtils::queryHostInetInfo() {
return resolver.resolve(query);
}

std::string NetworkUtils::hostIP(unsigned short family) {
utility::string_t NetworkUtils::hostIP(unsigned short family) {
auto hostInetInfo = queryHostInetInfo();
tcp::resolver::iterator end;
while (hostInetInfo != end) {
tcp::endpoint ep = *hostInetInfo++;
sockaddr sa = *ep.data();
if (sa.sa_family == family) {
return ep.address().to_string();
return utility::conversions::to_string_t(ep.address().to_string());
}
}
return nullptr;
}

std::string NetworkUtils::hostIP4()
utility::string_t NetworkUtils::hostIP4()
{
return hostIP(AF_INET);
return _XPLATSTR("localhost");
//hostIP(AF_INET);
}

std::string NetworkUtils::hostIP6()
utility::string_t NetworkUtils::hostIP6()
{
return hostIP(AF_INET6);
}

std::string NetworkUtils::hostName()
utility::string_t NetworkUtils::hostName()
{
return host_name();
return utility::conversions::to_string_t(host_name());
}
8 changes: 4 additions & 4 deletions Reploid/rest_controller/network_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace reploid {

private:
static HostInetInfo queryHostInetInfo();
static std::string hostIP(unsigned short family);
static utility::string_t hostIP(unsigned short family);

public:
// gets the host IP4 string formatted
static std::string hostIP4();
static utility::string_t hostIP4();

// gets the host IP6 string formatted
static std::string hostIP6();
static utility::string_t hostIP6();

static std::string hostName();
static utility::string_t hostName();
};

}
27 changes: 24 additions & 3 deletions ReploidTestService/ReploidTestService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@

#include <iostream>
#include "rest_controller/controller.hpp"
#include "user_interrupt_handler.h"

int main()
{
std::cout << "Hello World!\n";
int main(int argc, const char* argv[]) {
reploid::InterruptHandler::hookSIGINT();

reploid::Controller server;
server.setEndpoint(_XPLATSTR("http://host_auto_ip4:6502/kaiser/api"));

try {
// wait for server initialization...
server.accept().wait();
std::cout << "Modern C++ Microservice now listening for requests at: " << utility::conversions::to_utf8string(server.endpoint()) << '\n';

reploid::InterruptHandler::waitForUserInterrupt();

server.shutdown().wait();
}
catch (std::exception& e) {
std::cerr << "something wrong happen! :(" << '\n';
std::cerr << e.what() << '\n';
}
catch (...) {
}

return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
Expand Down
3 changes: 3 additions & 0 deletions ReploidTestService/ReploidTestService.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="user_interrupt_handler.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="..\packages\cpprestsdk.v141.2.10.12.1\build\native\cpprestsdk.v141.targets" Condition="Exists('..\packages\cpprestsdk.v141.2.10.12.1\build\native\cpprestsdk.v141.targets')" />
Expand Down
5 changes: 5 additions & 0 deletions ReploidTestService/ReploidTestService.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="user_interrupt_handler.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
File renamed without changes.

0 comments on commit db594d4

Please sign in to comment.