1
0

httpd.cpp 7.0 KB

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