server.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "servlet.h"
  3. #include <library/cpp/http/server/http_ex.h>
  4. #include <library/cpp/getopt/last_getopt.h>
  5. #include <util/generic/string.h>
  6. #include <util/system/types.h>
  7. namespace NYql {
  8. namespace NHttp {
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // TServerConfig
  11. ///////////////////////////////////////////////////////////////////////////////
  12. struct TBind {
  13. enum {
  14. OnLocal = 0x01,
  15. OnRemote = 0x02,
  16. };
  17. };
  18. class TServerConfig
  19. {
  20. public:
  21. TServerConfig()
  22. : Bind_(0)
  23. , Port_(0)
  24. {
  25. }
  26. inline TServerConfig& Bind(ui32 on) {
  27. Bind_ |= on;
  28. return *this;
  29. }
  30. inline bool IsBind(ui32 on) const {
  31. return Bind_ & on;
  32. }
  33. inline TServerConfig& SetPort(ui16 port) {
  34. Port_ = port;
  35. return *this;
  36. }
  37. inline ui16 GetPort() const {
  38. return Port_;
  39. }
  40. inline TServerConfig& SetAssetsPath(const TString& path) {
  41. AssetsPath_ = path;
  42. return *this;
  43. }
  44. inline const TString& GetAssetsPath() const {
  45. return AssetsPath_;
  46. }
  47. void InitCliOptions(NLastGetopt::TOpts& opts);
  48. void ParseFromCli(NLastGetopt::TOptsParseResult& res);
  49. private:
  50. ui32 Bind_;
  51. ui16 Port_;
  52. TString AssetsPath_;
  53. };
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // TServer
  56. ///////////////////////////////////////////////////////////////////////////////
  57. class TServer:
  58. public THttpServer, public THttpServer::ICallBack,
  59. private TNonCopyable
  60. {
  61. public:
  62. TServer(const TServerConfig& config);
  63. ~TServer();
  64. void RegisterServlet(const TString& path, TAutoPtr<IServlet> sp);
  65. const IServlet* FindServlet(TStringBuf path) const;
  66. TVector<TString> GetUrlMappings() const;
  67. private:
  68. TClientRequest* CreateClient() override final;
  69. private:
  70. TVector<std::pair<TString, IServlet*>> Servlets_;
  71. };
  72. } // namspace NNttp
  73. } // namspace NYql