diff --git a/apps/master/AdminRest.cpp b/apps/master/AdminRest.cpp new file mode 100644 index 000000000..74b93e46d --- /dev/null +++ b/apps/master/AdminRest.cpp @@ -0,0 +1,67 @@ +// +// Created by koncord on 04.09.17. +// + +#include "AdminRest.hpp" + +#include +#include + +#include "RestUtils.hpp" + +using namespace std; +using namespace chrono; +using namespace boost::property_tree; + + +AdminRest::AdminRest(const std::string &cert, const std::string &key, const std::string &verifyFile, + unsigned short port, std::shared_ptr master) : httpServer(cert, key, verifyFile), master(master) +{ + httpServer.config.port = port; +} + +void AdminRest::start() +{ + static const string AdminArea = "^/api/admin?"; + + httpServer.resource[AdminArea]["POST"] = [this](auto response, auto request) { + cout << request->method << endl; + cout << request->path << endl; + cout << request->http_version << endl; + + for (auto &header : request->header) + cout << header.first << ": " << header.second << endl; + + string resp; + master->luaStuff([&request, &response, &resp](sol::state &state) { + sol::protected_function func = state["OnAdminRequest"]; + + sol::protected_function_result result = func.call(request->remote_endpoint_address, request->content.string()); + if (result.valid()) + *response << result.get(); + else + { + cerr << "Error: " << result.get() << endl; + *response << response500; + } + }); + + }; + + /*httpServer.on_error = [](auto request, const boost::system::error_code& err) + { + std::cerr << "Error: " << err.message() << " " << err.category().name() << std::endl; + };*/ + + httpServer.default_resource["GET"] = [](auto response, auto /*request*/) { + cout << "Default request" << endl; + *response << response400; + }; + + httpServer.start(); +} + +void AdminRest::stop() +{ + httpServer.stop(); +} diff --git a/apps/master/AdminRest.hpp b/apps/master/AdminRest.hpp new file mode 100644 index 000000000..26a853f45 --- /dev/null +++ b/apps/master/AdminRest.hpp @@ -0,0 +1,24 @@ +// +// Created by koncord on 04.09.17. +// + +#pragma once + +#include "SimpleWeb/https_server.hpp" +#include "MasterServer.hpp" + +typedef SimpleWeb::Server HttpsServer; + +class AdminRest +{ +public: + AdminRest(const std::string &cert, const std::string &key, const std::string &verifyFile, unsigned short port, std::shared_ptr master); + void start(); + void stop(); + +private: + HttpsServer httpServer; + std::shared_ptr master; +}; + + diff --git a/apps/master/CMakeLists.txt b/apps/master/CMakeLists.txt index 22a42a936..204dfa12c 100644 --- a/apps/master/CMakeLists.txt +++ b/apps/master/CMakeLists.txt @@ -3,12 +3,15 @@ project(masterserver) #set(CMAKE_CXX_STANDARD 14) add_definitions(-std=gnu++14) -include_directories("./") +find_package(LuaJit REQUIRED) +find_package(OpenSSL REQUIRED) -set(SOURCE_FILES main.cpp MasterServer.cpp MasterServer.hpp RestServer.cpp RestServer.hpp) +include_directories("./" ${LUAJIT_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/extern/sol ${OPENSSL_INCLUDE_DIR}) + +set(SOURCE_FILES main.cpp MasterServer.cpp MasterServer.hpp RestServer.cpp RestServer.hpp AdminRest.cpp) add_executable(masterserver ${SOURCE_FILES}) -target_link_libraries(masterserver ${RakNet_LIBRARY} components) +target_link_libraries(masterserver ${RakNet_LIBRARY} ${LUAJIT_LIBRARY} ${OPENSSL_LIBRARIES} components) option(BUILD_MASTER_TEST "build master server test program" OFF) diff --git a/apps/master/main.cpp b/apps/master/main.cpp index 0ca8d0e02..0639f3872 100644 --- a/apps/master/main.cpp +++ b/apps/master/main.cpp @@ -4,12 +4,14 @@ #include #include "MasterServer.hpp" #include "RestServer.hpp" +#include "AdminRest.hpp" using namespace RakNet; using namespace std; unique_ptr restServer; shared_ptr masterServer; +unique_ptr restAdminServer; int main(int argc, char* argv[]) { @@ -28,10 +30,30 @@ int main(int argc, char* argv[]) restServer = make_unique(restPort.as(), masterServer->GetServers()); + + sol::object restAdminCert = config["restAdminCert"]; + if (restAdminCert.get_type() != sol::type::string) + throw runtime_error("config.restAdminCert is not correct"); + + sol::object restAdminKey = config["restAdminKey"]; + if (restAdminKey.get_type() != sol::type::string) + throw runtime_error("config.restAdminKey is not correct"); + + sol::object restAdminVerifyFile = config["restAdminVerifyFile"]; + if (restAdminVerifyFile.get_type() != sol::type::string) + throw runtime_error("config.restAdminVerifyFile is not correct"); + + sol::object restAdminPort = config["restAdminPort"]; + if (restAdminPort.get_type() != sol::type::number) + throw runtime_error("config.restAdminPort is not correct"); + + restAdminServer = make_unique(restAdminCert.as(), restAdminKey.as(), + restAdminVerifyFile.as(), restAdminPort.as(), masterServer); }); auto onExit = [](int /*sig*/){ restServer->stop(); + restAdminServer->stop(); masterServer->luaStuff([](sol::state &state) { sol::protected_function func = state["OnExit"]; if (func.valid()) @@ -47,8 +69,10 @@ int main(int argc, char* argv[]) masterServer->Start(); thread rest_thread([]() { restServer->start();}); + thread restAdmin_thread([]() { restAdminServer->start();}); rest_thread.join(); + restAdmin_thread.join(); masterServer->Wait(); return 0;