service.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <library/cpp/coroutine/engine/impl.h>
  3. #include <library/cpp/coroutine/listener/listen.h>
  4. #include <library/cpp/http/fetch/httpheader.h>
  5. #include <library/cpp/http/server/http.h>
  6. #include <library/cpp/logger/all.h>
  7. #include <util/network/ip.h>
  8. #include <library/cpp/cgiparam/cgiparam.h>
  9. #include <functional>
  10. struct TMonitor;
  11. namespace NMonitoring {
  12. struct IHttpRequest {
  13. virtual ~IHttpRequest() {
  14. }
  15. virtual const char* GetURI() const = 0;
  16. virtual const char* GetPath() const = 0;
  17. virtual const TCgiParameters& GetParams() const = 0;
  18. virtual const TCgiParameters& GetPostParams() const = 0;
  19. virtual TStringBuf GetPostContent() const = 0;
  20. virtual HTTP_METHOD GetMethod() const = 0;
  21. virtual const THttpHeaders& GetHeaders() const = 0;
  22. virtual TString GetRemoteAddr() const = 0;
  23. };
  24. // first param - output stream to write result to
  25. // second param - URL of request
  26. typedef std::function<void(IOutputStream&, const IHttpRequest&)> THandler;
  27. class TCoHttpServer: private TContListener::ICallBack {
  28. public:
  29. // initialize and schedule coroutines for execution
  30. TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler);
  31. void Start();
  32. void Stop();
  33. // this function implements THandler interface
  34. // by forwarding it to the httpserver
  35. // @note this call may be blocking; don't use inside coroutines
  36. // @throws may throw in case of connection error, etc
  37. void ProcessRequest(IOutputStream&, const IHttpRequest&);
  38. private:
  39. class TConnection;
  40. // ICallBack implementation
  41. void OnAcceptFull(const TAcceptFull& a) override;
  42. void OnError() override;
  43. private:
  44. TContExecutor& Executor;
  45. TContListener Listener;
  46. THandler Handler;
  47. TString BindAddr;
  48. TIpPort Port;
  49. };
  50. class TMtHttpServer: public THttpServer, private THttpServer::ICallBack {
  51. public:
  52. TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool = nullptr);
  53. TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool);
  54. ~TMtHttpServer() override {
  55. Stop();
  56. }
  57. /**
  58. * This will cause the server start to accept incoming connections.
  59. *
  60. * @return true if the port binding was successfull,
  61. * false otherwise.
  62. */
  63. bool Start();
  64. /**
  65. * Same as Start() member-function, but will throw TSystemError if
  66. * there were some errors.
  67. */
  68. void StartOrThrow();
  69. /**
  70. * Stops the server from accepting new connections.
  71. */
  72. void Stop();
  73. private:
  74. class TConnection;
  75. TClientRequest* CreateClient() override;
  76. THandler Handler;
  77. };
  78. // this class implements hybrid coroutine and threaded approach
  79. // requests for main page which holds counters and simple tables are served in a thread
  80. // requests for other pages which include access with inter-thread synchonization
  81. // will be served in a coroutine context
  82. class TMonService {
  83. public:
  84. TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort,
  85. THandler coHandler, THandler mtHandler);
  86. void Start();
  87. void Stop();
  88. protected:
  89. void DispatchRequest(IOutputStream& out, const IHttpRequest&);
  90. private:
  91. TCoHttpServer CoServer;
  92. TMtHttpServer MtServer;
  93. THandler MtHandler;
  94. };
  95. }