HttpServer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef HV_HTTP_SERVER_H_
  2. #define HV_HTTP_SERVER_H_
  3. #include "hexport.h"
  4. #include "HttpService.h"
  5. // #include "WebSocketServer.h"
  6. namespace hv {
  7. struct WebSocketService;
  8. }
  9. using hv::HttpService;
  10. using hv::WebSocketService;
  11. typedef struct http_server_s {
  12. char host[64];
  13. int port; // http_port
  14. int https_port;
  15. int http_version;
  16. int worker_processes;
  17. int worker_threads;
  18. uint32_t worker_connections; // max_connections = workers * worker_connections
  19. HttpService* service; // http service
  20. WebSocketService* ws; // websocket service
  21. void* userdata;
  22. int listenfd[2]; // 0: http, 1: https
  23. void* privdata;
  24. // hooks
  25. std::function<void()> onWorkerStart;
  26. std::function<void()> onWorkerStop;
  27. #ifdef __cplusplus
  28. http_server_s() {
  29. strcpy(host, "0.0.0.0");
  30. // port = DEFAULT_HTTP_PORT;
  31. // https_port = DEFAULT_HTTPS_PORT;
  32. // port = 8080;
  33. // https_port = 8443;
  34. port = https_port = 0;
  35. http_version = 1;
  36. worker_processes = 0;
  37. worker_threads = 0;
  38. worker_connections = 1024;
  39. service = NULL;
  40. ws = NULL;
  41. listenfd[0] = listenfd[1] = -1;
  42. userdata = NULL;
  43. privdata = NULL;
  44. }
  45. #endif
  46. } http_server_t;
  47. // @param wait: Whether to occupy current thread
  48. HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
  49. // NOTE: stop all loops and join all threads
  50. HV_EXPORT int http_server_stop(http_server_t* server);
  51. /*
  52. #include "HttpServer.h"
  53. using namespace hv;
  54. int main() {
  55. HttpService service;
  56. service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  57. resp->body = "pong";
  58. return 200;
  59. });
  60. HttpServer server;
  61. server.registerHttpService(&service);
  62. server.setPort(8080);
  63. server.setThreadNum(4);
  64. server.run();
  65. return 0;
  66. }
  67. */
  68. namespace hv {
  69. class HttpServer : public http_server_t {
  70. public:
  71. HttpServer() : http_server_t() {}
  72. ~HttpServer() { stop(); }
  73. void registerHttpService(HttpService* service) {
  74. this->service = service;
  75. }
  76. void setHost(const char* host = "0.0.0.0") {
  77. if (host) strcpy(this->host, host);
  78. }
  79. void setPort(int port = 0, int ssl_port = 0) {
  80. if (port != 0) this->port = port;
  81. if (ssl_port != 0) this->https_port = ssl_port;
  82. }
  83. void setProcessNum(int num) {
  84. this->worker_processes = num;
  85. }
  86. void setThreadNum(int num) {
  87. this->worker_threads = num;
  88. }
  89. int run(bool wait = true) {
  90. return http_server_run(this, wait);
  91. }
  92. int start() {
  93. return run(false);
  94. }
  95. int stop() {
  96. return http_server_stop(this);
  97. }
  98. };
  99. }
  100. #endif // HV_HTTP_SERVER_H_