httpd.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * @介绍:httpd即HTTP服务端后台程序,该示例程序展示了如何使用libhv构建一个功能完备的HTTP服务端。
  3. * 打算使用libhv做HTTP服务端的同学建议通读此程序,你将受益匪浅。
  4. *
  5. */
  6. #include "hv.h"
  7. #include "hmain.h"
  8. #include "iniparser.h"
  9. #include "HttpServer.h"
  10. #include "router.h"
  11. http_server_t g_http_server;
  12. HttpService g_http_service;
  13. static void print_version();
  14. static void print_help();
  15. static int parse_confile(const char* confile);
  16. // short options
  17. static const char options[] = "hvc:ts:dp:";
  18. // long options
  19. static const option_t long_options[] = {
  20. {'h', "help", NO_ARGUMENT},
  21. {'v', "version", NO_ARGUMENT},
  22. {'c', "confile", REQUIRED_ARGUMENT},
  23. {'t', "test", NO_ARGUMENT},
  24. {'s', "signal", REQUIRED_ARGUMENT},
  25. {'d', "daemon", NO_ARGUMENT},
  26. {'p', "port", REQUIRED_ARGUMENT}
  27. };
  28. static const char detail_options[] = R"(
  29. -h|--help Print this information
  30. -v|--version Print version
  31. -c|--confile <confile> Set configure file, default etc/{program}.conf
  32. -t|--test Test configure file and exit
  33. -s|--signal <signal> Send <signal> to process,
  34. <signal>=[start,stop,restart,status,reload]
  35. -d|--daemon Daemonize
  36. -p|--port <port> Set listen port
  37. )";
  38. void print_version() {
  39. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  40. }
  41. void print_help() {
  42. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  43. printf("Options:\n%s\n", detail_options);
  44. }
  45. // 解析配置文件
  46. int parse_confile(const char* confile) {
  47. // 加载配置文件
  48. IniParser ini;
  49. int ret = ini.LoadFromFile(confile);
  50. if (ret != 0) {
  51. printf("Load confile [%s] failed: %d\n", confile, ret);
  52. exit(-40);
  53. }
  54. // 设置日志文件
  55. // logfile
  56. string str = ini.GetValue("logfile");
  57. if (!str.empty()) {
  58. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  59. }
  60. hlog_set_file(g_main_ctx.logfile);
  61. // 设置日志等级
  62. // loglevel
  63. str = ini.GetValue("loglevel");
  64. if (!str.empty()) {
  65. hlog_set_level_by_str(str.c_str());
  66. }
  67. // 设置日志文件大小
  68. // log_filesize
  69. str = ini.GetValue("log_filesize");
  70. if (!str.empty()) {
  71. hlog_set_max_filesize_by_str(str.c_str());
  72. }
  73. // 设置日志保留天数
  74. // log_remain_days
  75. str = ini.GetValue("log_remain_days");
  76. if (!str.empty()) {
  77. hlog_set_remain_days(atoi(str.c_str()));
  78. }
  79. // 是否启用fsync强制刷新日志缓存到磁盘
  80. // log_fsync
  81. str = ini.GetValue("log_fsync");
  82. if (!str.empty()) {
  83. logger_enable_fsync(hlog, getboolean(str.c_str()));
  84. }
  85. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  86. hlog_fsync();
  87. // 设置工作进程数
  88. // worker_processes
  89. int worker_processes = 0;
  90. str = ini.GetValue("worker_processes");
  91. if (str.size() != 0) {
  92. if (strcmp(str.c_str(), "auto") == 0) {
  93. worker_processes = get_ncpu();
  94. hlogd("worker_processes=ncpu=%d", worker_processes);
  95. }
  96. else {
  97. worker_processes = atoi(str.c_str());
  98. }
  99. }
  100. g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  101. // 设置工作进程数
  102. // worker_threads
  103. int worker_threads = ini.Get<int>("worker_threads");
  104. g_http_server.worker_threads = LIMIT(0, worker_threads, 16);
  105. // 设置http监听端口
  106. // http_port
  107. int port = 0;
  108. const char* szPort = get_arg("p");
  109. if (szPort) {
  110. port = atoi(szPort);
  111. }
  112. if (port == 0) {
  113. port = ini.Get<int>("port");
  114. }
  115. if (port == 0) {
  116. port = ini.Get<int>("http_port");
  117. }
  118. g_http_server.port = port;
  119. // 设置https监听端口
  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. // 设置url前缀
  129. // base_url
  130. str = ini.GetValue("base_url");
  131. if (str.size() != 0) {
  132. g_http_service.base_url = str;
  133. }
  134. // 设置html文档根目录
  135. // document_root
  136. str = ini.GetValue("document_root");
  137. if (str.size() != 0) {
  138. g_http_service.document_root = str;
  139. }
  140. // 设置首页
  141. // home_page
  142. str = ini.GetValue("home_page");
  143. if (str.size() != 0) {
  144. g_http_service.home_page = str;
  145. }
  146. // 设置错误页面
  147. // error_page
  148. str = ini.GetValue("error_page");
  149. if (str.size() != 0) {
  150. g_http_service.error_page = str;
  151. }
  152. // 设置indexof目录
  153. // index_of
  154. str = ini.GetValue("index_of");
  155. if (str.size() != 0) {
  156. g_http_service.index_of = str;
  157. }
  158. // 设置SSL证书
  159. // ssl
  160. if (g_http_server.https_port > 0) {
  161. std::string crt_file = ini.GetValue("ssl_certificate");
  162. std::string key_file = ini.GetValue("ssl_privatekey");
  163. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  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. if (hssl_ctx_init(&param) == NULL) {
  170. hloge("SSL certificate verify failed!");
  171. exit(0);
  172. }
  173. else {
  174. hlogi("SSL certificate verify ok!");
  175. }
  176. }
  177. hlogi("parse_confile('%s') OK", confile);
  178. return 0;
  179. }
  180. // reload信号处理: 重新加载配置文件
  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. // 解析命令行
  189. //int ret = parse_opt(argc, argv, options);
  190. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  191. if (ret != 0) {
  192. print_help();
  193. exit(ret);
  194. }
  195. /*
  196. printf("---------------arg------------------------------\n");
  197. printf("%s\n", g_main_ctx.cmdline);
  198. for (auto& pair : g_main_ctx.arg_kv) {
  199. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  200. }
  201. for (auto& item : g_main_ctx.arg_list) {
  202. printf("%s\n", item.c_str());
  203. }
  204. printf("================================================\n");
  205. */
  206. /*
  207. printf("---------------env------------------------------\n");
  208. for (auto& pair : g_main_ctx.env_kv) {
  209. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  210. }
  211. printf("================================================\n");
  212. */
  213. // -h 打印帮助信息
  214. // help
  215. if (get_arg("h")) {
  216. print_help();
  217. exit(0);
  218. }
  219. // -v 打印版本信息
  220. // version
  221. if (get_arg("v")) {
  222. print_version();
  223. exit(0);
  224. }
  225. // -c 设置配置文件
  226. // parse_confile
  227. const char* confile = get_arg("c");
  228. if (confile) {
  229. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  230. }
  231. // 解析配置文件
  232. parse_confile(g_main_ctx.confile);
  233. // -t 测试配置文件
  234. // test
  235. if (get_arg("t")) {
  236. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  237. exit(0);
  238. }
  239. // -s 信号处理
  240. // signal
  241. signal_init(on_reload);
  242. const char* signal = get_arg("s");
  243. if (signal) {
  244. signal_handle(signal);
  245. }
  246. #ifdef OS_UNIX
  247. // -d 后台运行
  248. // daemon
  249. if (get_arg("d")) {
  250. // nochdir, noclose
  251. int ret = daemon(1, 1);
  252. if (ret != 0) {
  253. printf("daemon error: %d\n", ret);
  254. exit(-10);
  255. }
  256. }
  257. #endif
  258. // 创建pid文件
  259. // pidfile
  260. create_pidfile();
  261. // http_server
  262. // 注册路由
  263. Router::Register(g_http_service);
  264. g_http_server.service = &g_http_service;
  265. // 运行http服务
  266. ret = http_server_run(&g_http_server);
  267. return ret;
  268. }