httpd.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. g_http_server.https_port = ini.Get<int>("https_port");
  106. if (g_http_server.port == 0 && g_http_server.https_port == 0) {
  107. printf("Please config listen port!\n");
  108. exit(-10);
  109. }
  110. // base_url
  111. str = ini.GetValue("base_url");
  112. if (str.size() != 0) {
  113. g_http_service.base_url = str;
  114. }
  115. // document_root
  116. str = ini.GetValue("document_root");
  117. if (str.size() != 0) {
  118. g_http_service.document_root = str;
  119. }
  120. // home_page
  121. str = ini.GetValue("home_page");
  122. if (str.size() != 0) {
  123. g_http_service.home_page = str;
  124. }
  125. // error_page
  126. str = ini.GetValue("error_page");
  127. if (str.size() != 0) {
  128. g_http_service.error_page = str;
  129. }
  130. // index_of
  131. str = ini.GetValue("index_of");
  132. if (str.size() != 0) {
  133. g_http_service.index_of = str;
  134. }
  135. // ssl
  136. if (g_http_server.https_port > 0 && HV_WITH_SSL) {
  137. std::string crt_file = ini.GetValue("ssl_certificate");
  138. std::string key_file = ini.GetValue("ssl_privatekey");
  139. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  140. hssl_ctx_init_param_t param;
  141. memset(&param, 0, sizeof(param));
  142. param.crt_file = crt_file.c_str();
  143. param.key_file = key_file.c_str();
  144. param.ca_file = ca_file.c_str();
  145. if (hssl_ctx_init(&param) == NULL) {
  146. hloge("SSL certificate verify failed!");
  147. exit(0);
  148. }
  149. else {
  150. hlogi("SSL certificate verify ok!");
  151. }
  152. }
  153. hlogi("parse_confile('%s') OK", confile);
  154. return 0;
  155. }
  156. static void on_reload(void* userdata) {
  157. hlogi("reload confile [%s]", g_main_ctx.confile);
  158. parse_confile(g_main_ctx.confile);
  159. }
  160. int main(int argc, char** argv) {
  161. // g_main_ctx
  162. main_ctx_init(argc, argv);
  163. //int ret = parse_opt(argc, argv, options);
  164. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  165. if (ret != 0) {
  166. print_help();
  167. exit(ret);
  168. }
  169. /*
  170. printf("---------------arg------------------------------\n");
  171. printf("%s\n", g_main_ctx.cmdline);
  172. for (auto& pair : g_main_ctx.arg_kv) {
  173. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  174. }
  175. for (auto& item : g_main_ctx.arg_list) {
  176. printf("%s\n", item.c_str());
  177. }
  178. printf("================================================\n");
  179. */
  180. /*
  181. printf("---------------env------------------------------\n");
  182. for (auto& pair : g_main_ctx.env_kv) {
  183. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  184. }
  185. printf("================================================\n");
  186. */
  187. // help
  188. if (get_arg("h")) {
  189. print_help();
  190. exit(0);
  191. }
  192. // version
  193. if (get_arg("v")) {
  194. print_version();
  195. exit(0);
  196. }
  197. // parse_confile
  198. const char* confile = get_arg("c");
  199. if (confile) {
  200. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  201. }
  202. parse_confile(g_main_ctx.confile);
  203. // test
  204. if (get_arg("t")) {
  205. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  206. exit(0);
  207. }
  208. // signal
  209. signal_init(on_reload);
  210. const char* signal = get_arg("s");
  211. if (signal) {
  212. signal_handle(signal);
  213. }
  214. #ifdef OS_UNIX
  215. // daemon
  216. if (get_arg("d")) {
  217. // nochdir, noclose
  218. int ret = daemon(1, 1);
  219. if (ret != 0) {
  220. printf("daemon error: %d\n", ret);
  221. exit(-10);
  222. }
  223. }
  224. #endif
  225. // pidfile
  226. create_pidfile();
  227. // http_server
  228. Router::Register(g_http_service);
  229. g_http_server.service = &g_http_service;
  230. ret = http_server_run(&g_http_server);
  231. return ret;
  232. }