HttpServer.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef HV_HTTP_SERVER_H_
  2. #define HV_HTTP_SERVER_H_
  3. #include "hexport.h"
  4. #include "HttpService.h"
  5. struct WebSocketService;
  6. typedef struct http_server_s {
  7. char host[64];
  8. int port; // http_port
  9. int https_port;
  10. int http_version;
  11. int worker_processes;
  12. int worker_threads;
  13. HttpService* service;
  14. WebSocketService* ws;
  15. void* userdata;
  16. //private:
  17. int listenfd[2]; // 0: http, 1: https
  18. void* privdata;
  19. #ifdef __cplusplus
  20. http_server_s() {
  21. strcpy(host, "0.0.0.0");
  22. // port = DEFAULT_HTTP_PORT;
  23. // https_port = DEFAULT_HTTPS_PORT;
  24. // port = 8080;
  25. // https_port = 8443;
  26. port = https_port = 0;
  27. http_version = 1;
  28. worker_processes = 0;
  29. worker_threads = 0;
  30. service = NULL;
  31. ws = NULL;
  32. listenfd[0] = listenfd[1] = -1;
  33. userdata = NULL;
  34. privdata = NULL;
  35. }
  36. #endif
  37. } http_server_t;
  38. /*
  39. #include "HttpServer.h"
  40. int main() {
  41. HttpService service;
  42. service.base_url = "/api/v1";
  43. service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  44. resp->body = "pong";
  45. return 200;
  46. });
  47. http_server_t server;
  48. server.port = 8080;
  49. server.worker_processes = 4;
  50. server.service = &service;
  51. http_server_run(&server);
  52. return 0;
  53. }
  54. */
  55. HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
  56. // NOTE: stop all loops and join all threads
  57. HV_EXPORT int http_server_stop(http_server_t* server);
  58. #endif // HV_HTTP_SERVER_H_