httpd.cpp 6.7 KB

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