cppwebapplication.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
  3. Distributed under MIT license, or public domain if desired and
  4. recognized in your jurisdiction.
  5. See file LICENSE for detail.
  6. */
  7. #include "cppwebapplication.h"
  8. #include "cppwebcontroller.h"
  9. // clazy:excludeall=qgetenv
  10. CWF_BEGIN_NAMESPACE
  11. QPair<QString, qint64> getFileAndMaxSize()
  12. {
  13. QPair<QString, qlonglong> info;
  14. info.first = qgetenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data());
  15. info.second = QByteArray(qgetenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data())).toInt();
  16. if (info.second <= 0) {
  17. info.second = 20000000;
  18. }
  19. return info;
  20. }
  21. void writeLog(QtMsgType type, const QMessageLogContext &logContext, const QString &msg)
  22. {
  23. QPair<QString, qint64> info(getFileAndMaxSize());
  24. QFile file(info.first);
  25. if (file.size() > info.second) {
  26. file.resize(0);
  27. }
  28. if (file.open(QIODevice::Append)) {
  29. QTextStream out(&file);
  30. QString date = QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss:zzz");
  31. out << "Date: " << date;
  32. out << "\nCategory: " << logContext.category;
  33. out << "\nFile: " << logContext.file;
  34. out << "\nFunction: " << logContext.function;
  35. out << "\nVersion: " << logContext.version;
  36. out << "\nMessage: " << msg << "\n\n";
  37. if (type == QtFatalMsg) {
  38. out << "\nFATAL: SERVER OFFLINE\n\n";
  39. file.close();
  40. abort();
  41. }
  42. file.close();
  43. }
  44. }
  45. CppWebApplication::CppWebApplication(const QString &serverPath, Filter *filter)
  46. : configuration(serverPath)
  47. {
  48. if (configuration.isValid()) {
  49. qunsetenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data());
  50. qunsetenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data());
  51. qputenv(CONFIGURATION::CPP_LOG_VAR.toStdString().data(),
  52. configuration.getLogFilePath().toUtf8());
  53. qputenv(CONFIGURATION::CPP_LOG_MAX_VAR.toStdString().data(),
  54. QByteArray::number(configuration.getMaxLogFile()));
  55. if (configuration.isValid()) {
  56. valid = true;
  57. //qInstallMessageHandler(writeLog);
  58. server = new CppWebServer(configuration, filter);
  59. if (configuration.getAccessServerPages()) {
  60. server->addController<CppWebController>("/example");
  61. server->addController<CppWebController>("/authors");
  62. server->addController<CppWebController>("/documentation");
  63. server->addController<CppWebController>("/ssl");
  64. server->addController<CppWebController>("/index");
  65. }
  66. }
  67. // QPair<QString, qint64> info(getFileAndMaxSize());
  68. // if (!QFile(info.first).exists()) {
  69. // qDebug() << "Path not found to log file: " << configuration.getLogFilePath();
  70. // qDebug() << "Note: Use only US-ASCII characters for the serverPath.";
  71. // } else if (configuration.isValid()) {
  72. // }
  73. } else {
  74. qDebug() << "CPPWeb.ini not found. Please copy the CWF server folder to your project.";
  75. }
  76. }
  77. CppWebApplication::~CppWebApplication()
  78. {
  79. if (!server)
  80. delete server;
  81. }
  82. int CppWebApplication::start()
  83. {
  84. return start(configuration.getHost(), configuration.getPort());
  85. }
  86. int CppWebApplication::start(const QHostAddress &host, const int &port)
  87. {
  88. if (!server) {
  89. return -1;
  90. }
  91. if (!server->listen(host, port)) {
  92. qDebug() << "Error: " << server->errorString() << "\n";
  93. qDebug() << "Host: " << host;
  94. qDebug() << "Port: " << port;
  95. qDebug() << "Server offline\n";
  96. return -2;
  97. }
  98. qDebug() << "Server online\n";
  99. qDebug() << "Host: " << host;
  100. qDebug() << "Port: " << port;
  101. qDebug() << "LogFilePath: " << configuration.getLogFilePath();
  102. return 1;
  103. }
  104. bool CppWebApplication::close()
  105. {
  106. if (server) {
  107. server->close();
  108. return true;
  109. }
  110. return false;
  111. }
  112. CWF_END_NAMESPACE