HttpServer.h 1.2 KB

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