1
0

HttpServer.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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; // http_port
  9. int https_port;
  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[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. http_version = 1;
  27. worker_processes = 0;
  28. worker_threads = 0;
  29. service = NULL;
  30. ws = NULL;
  31. listenfd[0] = listenfd[1] = -1;
  32. userdata = NULL;
  33. privdata = NULL;
  34. }
  35. #endif
  36. } http_server_t;
  37. /*
  38. #include "HttpServer.h"
  39. int main() {
  40. HttpService service;
  41. service.base_url = "/v1/api";
  42. service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
  43. resp->body = "pong";
  44. return 200;
  45. });
  46. http_server_t server;
  47. server.port = 8080;
  48. server.worker_processes = 4;
  49. server.service = &service;
  50. http_server_run(&server);
  51. return 0;
  52. }
  53. */
  54. HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
  55. // NOTE: stop all loops and join all threads
  56. HV_EXPORT int http_server_stop(http_server_t* server);
  57. #endif