HttpServer.h 2.6 KB

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