httpd.cpp 6.8 KB

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