You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.5KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <map>
  11. #include <signal.h>
  12. #include <iostream>
  13. #include "Util/SSLBox.h"
  14. #include "Util/logger.h"
  15. #include "Util/onceToken.h"
  16. #include "Util/NoticeCenter.h"
  17. #include "Network/TcpServer.h"
  18. #include "Poller/EventPoller.h"
  19. #include "Common/config.h"
  20. #include "Http/WebSocketSession.h"
  21. using namespace std;
  22. using namespace toolkit;
  23. using namespace mediakit;
  24. namespace mediakit {
  25. ////////////HTTP配置///////////
  26. namespace Http {
  27. #define HTTP_FIELD "http."
  28. #define HTTP_PORT 80
  29. const char kPort[] = HTTP_FIELD"port";
  30. #define HTTPS_PORT 443
  31. extern const char kSSLPort[] = HTTP_FIELD"sslport";
  32. onceToken token1([](){
  33. mINI::Instance()[kPort] = HTTP_PORT;
  34. mINI::Instance()[kSSLPort] = HTTPS_PORT;
  35. },nullptr);
  36. }//namespace Http
  37. } // namespace mediakit
  38. void initEventListener(){
  39. static onceToken s_token([](){
  40. NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastHttpRequest,[](BroadcastHttpRequestArgs){
  41. //const Parser &parser,HttpSession::HttpResponseInvoker &invoker,bool &consumed
  42. if(strstr(parser.Url().data(),"/api/") != parser.Url().data()){
  43. return;
  44. }
  45. //url以"/api/起始,说明是http api"
  46. consumed = true;//该http请求已被消费
  47. _StrPrinter printer;
  48. ////////////////method////////////////////
  49. printer << "\r\nmethod:\r\n\t" << parser.Method();
  50. ////////////////url/////////////////
  51. printer << "\r\nurl:\r\n\t" << parser.Url();
  52. ////////////////protocol/////////////////
  53. printer << "\r\nprotocol:\r\n\t" << parser.Tail();
  54. ///////////////args//////////////////
  55. printer << "\r\nargs:\r\n";
  56. for(auto &pr : parser.getUrlArgs()){
  57. printer << "\t" << pr.first << " : " << pr.second << "\r\n";
  58. }
  59. ///////////////header//////////////////
  60. printer << "\r\nheader:\r\n";
  61. for(auto &pr : parser.getHeader()){
  62. printer << "\t" << pr.first << " : " << pr.second << "\r\n";
  63. }
  64. ////////////////content/////////////////
  65. printer << "\r\ncontent:\r\n" << parser.Content();
  66. auto contentOut = printer << endl;
  67. ////////////////我们测算异步回复,当然你也可以同步回复/////////////////
  68. EventPollerPool::Instance().getPoller()->async([invoker,contentOut](){
  69. HttpSession::KeyValue headerOut;
  70. //你可以自定义header,如果跟默认header重名,则会覆盖之
  71. //默认header有:Server,Connection,Date,Content-Type,Content-Length
  72. //请勿覆盖Connection、Content-Length键
  73. //键名覆盖时不区分大小写
  74. headerOut["TestHeader"] = "HeaderValue";
  75. invoker(200,headerOut,contentOut);
  76. });
  77. });
  78. }, nullptr);
  79. }
  80. int main(int argc,char *argv[]){
  81. //设置退出信号处理函数
  82. static semaphore sem;
  83. signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
  84. //设置日志
  85. Logger::Instance().add(std::make_shared<ConsoleChannel>());
  86. Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
  87. //加载配置文件,如果配置文件不存在就创建一个
  88. loadIniConfig();
  89. initEventListener();
  90. //加载证书,证书包含公钥和私钥
  91. SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
  92. //信任某个自签名证书
  93. SSL_Initor::Instance().trustCertificate((exeDir() + "ssl.p12").data());
  94. //不忽略无效证书证书(例如自签名或过期证书)
  95. SSL_Initor::Instance().ignoreInvalidCertificate(false);
  96. //开启http服务器
  97. TcpServer::Ptr httpSrv(new TcpServer());
  98. httpSrv->start<HttpSession>(mINI::Instance()[Http::kPort]);//默认80
  99. //如果支持ssl,还可以开启https服务器
  100. TcpServer::Ptr httpsSrv(new TcpServer());
  101. httpsSrv->start<HttpsSession>(mINI::Instance()[Http::kSSLPort]);//默认443
  102. InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl;
  103. sem.wait();
  104. return 0;
  105. }