mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-04-29 07:37:45 +00:00
Cleanup and simplification of rest server
This commit is contained in:
parent
bfc550be2b
commit
0e8f3ea6db
1 changed files with 590 additions and 540 deletions
|
@ -169,45 +169,89 @@ bool loadSettings( int32_t argc, char* argv[] )
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main( int argc, char* argv[] )
|
using ContentType = enum
|
||||||
{
|
{
|
||||||
g_log.setLogPath( "log\\SapphireAPI" );
|
NONE,
|
||||||
g_log.init();
|
TEXT_PLAIN,
|
||||||
|
JSON,
|
||||||
|
XML,
|
||||||
|
};
|
||||||
|
|
||||||
g_log.info( "===========================================================" );
|
std::string buildHttpResponse( uint16_t rCode, const std::string& content = "", ContentType type = NONE )
|
||||||
g_log.info( "Sapphire API Server " );
|
|
||||||
g_log.info( "Version: 0.0.1" );
|
|
||||||
g_log.info( "Compiled: " __DATE__ " " __TIME__ );
|
|
||||||
g_log.info( "===========================================================" );
|
|
||||||
|
|
||||||
if (!loadSettings(argc, argv))
|
|
||||||
{
|
{
|
||||||
throw std::exception();
|
std::string result{""};
|
||||||
|
std::string httpHead{"HTTP/1.1 "};
|
||||||
|
std::string contentHeader{"Content-Length: "};
|
||||||
|
std::string contentTypeHeader{"Content-Type: "};
|
||||||
|
|
||||||
|
switch( type )
|
||||||
|
{
|
||||||
|
case NONE:
|
||||||
|
contentTypeHeader = "";
|
||||||
|
break;
|
||||||
|
case TEXT_PLAIN:
|
||||||
|
contentTypeHeader += "text/plain\r\n";
|
||||||
|
break;
|
||||||
|
case JSON:
|
||||||
|
contentTypeHeader += "application/json\r\n";
|
||||||
|
break;
|
||||||
|
case XML:
|
||||||
|
contentTypeHeader += "text/xml\r\n";
|
||||||
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_exdData.loadZoneInfo();
|
switch( rCode )
|
||||||
g_exdData.loadClassJobInfo();
|
{
|
||||||
|
case 200:
|
||||||
|
result += httpHead + "200 OK\r\n";
|
||||||
|
if( content.size() > 0 )
|
||||||
|
{
|
||||||
|
result += contentTypeHeader;
|
||||||
|
result += contentHeader + std::to_string( content.size() ) + "\r\n";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 400:
|
||||||
|
case 401:
|
||||||
|
case 402:
|
||||||
|
case 403:
|
||||||
|
result += httpHead + std::to_string( rCode ) + "\r\n";
|
||||||
|
if( content.size() > 0 )
|
||||||
|
{
|
||||||
|
result += contentTypeHeader;
|
||||||
|
result += contentHeader + std::to_string( content.size() ) + "\r\n";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 500:
|
||||||
|
result += httpHead + "500 Internal Server Error\r\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
result += httpHead + std::to_string( rCode ) + "\r\n";
|
||||||
|
|
||||||
server.config.port = stoi( m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) );
|
|
||||||
g_log.info( "Starting REST server at port " + m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) + "..." );
|
|
||||||
|
|
||||||
server.resource["^/ZoneName/([0-9]+)$"]["GET"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
|
|
||||||
|
}
|
||||||
|
result += "\r\n";
|
||||||
|
if( content.size() > 0 )
|
||||||
|
result += content;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getZoneName( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
string number = request->path_match[1];
|
string number = request->path_match[1];
|
||||||
auto it = g_exdData.m_zoneInfoMap.find( atoi( number.c_str() ) );
|
auto it = g_exdData.m_zoneInfoMap.find( atoi( number.c_str() ) );
|
||||||
std::string responseStr = "Not found!";
|
std::string responseStr = "Not found!";
|
||||||
if( it != g_exdData.m_zoneInfoMap.end() )
|
if( it != g_exdData.m_zoneInfoMap.end() )
|
||||||
{
|
|
||||||
responseStr = it->second.zone_name + ", " + it->second.zone_str;
|
responseStr = it->second.zone_name + ", " + it->second.zone_str;
|
||||||
|
*response << buildHttpResponse( 200, responseStr );
|
||||||
}
|
}
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << responseStr.length() << "\r\n\r\n" << responseStr;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
void createAccount( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
/* Create account */
|
{
|
||||||
server.resource["^/sapphire-api/lobby/createAccount"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
std::string responseStr = "HTTP/1.1 400\r\n\r\n";
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -217,33 +261,29 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
std::string pass = pt.get<string>( "pass" );
|
std::string pass = pt.get<string>( "pass" );
|
||||||
std::string user = pt.get<string>( "username" );
|
std::string user = pt.get<string>( "username" );
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
std::string sId;
|
std::string sId;
|
||||||
if( g_sapphireAPI.createAccount( user, pass, sId ) )
|
if( g_sapphireAPI.createAccount( user, pass, sId ) )
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"sId\":\"" + sId + "\", \"lobbyHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.LobbyHost" ) + "\", \"frontierHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.FrontierHost" ) + "\"}";
|
std::string json_string = "{\"sId\":\"" + sId +
|
||||||
*response << "HTTP/1.1 200 OK\r\n "
|
"\", \"lobbyHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.LobbyHost" ) +
|
||||||
<< "Content-Type: application/json\r\n"
|
"\", \"frontierHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.FrontierHost" ) + "\"}";
|
||||||
<< "Content-Length: " << json_string.length() << "\r\n\r\n"
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
<< json_string;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
*response << "HTTP/1.1 400\r\n\r\n";
|
*response << buildHttpResponse( 400 );
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << responseStr.length() << "\r\nContent-Type: text/xml\r\n\r\n" << responseStr;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
void login( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
server.resource["^/sapphire-api/lobby/login"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
|
@ -256,33 +296,33 @@ int main( int argc, char* argv[] )
|
||||||
std::string sId;
|
std::string sId;
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
if( g_sapphireAPI.login( user, pass, sId ) )
|
if( g_sapphireAPI.login( user, pass, sId ) )
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"sId\":\"" + sId + "\", \"lobbyHost\":\"" + m_pConfig->getValue< std::string >("Settings.General.LobbyHost") + "\", \"frontierHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.FrontierHost" ) + "\"}";
|
std::string json_string = "{\"sId\":\"" + sId +
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
"\", \"lobbyHost\":\"" + m_pConfig->getValue< std::string >("Settings.General.LobbyHost") +
|
||||||
|
"\", \"frontierHost\":\"" + m_pConfig->getValue< std::string >( "Settings.General.FrontierHost" ) + "\"}";
|
||||||
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
*response << "HTTP/1.1 400\r\n\r\n";
|
*response << buildHttpResponse( 400 );
|
||||||
|
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/deleteCharacter"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void deleteCharacter( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string sId = pt.get<string>( "sId" );
|
std::string sId = pt.get<string>( "sId" );
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
std::string name = pt.get<string>( "name" );
|
std::string name = pt.get<string>( "name" );
|
||||||
|
@ -291,34 +331,34 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
int32_t accountId = g_sapphireAPI.checkSession( sId );
|
int32_t accountId = g_sapphireAPI.checkSession( sId );
|
||||||
|
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
g_sapphireAPI.deleteCharacter( name, accountId );
|
g_sapphireAPI.deleteCharacter( name, accountId );
|
||||||
std::string json_string = "{\"result\":\"success\"}";
|
std::string json_string = "{\"result\":\"success\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/createCharacter"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void createCharacter( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string sId = pt.get<string>( "sId" );
|
std::string sId = pt.get<string>( "sId" );
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
std::string name = pt.get<string>( "name" );
|
std::string name = pt.get<string>( "name" );
|
||||||
|
@ -332,33 +372,35 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
if( result != -1 )
|
if( result != -1 )
|
||||||
{
|
{
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int32_t charId = g_sapphireAPI.createCharacter( result, name, finalJson, m_pConfig->getValue< uint8_t >( "Settings.Parameters.DefaultGMRank", 255 ) );
|
int32_t charId = g_sapphireAPI.createCharacter( result, name, finalJson, m_pConfig->getValue< uint8_t >( "Settings.Parameters.DefaultGMRank", 255 ) );
|
||||||
|
|
||||||
std::string json_string = "{\"result\":\"" + std::to_string( charId ) + "\"}";
|
std::string json_string = "{\"result\":\"" + std::to_string( charId ) + "\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid\"}";
|
std::string json_string = "{\"result\":\"invalid\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/insertSession"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void insertSession( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -366,33 +408,31 @@ int main( int argc, char* argv[] )
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string sId = pt.get<string>( "sId" );
|
std::string sId = pt.get<string>( "sId" );
|
||||||
uint32_t accountId = pt.get<uint32_t>( "accountId" );
|
uint32_t accountId = pt.get<uint32_t>( "accountId" );
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
g_sapphireAPI.insertSession( accountId, sId );
|
g_sapphireAPI.insertSession( accountId, sId );
|
||||||
std::string json_string = "{\"result\":\"success\"}";
|
std::string json_string = "{\"result\":\"success\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
void checkNameTaken( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
server.resource["^/sapphire-api/lobby/checkNameTaken"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -406,9 +446,10 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -417,92 +458,90 @@ int main( int argc, char* argv[] )
|
||||||
json_string = "{\"result\":\"false\"}";
|
json_string = "{\"result\":\"false\"}";
|
||||||
else
|
else
|
||||||
json_string = "{\"result\":\"true\"}";
|
json_string = "{\"result\":\"true\"}";
|
||||||
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
void checkSession( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
server.resource["^/sapphire-api/lobby/checkSession"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string sId = pt.get<string>( "sId" );
|
std::string sId = pt.get<string>( "sId" );
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
|
|
||||||
int32_t result = g_sapphireAPI.checkSession( sId );
|
int32_t result = g_sapphireAPI.checkSession( sId );
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
if( result != -1 )
|
if( result != -1 )
|
||||||
{
|
{
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"" + std::to_string( result ) + "\"}";
|
std::string json_string = "{\"result\":\"" + std::to_string( result ) + "\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid\"}";
|
std::string json_string = "{\"result\":\"invalid\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/getNextCharId"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void getNextCharId( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"" + std::to_string( g_sapphireAPI.getNextCharId() ) + "\"}";
|
std::string json_string = "{\"result\":\"" + std::to_string( g_sapphireAPI.getNextCharId() ) + "\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/getNextContentId"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void getNextContentId( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -510,38 +549,37 @@ int main( int argc, char* argv[] )
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
|
|
||||||
// reloadConfig();
|
// reloadConfig();
|
||||||
|
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"" + std::to_string( g_sapphireAPI.getNextContentId() ) + "\"}";
|
std::string json_string = "{\"result\":\"" + std::to_string( g_sapphireAPI.getNextContentId() ) + "\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
}
|
||||||
|
|
||||||
server.resource["^/sapphire-api/lobby/getCharacterList"]["POST"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
void getCharacterList( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using namespace boost::property_tree;
|
using namespace boost::property_tree;
|
||||||
ptree pt;
|
ptree pt;
|
||||||
read_json( request->content, pt );
|
read_json( request->content, pt );
|
||||||
|
|
||||||
std::string sId = pt.get<string>( "sId" );
|
std::string sId = pt.get<string>( "sId" );
|
||||||
std::string secret = pt.get<string>( "secret" );
|
std::string secret = pt.get<string>( "secret" );
|
||||||
|
|
||||||
|
@ -551,10 +589,10 @@ int main( int argc, char* argv[] )
|
||||||
|
|
||||||
if( result != -1 )
|
if( result != -1 )
|
||||||
{
|
{
|
||||||
|
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret )
|
||||||
if( m_pConfig->getValue< std::string >( "Settings.General.ServerSecret" ) != secret ) {
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
std::string json_string = "{\"result\":\"invalid_secret\"}";
|
||||||
*response << "HTTP/1.1 403\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 403, json_string, JSON );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -578,26 +616,25 @@ int main( int argc, char* argv[] )
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
write_json( oss, pt );
|
write_json( oss, pt );
|
||||||
std::string responseStr = oss.str();
|
std::string responseStr = oss.str();
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << responseStr.length() << "\r\n\r\n" << responseStr;
|
*response << buildHttpResponse( 200, responseStr, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string json_string = "{\"result\":\"invalid\"}";
|
std::string json_string = "{\"result\":\"invalid\"}";
|
||||||
*response << "HTTP/1.1 200\r\nContent-Length: " << json_string.length() << "\r\n\r\n" << json_string;
|
*response << buildHttpResponse( 200, json_string, JSON );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
void get_init( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
server.resource["^(/frontier-api/ffxivsupport/view/get_init)(.*)"]["GET"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto web_root_path = boost::filesystem::canonical( "web" );
|
auto web_root_path = boost::filesystem::canonical( "web" );
|
||||||
|
@ -609,11 +646,6 @@ int main( int argc, char* argv[] )
|
||||||
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
||||||
throw invalid_argument( "file does not exist" );
|
throw invalid_argument( "file does not exist" );
|
||||||
|
|
||||||
std::string cache_control, etag;
|
|
||||||
|
|
||||||
// Uncomment the following line to enable Cache-Control
|
|
||||||
// cache_control="Cache-Control: max-age=86400\r\n";
|
|
||||||
|
|
||||||
auto ifs = make_shared<ifstream>();
|
auto ifs = make_shared<ifstream>();
|
||||||
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
||||||
|
|
||||||
|
@ -622,7 +654,7 @@ int main( int argc, char* argv[] )
|
||||||
auto length = ifs->tellg();
|
auto length = ifs->tellg();
|
||||||
ifs->seekg( 0, ios::beg );
|
ifs->seekg( 0, ios::beg );
|
||||||
|
|
||||||
*response << "HTTP/1.1 200 OK\r\n" << cache_control << etag << "Content-Length: " << length << "\r\n\r\n";
|
*response << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << length << "\r\n\r\n";
|
||||||
default_resource_send( server, response, ifs );
|
default_resource_send( server, response, ifs );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -630,15 +662,14 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
void get_headline_all( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
server.resource["^(/frontier-api/ffxivsupport/information/get_headline_all)(.*)"]["GET"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto web_root_path = boost::filesystem::canonical( "web" );
|
auto web_root_path = boost::filesystem::canonical( "web" );
|
||||||
|
@ -650,11 +681,6 @@ int main( int argc, char* argv[] )
|
||||||
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
||||||
throw invalid_argument( "file does not exist" );
|
throw invalid_argument( "file does not exist" );
|
||||||
|
|
||||||
std::string cache_control, etag;
|
|
||||||
|
|
||||||
// Uncomment the following line to enable Cache-Control
|
|
||||||
// cache_control="Cache-Control: max-age=86400\r\n";
|
|
||||||
|
|
||||||
auto ifs = make_shared<ifstream>();
|
auto ifs = make_shared<ifstream>();
|
||||||
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
||||||
|
|
||||||
|
@ -662,8 +688,7 @@ int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
auto length = ifs->tellg();
|
auto length = ifs->tellg();
|
||||||
ifs->seekg( 0, ios::beg );
|
ifs->seekg( 0, ios::beg );
|
||||||
|
*response << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << length << "\r\n\r\n";
|
||||||
*response << "HTTP/1.1 200 OK\r\n" << cache_control << etag << "Content-Length: " << length << "\r\n\r\n";
|
|
||||||
default_resource_send( server, response, ifs );
|
default_resource_send( server, response, ifs );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -671,19 +696,14 @@ int main( int argc, char* argv[] )
|
||||||
}
|
}
|
||||||
catch( exception& e )
|
catch( exception& e )
|
||||||
{
|
{
|
||||||
*response << "HTTP/1.1 500\r\n\r\n";
|
*response << buildHttpResponse( 500 );
|
||||||
g_log.error( e.what() );
|
g_log.error( e.what() );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
void defaultGet( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request )
|
||||||
|
{
|
||||||
//Default GET-example. If no other matches, this anonymous function will be called.
|
|
||||||
//Will respond with content in the web/-directory, and its subdirectories.
|
|
||||||
//Default file: index.html
|
|
||||||
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
|
|
||||||
server.default_resource["GET"] = [&]( shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request ) {
|
|
||||||
print_request_info( request );
|
print_request_info( request );
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto web_root_path = boost::filesystem::canonical( "web" );
|
auto web_root_path = boost::filesystem::canonical( "web" );
|
||||||
|
@ -697,11 +717,6 @@ int main( int argc, char* argv[] )
|
||||||
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
if( !( boost::filesystem::exists( path ) && boost::filesystem::is_regular_file( path ) ) )
|
||||||
throw invalid_argument( "file does not exist" );
|
throw invalid_argument( "file does not exist" );
|
||||||
|
|
||||||
std::string cache_control, etag;
|
|
||||||
|
|
||||||
// Uncomment the following line to enable Cache-Control
|
|
||||||
// cache_control="Cache-Control: max-age=86400\r\n";
|
|
||||||
|
|
||||||
auto ifs = make_shared<ifstream>();
|
auto ifs = make_shared<ifstream>();
|
||||||
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
ifs->open( path.string(), ifstream::in | ios::binary | ios::ate );
|
||||||
|
|
||||||
|
@ -710,7 +725,7 @@ int main( int argc, char* argv[] )
|
||||||
auto length = ifs->tellg();
|
auto length = ifs->tellg();
|
||||||
ifs->seekg( 0, ios::beg );
|
ifs->seekg( 0, ios::beg );
|
||||||
|
|
||||||
*response << "HTTP/1.1 200 OK\r\n" << cache_control << etag << "Content-Length: " << length << "\r\n\r\n";
|
*response << "HTTP/1.1 200 OK\r\n" << "Content-Length: " << length << "\r\n\r\n";
|
||||||
default_resource_send( server, response, ifs );
|
default_resource_send( server, response, ifs );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -719,11 +734,48 @@ int main( int argc, char* argv[] )
|
||||||
catch( const exception & )
|
catch( const exception & )
|
||||||
{
|
{
|
||||||
string content = "Path not found: " + request->path;
|
string content = "Path not found: " + request->path;
|
||||||
*response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
|
*response << buildHttpResponse( 400, content );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
thread server_thread( [&]() {
|
int main( int argc, char* argv[] )
|
||||||
|
{
|
||||||
|
g_log.setLogPath( "log\\SapphireAPI" );
|
||||||
|
g_log.init();
|
||||||
|
|
||||||
|
g_log.info( "===========================================================" );
|
||||||
|
g_log.info( "Sapphire API Server " );
|
||||||
|
g_log.info( "Version: 0.0.1" );
|
||||||
|
g_log.info( "Compiled: " __DATE__ " " __TIME__ );
|
||||||
|
g_log.info( "===========================================================" );
|
||||||
|
|
||||||
|
if( !loadSettings( argc, argv ) )
|
||||||
|
throw std::exception();
|
||||||
|
|
||||||
|
g_exdData.loadZoneInfo();
|
||||||
|
g_exdData.loadClassJobInfo();
|
||||||
|
|
||||||
|
server.config.port = stoi( m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) );
|
||||||
|
g_log.info( "Starting API server at port " + m_pConfig->getValue< std::string >( "Settings.General.HttpPort", "80" ) + "..." );
|
||||||
|
|
||||||
|
server.resource["^/ZoneName/([0-9]+)$"]["GET"] = &getZoneName;
|
||||||
|
server.resource["^/sapphire-api/lobby/createAccount"]["POST"] = &createAccount;
|
||||||
|
server.resource["^/sapphire-api/lobby/login"]["POST"] = &login;
|
||||||
|
server.resource["^/sapphire-api/lobby/deleteCharacter"]["POST"] = &deleteCharacter;
|
||||||
|
server.resource["^/sapphire-api/lobby/createCharacter"]["POST"] = &createCharacter;
|
||||||
|
server.resource["^/sapphire-api/lobby/insertSession"]["POST"] = &insertSession;
|
||||||
|
server.resource["^/sapphire-api/lobby/checkNameTaken"]["POST"] = &checkNameTaken;
|
||||||
|
server.resource["^/sapphire-api/lobby/checkSession"]["POST"] = &checkSession;
|
||||||
|
server.resource["^/sapphire-api/lobby/getNextCharId"]["POST"] = &getNextCharId;
|
||||||
|
server.resource["^/sapphire-api/lobby/getNextContentId"]["POST"] = &getNextContentId;
|
||||||
|
server.resource["^/sapphire-api/lobby/getCharacterList"]["POST"] = &getCharacterList;
|
||||||
|
server.resource["^(/frontier-api/ffxivsupport/view/get_init)(.*)"]["GET"] = &get_init;
|
||||||
|
server.resource["^(/frontier-api/ffxivsupport/information/get_headline_all)(.*)"]["GET"] = &get_headline_all;
|
||||||
|
|
||||||
|
server.default_resource["GET"] = &defaultGet;
|
||||||
|
|
||||||
|
thread server_thread( [&]()
|
||||||
|
{
|
||||||
//Start server
|
//Start server
|
||||||
server.start();
|
server.start();
|
||||||
} );
|
} );
|
||||||
|
@ -732,8 +784,6 @@ int main( int argc, char* argv[] )
|
||||||
this_thread::sleep_for( chrono::seconds( 1 ) );
|
this_thread::sleep_for( chrono::seconds( 1 ) );
|
||||||
|
|
||||||
server_thread.join();
|
server_thread.join();
|
||||||
g_log.info( "Started REST server at port " + std::to_string( server.config.port ) );
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue