1
0

httpd.cpp 6.9 KB

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