1
0

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