Add server_info http endpoint to serve http://versions

This commit is contained in:
Eric Harding committed 2018-07-19 15:12:39 -04:00
1 parent ac47f0246f
commit 71fe863f1b
3 files changed
+65 -1

No files matched your search

+30 -1
View File
@@ -4,6 +4,7 @@
#include "lib/api/public/ApiUtils.h"
#include "lib/api/core/ApiClientInterop.h"
#include "lib/api/services/UserService.client.pb.h"
#include "lib/api/services/AdminService.client.pb.h"
#include "lib/utils/bts_assert.h"
#include <vector>
#include <unordered_map>
@@ -123,7 +124,7 @@ ApiGatewaySession::~ApiGatewaySession(){}
void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, const Responder& reply) {
GatewayResponse response;
LOG(Api,Info, "Gateway Request: {}", api::toJSON(request));
LOG(Api,Diag, "Gateway Request: {}", api::toJSON(request));
switch(request.request_case()) {
case GatewayRequest::kLogin:
_me->handle_login(request.login(), reply);
@@ -138,4 +139,32 @@ void ApiGatewaySession::handle(const api::gateway::GatewayRequest& request, cons
}
}
std::string get_server_info(const api::ServiceContext& ctx) {
using namespace api::admin;
using std::endl;
api::VoidMessage vod;
AdminService admin(ctx.locate<api::admin::AdminService>());
auto response = admin.get_admin_data(vod, 100);
std::stringstream ss;
ss << "{" << endl;
if (response.success()) {
ss << " \"users\": [" << endl;
for (const auto& user : response.result()->users()) {
ss << " ";
if (&user != &response.result()->users()[0]) ss << ",";
ss << "{ ";
ss << "\"name\": \"" << user.name() << "\", ";
ss << "\"operator_id\": \"" << user.user_data().exchange_user_data().cme_data().operator_id() << "\"";
ss << "}" << endl;
}
ss << " ]" << endl;
}
ss << "}" << endl;
return ss.str();
}
}}
+4
View File
@@ -20,6 +20,10 @@ class ApiGatewaySession {
~ApiGatewaySession();
void handle(const api::gateway::GatewayRequest& request, const Responder&);
};
std::string get_server_info(const api::ServiceContext&);
}}
+31
View File
@@ -156,6 +156,34 @@ namespace http {
_mtimer->observe((utils::TimeStamp::now() - start).microseconds() / 1000.0);
}
bool require_authentication(connection_ptr& con, const std::string& username, const std::string& password) {
auto auth = con->get_request_header("Authorization");
if (!auth.empty() && auth.size() >= 7 && boost::iequals(auth.substr(0, 6), "Basic ")) {
auto b64 = auth.substr(6);
auto decoded = websocketpp::base64_decode(b64);
auto colon = decoded.find(':');
if (colon != std::string::npos) {
auto user = decoded.substr(0, colon);
auto pass = decoded.substr(colon+1);
if (user == username && pass == password) return true;
}
}
// Failed
con->append_header("WWW-Authenticate", "Basic realm=\"BTS\"");
con->set_status(websocketpp::http::status_code::unauthorized);
con->set_body("Not Authorized");
return false;
}
void check_auth_and_return_info(connection_ptr& con) {
if (require_authentication(con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) {
auto horrible_json = get_server_info(_ctx);
con->set_body(horrible_json);
con->set_status(websocketpp::http::status_code::ok);
}
}
void directory_index(connection_ptr& con, const std::string& resource, const boost::filesystem::path& path) {
// directory listing
std::string body = "<!doctype html>\n";
@@ -256,6 +284,9 @@ namespace http {
else if (boost::iequals(resource, "/metrics")) {
return_metrics(con);
}
else if (boost::iequals(resource, "/server_info")) {
check_auth_and_return_info(con);
}
else if (boost::iequals(resource, "/bts_installer.exe") && boost::filesystem::is_regular_file(_www+"/bts_installer.exe")) {
return_bts_installer(con);
}