HttpServer.h 1.3 KB

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