monservice.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. #include "service.h"
  3. #include "auth.h"
  4. #include "mon_service_http_request.h"
  5. #include <library/cpp/monlib/service/pages/index_mon_page.h>
  6. #include <library/cpp/monlib/service/pages/mon_page.h>
  7. #include <util/system/progname.h>
  8. #include <functional>
  9. namespace NMonitoring {
  10. class TMonService2: public TMtHttpServer {
  11. protected:
  12. const TString Title;
  13. char StartTime[26];
  14. TIntrusivePtr<TIndexMonPage> IndexMonPage;
  15. THolder<IAuthProvider> AuthProvider_;
  16. public:
  17. static THttpServerOptions HttpServerOptions(ui16 port, const TString& host, ui32 threads) {
  18. THttpServerOptions opts(port);
  19. if (!host.empty()) {
  20. opts.SetHost(host);
  21. }
  22. opts.SetClientTimeout(TDuration::Minutes(1));
  23. opts.EnableCompression(true);
  24. opts.SetThreads(threads);
  25. opts.SetMaxConnections(std::max<ui32>(100, threads));
  26. opts.EnableRejectExcessConnections(true);
  27. return opts;
  28. }
  29. static THttpServerOptions HttpServerOptions(ui16 port, ui32 threads) {
  30. return HttpServerOptions(port, TString(), threads);
  31. }
  32. public:
  33. explicit TMonService2(ui16 port, const TString& title = GetProgramName(), THolder<IAuthProvider> auth = nullptr);
  34. explicit TMonService2(ui16 port, ui32 threads, const TString& title = GetProgramName(), THolder<IAuthProvider> auth = nullptr);
  35. explicit TMonService2(ui16 port, const TString& host, ui32 threads, const TString& title = GetProgramName(), THolder<IAuthProvider> auth = nullptr);
  36. explicit TMonService2(const THttpServerOptions& options, const TString& title = GetProgramName(), THolder<IAuthProvider> auth = nullptr);
  37. explicit TMonService2(const THttpServerOptions& options, TSimpleSharedPtr<IThreadPool> pool, const TString& title = GetProgramName(), THolder<IAuthProvider> auth = nullptr);
  38. ~TMonService2() override {
  39. }
  40. const char* GetStartTime() const {
  41. return StartTime;
  42. }
  43. const TString& GetTitle() const {
  44. return Title;
  45. }
  46. virtual void ServeRequest(IOutputStream& out, const NMonitoring::IHttpRequest& request);
  47. virtual void OutputIndex(IOutputStream& out);
  48. virtual void OutputIndexPage(IOutputStream& out);
  49. virtual void OutputIndexBody(IOutputStream& out);
  50. void Register(IMonPage* page);
  51. void Register(TMonPagePtr page);
  52. TIndexMonPage* RegisterIndexPage(const TString& path, const TString& title);
  53. IMonPage* FindPage(const TString& relativePath);
  54. TIndexMonPage* FindIndexPage(const TString& relativePath);
  55. void SortPages();
  56. TIndexMonPage* GetRoot() {
  57. return IndexMonPage.Get();
  58. }
  59. };
  60. }