1
0

httpd.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. #include "HttpServer.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 = ini.Get<int>("port");
  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 (getboolean(str.c_str())) {
  137. g_http_server.ssl = 1;
  138. std::string crt_file = ini.GetValue("ssl_certificate");
  139. std::string key_file = ini.GetValue("ssl_privatekey");
  140. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  141. if (ssl_ctx_init(crt_file.c_str(), key_file.c_str(), ca_file.c_str()) != 0) {
  142. hloge("SSL certificate verify failed!");
  143. exit(0);
  144. }
  145. else {
  146. hlogi("SSL certificate verify ok!");
  147. }
  148. }
  149. return 0;
  150. }
  151. static void on_reload(void* userdata) {
  152. hlogi("reload confile [%s]", g_main_ctx.confile);
  153. parse_confile(g_main_ctx.confile);
  154. }
  155. int main(int argc, char** argv) {
  156. // g_main_ctx
  157. main_ctx_init(argc, argv);
  158. //int ret = parse_opt(argc, argv, options);
  159. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  160. if (ret != 0) {
  161. print_help();
  162. exit(ret);
  163. }
  164. /*
  165. printf("---------------arg------------------------------\n");
  166. printf("%s\n", g_main_ctx.cmdline);
  167. for (auto& pair : g_main_ctx.arg_kv) {
  168. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  169. }
  170. for (auto& item : g_main_ctx.arg_list) {
  171. printf("%s\n", item.c_str());
  172. }
  173. printf("================================================\n");
  174. */
  175. /*
  176. printf("---------------env------------------------------\n");
  177. for (auto& pair : g_main_ctx.env_kv) {
  178. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  179. }
  180. printf("================================================\n");
  181. */
  182. // help
  183. if (get_arg("h")) {
  184. print_help();
  185. exit(0);
  186. }
  187. // version
  188. if (get_arg("v")) {
  189. print_version();
  190. exit(0);
  191. }
  192. // parse_confile
  193. const char* confile = get_arg("c");
  194. if (confile) {
  195. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  196. }
  197. parse_confile(g_main_ctx.confile);
  198. // test
  199. if (get_arg("t")) {
  200. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  201. exit(0);
  202. }
  203. // signal
  204. signal_init(on_reload);
  205. const char* signal = get_arg("s");
  206. if (signal) {
  207. handle_signal(signal);
  208. }
  209. #ifdef OS_UNIX
  210. // daemon
  211. if (get_arg("d")) {
  212. // nochdir, noclose
  213. int ret = daemon(1, 1);
  214. if (ret != 0) {
  215. printf("daemon error: %d\n", ret);
  216. exit(-10);
  217. }
  218. // parent process exit after daemon, so pid changed.
  219. g_main_ctx.pid = getpid();
  220. }
  221. #endif
  222. // pidfile
  223. create_pidfile();
  224. // HttpService
  225. g_http_service.preprocessor = http_api_preprocessor;
  226. g_http_service.postprocessor = http_api_postprocessor;
  227. #define XXX(path, method, handler) \
  228. g_http_service.AddApi(path, HTTP_##method, handler);
  229. HTTP_API_MAP(XXX)
  230. #undef XXX
  231. // http_server
  232. g_http_server.service = &g_http_service;
  233. ret = http_server_run(&g_http_server);
  234. return ret;
  235. }