cppwebapplication.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  8. * @mainpage The C++ Web Framework
  9. *
  10. * ​​The C++ Web Framework (CWF) is a MVC web framework, Open Source, under MIT License, created by Herik Lima and Marcelo Eler,
  11. * using C++ with Qt to be used in the development of web applications. The CWF was designed to consume few computational resources,
  12. * such as memory and processing, and have a low response time for requests. With MVC (Model-View-Controller) architecture,
  13. * you can create classes to take care of the business layer (Model), use CSTL (C++ Server Pages Standard Tag Library)
  14. * within the Web Pages to take care of data presentation (View) and use the controllers as a between the two layers (Controller).
  15. *
  16. * Because it is created in Qt, the C++ Web Framework can run on the same platforms supported by Qt:
  17. *
  18. *
  19. * Desktop: Linux, OS X, Windows
  20. * Embedded and RTOS: Linux, QNX, VxWorks, Windows
  21. * Mobile: Android, iOS, Windows
  22. *
  23. *
  24. * The CWF has only one configuration file, called CPPWeb.ini and a policy of using only C++ and Qt in the development
  25. * of its components in order to avoid the installation of numerous libraries to avoid conflicts, maintain multiplatform characteristics,
  26. * facilitate installation and keep the learning curve low in order to make web development as simple as possible, even for beginners.
  27. */
  28. #ifndef CPPWEBAPPLICATION_H
  29. #define CPPWEBAPPLICATION_H
  30. #include <QString>
  31. #include <QCoreApplication>
  32. #include <QMessageLogContext>
  33. #include "cppwebserver.h"
  34. #include "cppwebframework_global.h"
  35. CWF_BEGIN_NAMESPACE
  36. /**
  37. * @brief This class is responsible for encapsulating the QCoreApplication, the
  38. * CppWebServer and configure the server logging mechanism.
  39. */
  40. class CPPWEBFRAMEWORKSHARED_EXPORT CppWebApplication
  41. {
  42. Configuration configuration;
  43. CppWebServer *server;
  44. bool valid = false;
  45. CppWebApplication(const CppWebApplication &other) = delete;
  46. CppWebApplication &operator=(const CppWebApplication &other) = delete;
  47. public:
  48. /**
  49. * @brief Constructs a QCoreApplication, a CppWebServer and install the message handler.
  50. * if the server folder's path was not found in the serverPath, it will look into the executable's folder.
  51. * @param int argc : Main function parameter used to build QCoreApplication.
  52. * @param char *argv[] : Main function parameter used to build QCoreApplication.
  53. * @param const Configuration &config : Used to set the parameters of the server.
  54. * @param Filter *filter : Install a filter for requests on the server.
  55. */
  56. CppWebApplication(const QString &serverPath, Filter *filter = nullptr);
  57. /**
  58. * @brief Destroys the server dynamically allocated.
  59. */
  60. ~CppWebApplication();
  61. /**
  62. * @brief Hitches a url to a Controller.
  63. * @param const QString &url : Url name.
  64. * @param Controller *controller : Controller that will answer requests made to url.
  65. * @par Example
  66. * @code
  67. * #include <QCoreApplication>
  68. * #include <controllers/helloworldcontroller.h>
  69. * #include <cwf/cppwebapplication.h>
  70. *
  71. * int main(int argc, char *argv[])
  72. * {
  73. * CWF::CppWebApplication server(argc, argv, "/PATH_TO_EXAMPLE/server"));
  74. * server.addUrlController<HelloWorldController>("/hello");
  75. * return server.start();
  76. * }
  77. * @endcode
  78. */
  79. template<typename CONTROLLER>
  80. void addController(const QString &url) noexcept
  81. {
  82. server->addController<CONTROLLER>(url);
  83. }
  84. /**
  85. * @brief Starts the server and QCoreApplication.
  86. * @return int : Returns -1 if it fails.
  87. */
  88. int start();
  89. int start(const QHostAddress &host, const int &port);
  90. bool close();
  91. };
  92. CWF_END_NAMESPACE
  93. #endif // CPPWEBAPPLICATION_H