forked from mirror/openmw-tes3mp
[Master] Add Admin Rest Server
Add admin rest callback - "OnAdminRequest". Called on every POST request
This commit is contained in:
parent
b869fe0b76
commit
d1388cdf84
4 changed files with 121 additions and 3 deletions
67
apps/master/AdminRest.cpp
Normal file
67
apps/master/AdminRest.cpp
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
//
|
||||||
|
// Created by koncord on 04.09.17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "AdminRest.hpp"
|
||||||
|
|
||||||
|
#include <boost/property_tree/ptree.hpp>
|
||||||
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
|
|
||||||
|
#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<MasterServer> 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<string>();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cerr << "Error: " << result.get<string>() << 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();
|
||||||
|
}
|
24
apps/master/AdminRest.hpp
Normal file
24
apps/master/AdminRest.hpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
//
|
||||||
|
// Created by koncord on 04.09.17.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "SimpleWeb/https_server.hpp"
|
||||||
|
#include "MasterServer.hpp"
|
||||||
|
|
||||||
|
typedef SimpleWeb::Server<SimpleWeb::HTTPS> HttpsServer;
|
||||||
|
|
||||||
|
class AdminRest
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AdminRest(const std::string &cert, const std::string &key, const std::string &verifyFile, unsigned short port, std::shared_ptr<MasterServer> master);
|
||||||
|
void start();
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
HttpsServer httpServer;
|
||||||
|
std::shared_ptr<MasterServer> master;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,12 +3,15 @@ project(masterserver)
|
||||||
#set(CMAKE_CXX_STANDARD 14)
|
#set(CMAKE_CXX_STANDARD 14)
|
||||||
add_definitions(-std=gnu++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})
|
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)
|
option(BUILD_MASTER_TEST "build master server test program" OFF)
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,14 @@
|
||||||
#include <extern/sol/sol.hpp>
|
#include <extern/sol/sol.hpp>
|
||||||
#include "MasterServer.hpp"
|
#include "MasterServer.hpp"
|
||||||
#include "RestServer.hpp"
|
#include "RestServer.hpp"
|
||||||
|
#include "AdminRest.hpp"
|
||||||
|
|
||||||
using namespace RakNet;
|
using namespace RakNet;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
unique_ptr<RestServer> restServer;
|
unique_ptr<RestServer> restServer;
|
||||||
shared_ptr<MasterServer> masterServer;
|
shared_ptr<MasterServer> masterServer;
|
||||||
|
unique_ptr<AdminRest> restAdminServer;
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
@ -28,10 +30,30 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
|
|
||||||
restServer = make_unique<RestServer>(restPort.as<unsigned short>(), masterServer->GetServers());
|
restServer = make_unique<RestServer>(restPort.as<unsigned short>(), 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<AdminRest>(restAdminCert.as<string>(), restAdminKey.as<string>(),
|
||||||
|
restAdminVerifyFile.as<string>(), restAdminPort.as<unsigned short>(), masterServer);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto onExit = [](int /*sig*/){
|
auto onExit = [](int /*sig*/){
|
||||||
restServer->stop();
|
restServer->stop();
|
||||||
|
restAdminServer->stop();
|
||||||
masterServer->luaStuff([](sol::state &state) {
|
masterServer->luaStuff([](sol::state &state) {
|
||||||
sol::protected_function func = state["OnExit"];
|
sol::protected_function func = state["OnExit"];
|
||||||
if (func.valid())
|
if (func.valid())
|
||||||
|
@ -47,8 +69,10 @@ int main(int argc, char* argv[])
|
||||||
masterServer->Start();
|
masterServer->Start();
|
||||||
|
|
||||||
thread rest_thread([]() { restServer->start();});
|
thread rest_thread([]() { restServer->start();});
|
||||||
|
thread restAdmin_thread([]() { restAdminServer->start();});
|
||||||
|
|
||||||
rest_thread.join();
|
rest_thread.join();
|
||||||
|
restAdmin_thread.join();
|
||||||
masterServer->Wait();
|
masterServer->Wait();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue