#include "monservice.h" #include #include #include #include #include #include #include using namespace NMonitoring; TMonService2::TMonService2(ui16 port, const TString& host, ui32 threads, const TString& title, THolder auth) : TMonService2(HttpServerOptions(port, host, threads), title, std::move(auth)) { } TMonService2::TMonService2(const THttpServerOptions& options, const TString& title, THolder auth) : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2)) , Title(title) , IndexMonPage(new TIndexMonPage("", Title)) , AuthProvider_{std::move(auth)} { Y_ABORT_UNLESS(!!title); time_t t = time(nullptr); ctime_r(&t, StartTime); } TMonService2::TMonService2(const THttpServerOptions& options, TSimpleSharedPtr pool, const TString& title, THolder auth) : NMonitoring::TMtHttpServer(options, std::bind(&TMonService2::ServeRequest, this, std::placeholders::_1, std::placeholders::_2), std::move(pool)) , Title(title) , IndexMonPage(new TIndexMonPage("", Title)) , AuthProvider_{std::move(auth)} { Y_ABORT_UNLESS(!!title); time_t t = time(nullptr); ctime_r(&t, StartTime); } TMonService2::TMonService2(ui16 port, ui32 threads, const TString& title, THolder auth) : TMonService2(port, TString(), threads, title, std::move(auth)) { } TMonService2::TMonService2(ui16 port, const TString& title, THolder auth) : TMonService2(port, TString(), 0, title, std::move(auth)) { } void TMonService2::OutputIndex(IOutputStream& out) { IndexMonPage->OutputIndex(out, true); } void TMonService2::OutputIndexPage(IOutputStream& out) { out << HTTPOKHTML; out << "\n"; IndexMonPage->OutputHead(out); OutputIndexBody(out); out << "\n"; } void TMonService2::OutputIndexBody(IOutputStream& out) { out << "\n"; // part of common navbar out << "\n"; out << "
\n" << "

" << Title << "

\n"; OutputIndex(out); out << "
\n" << "\n"; } void TMonService2::ServeRequest(IOutputStream& out, const NMonitoring::IHttpRequest& request) { TString path = request.GetPath(); Y_ABORT_UNLESS(path.StartsWith('/')); if (AuthProvider_) { const auto authResult = AuthProvider_->Check(request); switch (authResult.Status) { case TAuthResult::EStatus::NoCredentials: out << HTTPUNAUTHORIZED; return; case TAuthResult::EStatus::Denied: out << HTTPFORBIDDEN; return; case TAuthResult::EStatus::Ok: break; } } if (path == "/") { OutputIndexPage(out); } else { TMonService2HttpRequest monService2HttpRequest( &out, &request, this, IndexMonPage.Get(), path, nullptr); IndexMonPage->Output(monService2HttpRequest); } } void TMonService2::Register(IMonPage* page) { IndexMonPage->Register(page); } void TMonService2::Register(TMonPagePtr page) { IndexMonPage->Register(std::move(page)); } TIndexMonPage* TMonService2::RegisterIndexPage(const TString& path, const TString& title) { return IndexMonPage->RegisterIndexPage(path, title); } IMonPage* TMonService2::FindPage(const TString& relativePath) { return IndexMonPage->FindPage(relativePath); } TIndexMonPage* TMonService2::FindIndexPage(const TString& relativePath) { return IndexMonPage->FindIndexPage(relativePath); } void TMonService2::SortPages() { IndexMonPage->SortPages(); }