httpd.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "h.h"
  2. #include "hmain.h"
  3. #include "httpd_conf.h"
  4. #include "http_server.h"
  5. #include "http_api_test.h"
  6. httpd_conf_ctx_t g_conf_ctx;
  7. HttpService g_http_service;
  8. static void print_version();
  9. static void print_help();
  10. static int parse_confile(const char* confile);
  11. // short options
  12. static const char options[] = "hvc:ts:dp:";
  13. // long options
  14. static const option_t long_options[] = {
  15. {'h', "help", NO_ARGUMENT},
  16. {'v', "version", NO_ARGUMENT},
  17. {'c', "confile", REQUIRED_ARGUMENT},
  18. {'t', "test", NO_ARGUMENT},
  19. {'s', "signal", REQUIRED_ARGUMENT},
  20. {'d', "daemon", NO_ARGUMENT},
  21. {'p', "port", REQUIRED_ARGUMENT}
  22. };
  23. static const char detail_options[] = R"(
  24. -h|--help Print this information
  25. -v|--version Print version
  26. -c|--confile <confile> Set configure file, default etc/{program}.conf
  27. -t|--test Test Configure file and exit
  28. -s|--signal <signal> Send <signal> to process,
  29. <signal>=[start,stop,restart,status,reload]
  30. -d|--daemon Daemonize
  31. -p|--port <port> Set listen port
  32. )";
  33. void print_version() {
  34. printf("%s version %s\n", g_main_ctx.program_name, get_compile_version());
  35. }
  36. void print_help() {
  37. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  38. printf("Options:\n%s\n", detail_options);
  39. }
  40. int parse_confile(const char* confile) {
  41. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  42. if (ret != 0) {
  43. printf("Load confile [%s] failed: %d\n", confile, ret);
  44. exit(-40);
  45. }
  46. // logfile
  47. string str = g_conf_ctx.parser->GetValue("logfile");
  48. if (!str.empty()) {
  49. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  50. }
  51. hlog_set_file(g_main_ctx.logfile);
  52. // loglevel
  53. const char* szLoglevel = g_conf_ctx.parser->GetValue("loglevel").c_str();
  54. if (stricmp(szLoglevel, "VERBOSE") == 0) {
  55. g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
  56. } else if (stricmp(szLoglevel, "DEBUG") == 0) {
  57. g_conf_ctx.loglevel = LOG_LEVEL_DEBUG;
  58. } else if (stricmp(szLoglevel, "INFO") == 0) {
  59. g_conf_ctx.loglevel = LOG_LEVEL_INFO;
  60. } else if (stricmp(szLoglevel, "WARN") == 0) {
  61. g_conf_ctx.loglevel = LOG_LEVEL_WARN;
  62. } else if (stricmp(szLoglevel, "ERROR") == 0) {
  63. g_conf_ctx.loglevel = LOG_LEVEL_ERROR;
  64. } else if (stricmp(szLoglevel, "FATAL") == 0) {
  65. g_conf_ctx.loglevel = LOG_LEVEL_FATAL;
  66. } else if (stricmp(szLoglevel, "SILENT") == 0) {
  67. g_conf_ctx.loglevel = LOG_LEVEL_SILENT;
  68. } else {
  69. g_conf_ctx.loglevel = LOG_LEVEL_VERBOSE;
  70. }
  71. hlog_set_level(g_conf_ctx.loglevel);
  72. // log_remain_days
  73. str = g_conf_ctx.parser->GetValue("log_remain_days");
  74. if (!str.empty()) {
  75. hlog_set_remain_days(atoi(str.c_str()));
  76. }
  77. hlogi("%s version: %s", g_main_ctx.program_name, get_compile_version());
  78. // worker_processes
  79. int worker_processes = 0;
  80. str = g_conf_ctx.parser->GetValue("worker_processes");
  81. if (str.size() != 0) {
  82. if (strcmp(str.c_str(), "auto") == 0) {
  83. worker_processes = get_ncpu();
  84. hlogd("worker_processes=ncpu=%d", worker_processes);
  85. }
  86. else {
  87. worker_processes = atoi(str.c_str());
  88. }
  89. }
  90. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  91. // port
  92. int port = 0;
  93. const char* szPort = get_arg("p");
  94. if (szPort) {
  95. port = atoi(szPort);
  96. }
  97. if (port == 0) {
  98. port = atoi(g_conf_ctx.parser->GetValue("port").c_str());
  99. }
  100. if (port == 0) {
  101. printf("Please config listen port!\n");
  102. exit(-10);
  103. }
  104. g_conf_ctx.port = port;
  105. // http server
  106. // base_url
  107. str = g_conf_ctx.parser->GetValue("base_url");
  108. if (str.size() != 0) {
  109. g_http_service.base_url = str;
  110. }
  111. // document_root
  112. str = g_conf_ctx.parser->GetValue("document_root");
  113. if (str.size() != 0) {
  114. g_http_service.document_root = str;
  115. }
  116. // home_page
  117. str = g_conf_ctx.parser->GetValue("home_page");
  118. if (str.size() != 0) {
  119. g_http_service.home_page = str;
  120. }
  121. // error_page
  122. str = g_conf_ctx.parser->GetValue("error_page");
  123. if (str.size() != 0) {
  124. g_http_service.error_page = str;
  125. }
  126. return 0;
  127. }
  128. static void on_reload(void* userdata) {
  129. hlogi("reload confile [%s]", g_main_ctx.confile);
  130. parse_confile(g_main_ctx.confile);
  131. }
  132. int main(int argc, char** argv) {
  133. // g_main_ctx
  134. main_ctx_init(argc, argv);
  135. //int ret = parse_opt(argc, argv, options);
  136. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  137. if (ret != 0) {
  138. print_help();
  139. exit(ret);
  140. }
  141. /*
  142. printf("---------------arg------------------------------\n");
  143. printf("%s\n", g_main_ctx.cmdline);
  144. for (auto& pair : g_main_ctx.arg_kv) {
  145. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  146. }
  147. for (auto& item : g_main_ctx.arg_list) {
  148. printf("%s\n", item.c_str());
  149. }
  150. printf("================================================\n");
  151. */
  152. /*
  153. printf("---------------env------------------------------\n");
  154. for (auto& pair : g_main_ctx.env_kv) {
  155. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  156. }
  157. printf("================================================\n");
  158. */
  159. // help
  160. if (get_arg("h")) {
  161. print_help();
  162. exit(0);
  163. }
  164. // version
  165. if (get_arg("v")) {
  166. print_version();
  167. exit(0);
  168. }
  169. // g_conf_ctx
  170. conf_ctx_init(&g_conf_ctx);
  171. const char* confile = get_arg("c");
  172. if (confile) {
  173. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  174. }
  175. parse_confile(g_main_ctx.confile);
  176. // test
  177. if (get_arg("t")) {
  178. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  179. exit(0);
  180. }
  181. // signal
  182. signal_init(on_reload);
  183. const char* signal = get_arg("s");
  184. if (signal) {
  185. handle_signal(signal);
  186. }
  187. #ifdef OS_UNIX
  188. // daemon
  189. if (get_arg("d")) {
  190. // nochdir, noclose
  191. int ret = daemon(1, 1);
  192. if (ret != 0) {
  193. printf("daemon error: %d\n", ret);
  194. exit(-10);
  195. }
  196. // parent process exit after daemon, so pid changed.
  197. g_main_ctx.pid = getpid();
  198. }
  199. #endif
  200. // pidfile
  201. create_pidfile();
  202. // HttpService
  203. g_http_service.preprocessor = http_api_preprocessor;
  204. g_http_service.postprocessor = http_api_postprocessor;
  205. #define XXX(path, method, handler) \
  206. g_http_service.AddApi(path, HTTP_##method, handler);
  207. HTTP_API_MAP(XXX)
  208. #undef XXX
  209. // http_server
  210. http_server_t srv;
  211. srv.port = g_conf_ctx.port;
  212. srv.worker_processes = g_conf_ctx.worker_processes;
  213. srv.service = &g_http_service;
  214. ret = http_server_run(&srv);
  215. return ret;
  216. }