server.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef KBF_HTTP_SERVER_H
  2. #define KBF_HTTP_SERVER_H
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <esp_http_server.h>
  7. #include "kbf/http/common.h"
  8. using std::string;
  9. using std::vector;
  10. using std::map;
  11. namespace kbf::http {
  12. /** @brief HTTP Server */
  13. class Server {
  14. public:
  15. /** @brief Tag used for logging. */
  16. static constexpr const char *TAG = "kbf::http::Server";
  17. /**
  18. * @brief Destructor.
  19. *
  20. * Will call stop() if #running.
  21. */
  22. ~Server();
  23. /**
  24. * @brief Holds information about a route and it's handler.
  25. */
  26. struct Route {
  27. public:
  28. /** @brief HTTP method */
  29. http::Method method;
  30. /**
  31. * @brief URI
  32. *
  33. * @example "/"
  34. * @example "/foo"
  35. */
  36. const string uri;
  37. /**
  38. * @brief Request handler function.
  39. *
  40. * @param request Request sent by a client
  41. * @param data custom data passed to the request handler function
  42. * @return Response to be sent to the client
  43. */
  44. Response (*handler)(const Request &request, void *data);
  45. /**
  46. * @brief Custom data passed to the request handler function.
  47. */
  48. void *data = nullptr;
  49. /**
  50. * @brief Called after a response is sent successfully.
  51. *
  52. * @param response sent http::Response
  53. * @param data custom data (from Route::data)
  54. */
  55. void (*onResponseSent)(const Response &response, void *data) = nullptr;
  56. };
  57. /**
  58. * @brief Adds a Route handler.
  59. *
  60. * @note All routes must be added before calling start().
  61. *
  62. * @note Removing routes is not yet supported.
  63. *
  64. * @param route Route to add
  65. * @return *this (to enable builder-like syntax)
  66. */
  67. Server &route(Route route);
  68. /**
  69. * @brief Starts the HTTP server.
  70. *
  71. * @param port TCP port, default is 80
  72. * @return *this (to enable builder-like syntax)
  73. */
  74. Server &start(int port = 80);
  75. /**
  76. * @brief Starts the server in SSL (HTTPS) mode.
  77. *
  78. * @param cert_start start of the certificate
  79. * @param cert_end end of the certificate
  80. * @param key_start start of the private key
  81. * @param key_end end of the private key
  82. * @param port TCP port, default is 443
  83. * @return *this (to enable builder-like syntax)
  84. */
  85. Server &startSSL(const unsigned char *cert_start, const unsigned char *cert_end,
  86. const unsigned char *key_start, const unsigned char *key_end,
  87. int port = 443);
  88. /**
  89. * @brief Stops the HTTP server.
  90. */
  91. void stop();
  92. /**
  93. * @brief Returns whether the server is running.
  94. *
  95. * @return true if server is running; false otherwise
  96. */
  97. [[nodiscard]] bool isRunning() const { return running; }
  98. private:
  99. bool running = false;
  100. httpd_handle_t handle = nullptr;
  101. vector<Route> routes;
  102. static esp_err_t handleHttpRequest(httpd_req_t *httpdRequest);
  103. void registerUriHandlers();
  104. };
  105. }
  106. #endif //KBF_HTTP_SERVER_H