identt
InfoService.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_QUERY_INFO_SERVICE_HPP_
34 #define _IDENTT_QUERY_INFO_SERVICE_HPP_
35 
36 #include <query/QueryBase.hpp>
37 
38 namespace identt {
39 namespace query {
40 
41 template <class HttpServerT>
42 class InfoService : protected identt::query::ServiceBase<HttpServerT> {
43 public:
44 
65  identt::utils::SharedTable::pointer stptr,
66  typename std::shared_ptr<HttpServerT> server,
67  ::identt::query::HelpQuery::pointer helpquery,
68  const unsigned int scope)
69  : identt::query::ServiceBase<HttpServerT>(IDENTT_SERVICE_SCOPE_HTTP | IDENTT_SERVICE_SCOPE_HTTPS)
70  {
71  if (!(this->myscope & scope)) return; // scope mismatch
72 
73  // Endpoint : GET help
74  helpquery->add({scope,"GET help", { "This query, Gets info about endpoints this server" } });
75 
76  server->resource["/help$"]["GET"]
77  =[this,stptr,scope,helpquery](typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request) {
78  IDENTT_PARALLEL_ONE([this,stptr,scope,helpquery,response,request] {
79  try {
80  DLOG(INFO) << request->path;
81  std::string output;
82  std::stringstream content_stream;
83  for (auto& var: helpquery->get(scope))
84  {
85  content_stream << "\r\n\r\n" << var.route ;
86  size_t counter=0;
87  for (auto& desc: var.desc) content_stream << "\r\n -- " << desc;
88  }
89  output = content_stream.str();
90  *response << "HTTP/1.1 200 OK\r\n";
91  *response << "Content-type: text/plain\r\n";
92  *response << "Content-Length: " << output.length() << "\r\n\r\n" << output.c_str();
93  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
94  } catch (...)
95  {
96  this->HttpErrorAction(response,request,500,"INTERNAL SERVER ERROR");
97  }
98  });
99  };
100 
101 
102  // Endpoint : GET info
103  helpquery->add({scope,"GET info", { "Gets info about this server" } });
104 
105  server->resource["/info$"]["GET"]
106  =[this,stptr](typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request) {
107  IDENTT_PARALLEL_ONE([this,stptr,response,request] {
108  try {
109  LOG(INFO) << request->path;
110  ::identt::query::StateT sstate;
111  sstate.set_ts( IDENTT_CURRTIME_MS );
112  sstate.set_is_ready( stptr->is_ready.Get() );
113  // get other params if ready
114  if (sstate.is_ready())
115  {
116  sstate.set_hostname( stptr->hostname.Get() );
117  sstate.set_is_master( stptr->is_master.Get() );
118  if(!sstate.is_master())
119  sstate.set_master( stptr->master.Get() );
120  }
121 
122  // aftermath
123  std::string output;
124  pb2json(&sstate , output);
125  this->HttpOKAction(response,request,200,"OK","application/json",output,true);
126  } catch (...)
127  {
128  this->HttpErrorAction(response,request,500,"INTERNAL SERVER ERROR");
129  }
130  });
131  };
132 
133  }
134 
135 private:
136 
137 };
138 } // namespace query
139 } // namespace identt
140 
141 #endif // _IDENTT_QUERY_INFO_SERVICE_HPP_
void HttpOKAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em, const char *content_type, std::string &payload, bool add_cors)
HttpOKAction : OK Action template.
Definition: ServiceBase.hpp:154
InfoService(identt::utils::SharedTable::pointer stptr, typename std::shared_ptr< HttpServerT > server, ::identt::query::HelpQuery::pointer helpquery, const unsigned int scope)
InfoService : constructor.
Definition: InfoService.hpp:64
void HttpErrorAction(typename HttpServerT::RespPtr response, typename HttpServerT::ReqPtr request, int ec, const char *em)
HttpErrorAction : Error Action Template no payload maybe template.
Definition: ServiceBase.hpp:85
Definition: InfoService.hpp:42
void pb2json(const google::protobuf::Message *msg, std::string &str)
pb2json : Convert protobuf to json
Definition: ProtoJson.cc:415
Definition: ServiceBase.hpp:55
Definition: CryptoBase.hpp:49