HttpServer.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef HTTP_SERVER_H_
  2. #define HTTP_SERVER_H_
  3. #include "HttpService.h"
  4. typedef struct http_server_s {
  5. char host[64];
  6. int port;
  7. int ssl;
  8. int http_version;
  9. HttpService* service;
  10. int worker_processes;
  11. int worker_threads;
  12. void* userdata;
  13. //private:
  14. int listenfd;
  15. void* privdata;
  16. #ifdef __cplusplus
  17. http_server_s() {
  18. strcpy(host, "0.0.0.0");
  19. port = DEFAULT_HTTP_PORT;
  20. ssl = 0;
  21. http_version = 1;
  22. service = NULL;
  23. worker_processes = 0;
  24. worker_threads = 0;
  25. listenfd = -1;
  26. userdata = NULL;
  27. privdata = NULL;
  28. }
  29. #endif
  30. } http_server_t;
  31. /*
  32. #include "HttpServer.h"
  33. int http_api_hello(HttpRequest* req, HttpResponse* res) {
  34. res->body = "hello";
  35. return 0;
  36. }
  37. int main() {
  38. HttpService service;
  39. service.base_url = "/v1/api";
  40. service.AddApi("/hello", HTTP_GET, http_api_hello);
  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. int http_server_run(http_server_t* server, int wait = 1);
  50. // just for worker_processes = 0 && worker_threads <= 1
  51. int http_server_stop(http_server_t* server);
  52. #endif