1
0

httpd.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. // index_of
  127. str = g_conf_ctx.parser->GetValue("index_of");
  128. if (str.size() != 0) {
  129. g_http_service.index_of = str;
  130. }
  131. return 0;
  132. }
  133. static void on_reload(void* userdata) {
  134. hlogi("reload confile [%s]", g_main_ctx.confile);
  135. parse_confile(g_main_ctx.confile);
  136. }
  137. int main(int argc, char** argv) {
  138. // g_main_ctx
  139. main_ctx_init(argc, argv);
  140. //int ret = parse_opt(argc, argv, options);
  141. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  142. if (ret != 0) {
  143. print_help();
  144. exit(ret);
  145. }
  146. /*
  147. printf("---------------arg------------------------------\n");
  148. printf("%s\n", g_main_ctx.cmdline);
  149. for (auto& pair : g_main_ctx.arg_kv) {
  150. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  151. }
  152. for (auto& item : g_main_ctx.arg_list) {
  153. printf("%s\n", item.c_str());
  154. }
  155. printf("================================================\n");
  156. */
  157. /*
  158. printf("---------------env------------------------------\n");
  159. for (auto& pair : g_main_ctx.env_kv) {
  160. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  161. }
  162. printf("================================================\n");
  163. */
  164. // help
  165. if (get_arg("h")) {
  166. print_help();
  167. exit(0);
  168. }
  169. // version
  170. if (get_arg("v")) {
  171. print_version();
  172. exit(0);
  173. }
  174. // g_conf_ctx
  175. conf_ctx_init(&g_conf_ctx);
  176. const char* confile = get_arg("c");
  177. if (confile) {
  178. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  179. }
  180. parse_confile(g_main_ctx.confile);
  181. // test
  182. if (get_arg("t")) {
  183. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  184. exit(0);
  185. }
  186. // signal
  187. signal_init(on_reload);
  188. const char* signal = get_arg("s");
  189. if (signal) {
  190. handle_signal(signal);
  191. }
  192. #ifdef OS_UNIX
  193. // daemon
  194. if (get_arg("d")) {
  195. // nochdir, noclose
  196. int ret = daemon(1, 1);
  197. if (ret != 0) {
  198. printf("daemon error: %d\n", ret);
  199. exit(-10);
  200. }
  201. // parent process exit after daemon, so pid changed.
  202. g_main_ctx.pid = getpid();
  203. }
  204. #endif
  205. // pidfile
  206. create_pidfile();
  207. // HttpService
  208. g_http_service.preprocessor = http_api_preprocessor;
  209. g_http_service.postprocessor = http_api_postprocessor;
  210. #define XXX(path, method, handler) \
  211. g_http_service.AddApi(path, HTTP_##method, handler);
  212. HTTP_API_MAP(XXX)
  213. #undef XXX
  214. // http_server
  215. http_server_t srv;
  216. srv.port = g_conf_ctx.port;
  217. srv.worker_processes = g_conf_ctx.worker_processes;
  218. srv.service = &g_http_service;
  219. ret = http_server_run(&srv);
  220. return ret;
  221. }