1
0

httpd.cpp 7.3 KB

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