EpcTools
An event based multi-threaded C++ development framework.
emgmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2019 Sprint
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #ifndef __EMGMT_H
18 #define __EMGMT_H
19 
22 
23 #include <iostream>
24 #include <pistache/endpoint.h>
25 #include <pistache/http_header.h>
26 #include <pistache/router.h>
27 
28 #include "elogger.h"
29 #include "estring.h"
30 #include "etime.h"
31 
33 class EManagementUserNameHeader : public Pistache::Http::Header::Header
34 {
35 public:
36  NAME("X-User-Name")
37 
38 
40  {
41  }
42 
45  void parse(const std::string &data)
46  {
47  m_username = data;
48  }
49 
52  void write(std::ostream &os) const
53  {
54  os << m_username;
55  }
56 
58  EString &getUserName() { return m_username; }
59 
60 private:
61  EString m_username;
62 };
63 
66 {
67  friend class EManagementEndpoint;
68 
69 public:
71  enum class HttpMethod
72  {
74  httpGet,
76  httpPost,
78  httpPut,
80  httpDelete,
82  httpPatch,
83  httpOptions
85  };
86 
91  EManagementHandler(HttpMethod mthd, cpStr pth, ELogger &audit);
96  EManagementHandler(HttpMethod mthd, const std::string &pth, ELogger &audit);
97 
99  virtual ~EManagementHandler() = default;
100 
104  virtual Void process(const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter &response) = 0;
105 
109  Void handler(const Pistache::Rest::Request& request, Pistache::Http::ResponseWriter response);
110 
112  const EString &path() { return m_path; }
114  HttpMethod httpMethod() { return m_method; }
115 
116 protected:
117  Pistache::Rest::Route::Handler getHandler();
118 
119 private:
121 
122  EString m_path;
123  HttpMethod m_method;
124  ELogger &m_audit;
125 };
126 
129 {
130 public:
134  EManagementEndpoint(uint16_t port, size_t thrds=1);
138  EManagementEndpoint(Pistache::Address &addr, size_t thrds=1);
139 
141  Void start();
143  Void shutdown();
144 
146  Void registerHandler(EManagementHandler &hndlr);
147 
148 private:
149  Void init(size_t thrds);
150 
151  std::shared_ptr<Pistache::Http::Endpoint> m_endpoint;
152  Pistache::Rest::Router m_router;
153 
154  static Bool m_username_header_registered;
155 };
156 
157 #endif // #ifndef __EMGMT_H
Encapsulates and extends a std::string object.
HttpMethod httpMethod()
Returns the HTTP method for this HTTP handler.
Definition: emgmt.h:114
void parse(const std::string &data)
Parses the user name from the buffer.
Definition: emgmt.h:45
Custom HTTP header class for the X-User-Name header.
Definition: emgmt.h:33
HttpMethod
Represents the type of the handler.
Definition: emgmt.h:71
Provides class for manipulating time of day values.
Defines the logging related classes.
EString & getUserName()
Returns the value of the user name.
Definition: emgmt.h:58
Defines a logger.
Definition: elogger.h:76
const EString & path()
Returns the route path for this HTTP handler.
Definition: emgmt.h:112
Implemts the HTTP server endpoint.
Definition: emgmt.h:128
Pure virtual base class for an administrative management interface handler.
Definition: emgmt.h:65
String class.
Definition: estring.h:31
void write(std::ostream &os) const
Serializes the user name to the output stream.
Definition: emgmt.h:52