Добавил C++-сервер
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
#include <controllers/dispatcher.h>
|
||||
#include <controllers/implementation.h>
|
||||
|
||||
std::string Dispatcher::operator()(const char* request) const {
|
||||
// todo: формирование успешного и ошибочного ответов
|
||||
|
||||
std::string command;
|
||||
json result;
|
||||
try {
|
||||
result = CallImpl(request, command);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
json json;
|
||||
json[fields::response::command] = command;
|
||||
json[fields::response::status] = "error";
|
||||
json[fields::response::error_message] = e.what();
|
||||
|
||||
result = json.dump();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
json json;
|
||||
json[fields::response::command] = command;
|
||||
json[fields::response::status] = "ok";
|
||||
json[fields::response::result] = result;
|
||||
|
||||
result = json.dump();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
json Dispatcher::CallImpl(const char* request, std::string& command) const {
|
||||
// Проверяем корректность формата запроса
|
||||
auto json = ParseJson(request);
|
||||
|
||||
// Проверяем наличие необходимых ключей
|
||||
CheckForRequiredFields(json, { fields::request::command, fields::request::parameters });
|
||||
|
||||
// Проверяем наличие команды с указанным именем
|
||||
command = json[fields::request::command].get<std::string>();
|
||||
if (!controllers_.count(command)) {
|
||||
return "unknown command '" + command + "'";
|
||||
}
|
||||
|
||||
// Получаем команду по имени
|
||||
const auto& controller = *controllers_.at(command);
|
||||
|
||||
// Формируем и возвращаем ответ
|
||||
auto parameters = json[fields::request::parameters];
|
||||
return controller(parameters);
|
||||
}
|
||||
Reference in New Issue
Block a user