1
0

httpd.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, 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. // ssl
  154. if (g_http_server.https_port > 0) {
  155. std::string crt_file = ini.GetValue("ssl_certificate");
  156. std::string key_file = ini.GetValue("ssl_privatekey");
  157. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  158. hlogi("SSL backend is %s", hssl_backend());
  159. hssl_ctx_init_param_t param;
  160. memset(&param, 0, sizeof(param));
  161. param.crt_file = crt_file.c_str();
  162. param.key_file = key_file.c_str();
  163. param.ca_file = ca_file.c_str();
  164. param.endpoint = HSSL_SERVER;
  165. if (hssl_ctx_init(&param) == NULL) {
  166. hloge("SSL certificate verify failed!");
  167. exit(0);
  168. }
  169. else {
  170. hlogi("SSL certificate verify ok!");
  171. }
  172. }
  173. hlogi("parse_confile('%s') OK", confile);
  174. return 0;
  175. }
  176. static void on_reload(void* userdata) {
  177. hlogi("reload confile [%s]", g_main_ctx.confile);
  178. parse_confile(g_main_ctx.confile);
  179. }
  180. int main(int argc, char** argv) {
  181. // g_main_ctx
  182. main_ctx_init(argc, argv);
  183. //int ret = parse_opt(argc, argv, options);
  184. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  185. if (ret != 0) {
  186. print_help();
  187. exit(ret);
  188. }
  189. // help
  190. if (get_arg("h")) {
  191. print_help();
  192. exit(0);
  193. }
  194. // version
  195. if (get_arg("v")) {
  196. print_version();
  197. exit(0);
  198. }
  199. // parse_confile
  200. const char* confile = get_arg("c");
  201. if (confile) {
  202. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  203. }
  204. parse_confile(g_main_ctx.confile);
  205. // test
  206. if (get_arg("t")) {
  207. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  208. exit(0);
  209. }
  210. // signal
  211. signal_init(on_reload);
  212. const char* signal = get_arg("s");
  213. if (signal) {
  214. signal_handle(signal);
  215. }
  216. #ifdef OS_UNIX
  217. // daemon
  218. if (get_arg("d")) {
  219. // nochdir, noclose
  220. int ret = daemon(1, 1);
  221. if (ret != 0) {
  222. printf("daemon error: %d\n", ret);
  223. exit(-10);
  224. }
  225. }
  226. #endif
  227. // pidfile
  228. create_pidfile();
  229. // http_server
  230. Router::Register(g_http_service);
  231. g_http_server.registerHttpService(&g_http_service);
  232. g_http_server.run();
  233. return ret;
  234. }