httpd.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. #include "HttpServer.h"
  5. #include "router.h"
  6. http_server_t g_http_server;
  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, hv_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. IniParser ini;
  42. int ret = ini.LoadFromFile(confile);
  43. if (ret != 0) {
  44. printf("Load confile [%s] failed: %d\n", confile, ret);
  45. exit(-40);
  46. }
  47. // logfile
  48. string str = ini.GetValue("logfile");
  49. if (!str.empty()) {
  50. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  51. }
  52. hlog_set_file(g_main_ctx.logfile);
  53. // loglevel
  54. str = ini.GetValue("loglevel");
  55. if (!str.empty()) {
  56. hlog_set_level_by_str(str.c_str());
  57. }
  58. // log_filesize
  59. str = ini.GetValue("log_filesize");
  60. if (!str.empty()) {
  61. hlog_set_max_filesize_by_str(str.c_str());
  62. }
  63. // log_remain_days
  64. str = ini.GetValue("log_remain_days");
  65. if (!str.empty()) {
  66. hlog_set_remain_days(atoi(str.c_str()));
  67. }
  68. // log_fsync
  69. str = ini.GetValue("log_fsync");
  70. if (!str.empty()) {
  71. logger_enable_fsync(hlog, getboolean(str.c_str()));
  72. }
  73. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  74. hlog_fsync();
  75. // worker_processes
  76. int worker_processes = 0;
  77. str = ini.GetValue("worker_processes");
  78. if (str.size() != 0) {
  79. if (strcmp(str.c_str(), "auto") == 0) {
  80. worker_processes = get_ncpu();
  81. hlogd("worker_processes=ncpu=%d", worker_processes);
  82. }
  83. else {
  84. worker_processes = atoi(str.c_str());
  85. }
  86. }
  87. g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  88. // worker_threads
  89. int worker_threads = ini.Get<int>("worker_threads");
  90. g_http_server.worker_threads = LIMIT(0, worker_threads, 16);
  91. // http_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 = ini.Get<int>("port");
  99. }
  100. if (port == 0) {
  101. port = ini.Get<int>("http_port");
  102. }
  103. g_http_server.port = port;
  104. // https_port
  105. if (HV_WITH_SSL) {
  106. g_http_server.https_port = ini.Get<int>("https_port");
  107. }
  108. if (g_http_server.port == 0 && g_http_server.https_port == 0) {
  109. printf("Please config listen port!\n");
  110. exit(-10);
  111. }
  112. // base_url
  113. str = ini.GetValue("base_url");
  114. if (str.size() != 0) {
  115. g_http_service.base_url = str;
  116. }
  117. // document_root
  118. str = ini.GetValue("document_root");
  119. if (str.size() != 0) {
  120. g_http_service.document_root = str;
  121. }
  122. // home_page
  123. str = ini.GetValue("home_page");
  124. if (str.size() != 0) {
  125. g_http_service.home_page = str;
  126. }
  127. // error_page
  128. str = ini.GetValue("error_page");
  129. if (str.size() != 0) {
  130. g_http_service.error_page = str;
  131. }
  132. // index_of
  133. str = ini.GetValue("index_of");
  134. if (str.size() != 0) {
  135. g_http_service.index_of = str;
  136. }
  137. // ssl
  138. if (g_http_server.https_port > 0) {
  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. hssl_ctx_init_param_t param;
  143. memset(&param, 0, sizeof(param));
  144. param.crt_file = crt_file.c_str();
  145. param.key_file = key_file.c_str();
  146. param.ca_file = ca_file.c_str();
  147. if (hssl_ctx_init(&param) == NULL) {
  148. hloge("SSL certificate verify failed!");
  149. exit(0);
  150. }
  151. else {
  152. hlogi("SSL certificate verify ok!");
  153. }
  154. }
  155. hlogi("parse_confile('%s') OK", confile);
  156. return 0;
  157. }
  158. static void on_reload(void* userdata) {
  159. hlogi("reload confile [%s]", g_main_ctx.confile);
  160. parse_confile(g_main_ctx.confile);
  161. }
  162. int main(int argc, char** argv) {
  163. // g_main_ctx
  164. main_ctx_init(argc, argv);
  165. //int ret = parse_opt(argc, argv, options);
  166. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  167. if (ret != 0) {
  168. print_help();
  169. exit(ret);
  170. }
  171. /*
  172. printf("---------------arg------------------------------\n");
  173. printf("%s\n", g_main_ctx.cmdline);
  174. for (auto& pair : g_main_ctx.arg_kv) {
  175. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  176. }
  177. for (auto& item : g_main_ctx.arg_list) {
  178. printf("%s\n", item.c_str());
  179. }
  180. printf("================================================\n");
  181. */
  182. /*
  183. printf("---------------env------------------------------\n");
  184. for (auto& pair : g_main_ctx.env_kv) {
  185. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  186. }
  187. printf("================================================\n");
  188. */
  189. // help
  190. if (get_arg("h")) {
  191. print_help();
  192. exit(0);
  193. }
  194. // version
  195. if (get_arg("v")) {
  196. print_version();
  197. exit(0);
  198. }
  199. // parse_confile
  200. const char* confile = get_arg("c");
  201. if (confile) {
  202. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  203. }
  204. parse_confile(g_main_ctx.confile);
  205. // test
  206. if (get_arg("t")) {
  207. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  208. exit(0);
  209. }
  210. // signal
  211. signal_init(on_reload);
  212. const char* signal = get_arg("s");
  213. if (signal) {
  214. signal_handle(signal);
  215. }
  216. #ifdef OS_UNIX
  217. // daemon
  218. if (get_arg("d")) {
  219. // nochdir, noclose
  220. int ret = daemon(1, 1);
  221. if (ret != 0) {
  222. printf("daemon error: %d\n", ret);
  223. exit(-10);
  224. }
  225. }
  226. #endif
  227. // pidfile
  228. create_pidfile();
  229. // http_server
  230. Router::Register(g_http_service);
  231. g_http_server.service = &g_http_service;
  232. ret = http_server_run(&g_http_server);
  233. return ret;
  234. }