server_main.cc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include "workflow/HttpMessage.h"
  4. #include "workflow/HttpUtil.h"
  5. #include "workflow/WFHttpServer.h"
  6. #include "workflow/WFFacilities.h"
  7. #include "config/config.h"
  8. #include "file_service.h"
  9. using namespace protocol;
  10. static WFFacilities::WaitGroup wait_group(1);
  11. static srpc::RPCConfig config;
  12. void sig_handler(int signo)
  13. {
  14. wait_group.done();
  15. }
  16. void init()
  17. {
  18. if (config.load("./server.conf") == false)
  19. {
  20. perror("Load config failed");
  21. exit(1);
  22. }
  23. signal(SIGINT, sig_handler);
  24. }
  25. int main()
  26. {
  27. init();
  28. unsigned short port = config.server_port();
  29. const char *cert_file = config.server_cert_file();
  30. const char *file_key = config.server_file_key();
  31. FileService service(config.get_root_path(), config.get_error_page());
  32. auto&& proc = std::bind(&FileService::process, &service,
  33. std::placeholders::_1);
  34. int ret;
  35. WFHttpServer server(proc);
  36. if (strlen(cert_file) != 0 && strlen(file_key) != 0)
  37. ret = server.start(port, cert_file, file_key);
  38. else
  39. ret = server.start(port);
  40. if (ret == 0)
  41. {
  42. fprintf(stderr, "http file service start, port %u\n", port);
  43. wait_group.wait();
  44. server.stop();
  45. }
  46. else
  47. perror("http file service start");
  48. return 0;
  49. }