httpd.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. #include "HttpServer.h"
  5. #include "http_api_test.h"
  6. #include "ssl_ctx.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. const char* szLoglevel = ini.GetValue("loglevel").c_str();
  56. int loglevel = LOG_LEVEL_DEBUG;
  57. if (stricmp(szLoglevel, "VERBOSE") == 0) {
  58. loglevel = LOG_LEVEL_VERBOSE;
  59. } else if (stricmp(szLoglevel, "DEBUG") == 0) {
  60. loglevel = LOG_LEVEL_DEBUG;
  61. } else if (stricmp(szLoglevel, "INFO") == 0) {
  62. loglevel = LOG_LEVEL_INFO;
  63. } else if (stricmp(szLoglevel, "WARN") == 0) {
  64. loglevel = LOG_LEVEL_WARN;
  65. } else if (stricmp(szLoglevel, "ERROR") == 0) {
  66. loglevel = LOG_LEVEL_ERROR;
  67. } else if (stricmp(szLoglevel, "FATAL") == 0) {
  68. loglevel = LOG_LEVEL_FATAL;
  69. } else if (stricmp(szLoglevel, "SILENT") == 0) {
  70. loglevel = LOG_LEVEL_SILENT;
  71. } else {
  72. loglevel = LOG_LEVEL_VERBOSE;
  73. }
  74. hlog_set_level(loglevel);
  75. // log_filesize
  76. str = ini.GetValue("log_filesize");
  77. if (!str.empty()) {
  78. int num = atoi(str.c_str());
  79. if (num > 0) {
  80. // 16 16M 16MB
  81. const char* p = str.c_str() + str.size() - 1;
  82. char unit;
  83. if (*p >= '0' && *p <= '9') unit = 'M';
  84. else if (*p == 'B') unit = *(p-1);
  85. else unit = *p;
  86. unsigned long long filesize = num;
  87. switch (unit) {
  88. case 'K': filesize <<= 10; break;
  89. case 'M': filesize <<= 20; break;
  90. case 'G': filesize <<= 30; break;
  91. default: filesize <<= 20; break;
  92. }
  93. hlog_set_max_filesize(filesize);
  94. }
  95. }
  96. // log_remain_days
  97. str = ini.GetValue("log_remain_days");
  98. if (!str.empty()) {
  99. hlog_set_remain_days(atoi(str.c_str()));
  100. }
  101. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  102. // worker_processes
  103. int worker_processes = 0;
  104. str = ini.GetValue("worker_processes");
  105. if (str.size() != 0) {
  106. if (strcmp(str.c_str(), "auto") == 0) {
  107. worker_processes = get_ncpu();
  108. hlogd("worker_processes=ncpu=%d", worker_processes);
  109. }
  110. else {
  111. worker_processes = atoi(str.c_str());
  112. }
  113. }
  114. g_http_server.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  115. // worker_threads
  116. int worker_threads = ini.Get<int>("worker_threads");
  117. g_http_server.worker_threads = LIMIT(0, worker_threads, 16);
  118. // port
  119. int port = 0;
  120. const char* szPort = get_arg("p");
  121. if (szPort) {
  122. port = atoi(szPort);
  123. }
  124. if (port == 0) {
  125. port = ini.Get<int>("port");
  126. }
  127. if (port == 0) {
  128. printf("Please config listen port!\n");
  129. exit(-10);
  130. }
  131. g_http_server.port = port;
  132. // http server
  133. // base_url
  134. str = ini.GetValue("base_url");
  135. if (str.size() != 0) {
  136. g_http_service.base_url = str;
  137. }
  138. // document_root
  139. str = ini.GetValue("document_root");
  140. if (str.size() != 0) {
  141. g_http_service.document_root = str;
  142. }
  143. // home_page
  144. str = ini.GetValue("home_page");
  145. if (str.size() != 0) {
  146. g_http_service.home_page = str;
  147. }
  148. // error_page
  149. str = ini.GetValue("error_page");
  150. if (str.size() != 0) {
  151. g_http_service.error_page = str;
  152. }
  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. str = ini.GetValue("ssl");
  160. if (getboolean(str.c_str())) {
  161. g_http_server.ssl = 1;
  162. std::string crt_file = ini.GetValue("ssl_certificate");
  163. std::string key_file = ini.GetValue("ssl_privatekey");
  164. std::string ca_file = ini.GetValue("ssl_ca_certificate");
  165. if (ssl_ctx_init(crt_file.c_str(), key_file.c_str(), ca_file.c_str()) != 0) {
  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. /*
  190. printf("---------------arg------------------------------\n");
  191. printf("%s\n", g_main_ctx.cmdline);
  192. for (auto& pair : g_main_ctx.arg_kv) {
  193. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  194. }
  195. for (auto& item : g_main_ctx.arg_list) {
  196. printf("%s\n", item.c_str());
  197. }
  198. printf("================================================\n");
  199. */
  200. /*
  201. printf("---------------env------------------------------\n");
  202. for (auto& pair : g_main_ctx.env_kv) {
  203. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  204. }
  205. printf("================================================\n");
  206. */
  207. // help
  208. if (get_arg("h")) {
  209. print_help();
  210. exit(0);
  211. }
  212. // version
  213. if (get_arg("v")) {
  214. print_version();
  215. exit(0);
  216. }
  217. // parse_confile
  218. const char* confile = get_arg("c");
  219. if (confile) {
  220. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  221. }
  222. parse_confile(g_main_ctx.confile);
  223. // test
  224. if (get_arg("t")) {
  225. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  226. exit(0);
  227. }
  228. // signal
  229. signal_init(on_reload);
  230. const char* signal = get_arg("s");
  231. if (signal) {
  232. signal_handle(signal);
  233. }
  234. #ifdef OS_UNIX
  235. // daemon
  236. if (get_arg("d")) {
  237. // nochdir, noclose
  238. int ret = daemon(1, 1);
  239. if (ret != 0) {
  240. printf("daemon error: %d\n", ret);
  241. exit(-10);
  242. }
  243. // parent process exit after daemon, so pid changed.
  244. g_main_ctx.pid = getpid();
  245. }
  246. #endif
  247. // pidfile
  248. create_pidfile();
  249. // HttpService
  250. g_http_service.preprocessor = http_api_preprocessor;
  251. g_http_service.postprocessor = http_api_postprocessor;
  252. #define XXX(path, method, handler) \
  253. g_http_service.AddApi(path, HTTP_##method, handler);
  254. HTTP_API_MAP(XXX)
  255. #undef XXX
  256. // http_server
  257. g_http_server.service = &g_http_service;
  258. ret = http_server_run(&g_http_server);
  259. return ret;
  260. }