DataStorageAPI.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "API/WebAPI.h"
  2. #include "DataInterface/GraphClient.h"
  3. #include <DataStorageClient.h>
  4. #include <HttpServer.h>
  5. #include <INIReader.h>
  6. #include <Logging.h>
  7. #include <MySQLClient.h>
  8. #include <sstream>
  9. int main(int argc, char** argv)
  10. {
  11. try
  12. {
  13. Logging::OpenLog();
  14. Logging::SetLogMask(Logging::Severity::Info);
  15. INIReader iniReader("DataStorageAPI.ini");
  16. if (iniReader.ParseError() != 0)
  17. throw std::runtime_error("Can't read DataLogger.ini.");
  18. Logging::Log(Logging::Severity::Info, "Starting DataStorageAPI");
  19. int port = iniReader.GetInteger("DataStorageAPI", "Port", 0);
  20. if (port == 0)
  21. throw std::runtime_error("Port directive missing in ini file.");
  22. std::string table = iniReader.Get("DataStorageAPI", "Table", "");
  23. if (table.empty())
  24. throw std::runtime_error("Table directive missing in ini file.");
  25. std::shared_ptr<MySQL::MySQLClient> pMySQLClient = std::make_shared<MySQL::MySQLClient>();
  26. {
  27. std::string hostname = iniReader.Get("MySQL", "Hostname", "");
  28. if (hostname.empty())
  29. throw std::runtime_error("MySQL Hostname directive missing in ini file.");
  30. std::string username = iniReader.Get("MySQL", "Username", "");
  31. if (username.empty())
  32. throw std::runtime_error("MySQL Username directive missing in ini file.");
  33. std::string password = iniReader.Get("MySQL", "Password", "");
  34. if (password.empty())
  35. throw std::runtime_error("MySQL Password directive missing in ini file.");
  36. std::string database = iniReader.Get("MySQL", "Database", "");
  37. if (database.empty())
  38. throw std::runtime_error("MySQL Database directive missing in ini file.");
  39. pMySQLClient->Connect(hostname, username, password, database);
  40. }
  41. DataStorage::DataStorageClient dataStorageClient(pMySQLClient, table);
  42. DataStorageInterface::DataInterface::GraphClient graphClient(pMySQLClient.get());
  43. auto callback = std::bind(&DataStorageInterface::API::WebAPI::ProcessQuery, &dataStorageClient, &graphClient, std::placeholders::_1, std::placeholders::_2);
  44. Http::HttpServer server(port, callback);
  45. Logging::Log(Logging::Severity::Info, "Startup Complete");
  46. server.Wait();
  47. Logging::Log(Logging::Severity::Info, "Stopping DataStorageAPI...");
  48. Logging::CloseLog();
  49. }
  50. catch (const std::exception& e)
  51. {
  52. std::stringstream ss;
  53. ss << "ERROR: " << e.what() << std::endl;
  54. Logging::Log(Logging::Severity::Error, ss.str());
  55. Logging::CloseLog();
  56. return -1;
  57. }
  58. return 0;
  59. }