30
30
#include < sstream> // for stringstream
31
31
#include < string> // for string
32
32
33
- #include < oatpp/web/server/HttpConnectionHandler.hpp>
34
33
#include < oatpp/network/tcp/server/ConnectionProvider.hpp>
34
+ #include < oatpp/web/server/HttpConnectionHandler.hpp>
35
35
36
- #include < cloe/core.hpp> // for logger::get
37
- #include < cloe/handler.hpp> // for Request
38
- using namespace cloe ; // NOLINT(build/namespaces)
36
+ #include < cloe/core/logger.hpp> // for logger::get
37
+ #include < cloe/handler.hpp> // for Request
38
+ #include < fable/json.hpp> // for Json
39
+ using namespace cloe ; // NOLINT(build/namespaces)
39
40
40
- #include " oak/route_muxer.hpp" // for Muxer<>
41
41
#include " oak/request_stub.hpp" // for RequestStub
42
+ #include " oak/route_muxer.hpp" // for Muxer<>
42
43
43
44
namespace oak {
44
45
@@ -62,7 +63,7 @@ class Request : public cloe::Request {
62
63
explicit Request (const oatpp::web::protocol::http::incoming::Request& req) {
63
64
auto head = req.getStartingLine ();
64
65
dest_ = head.path .std_str ();
65
- endpoint_ = Muxer<Handler>::normalize (dest_);
66
+ endpoint_ = Muxer<cloe:: Handler>::normalize (dest_);
66
67
for (const auto & [k, v] : req.getQueryParameters ().getAll ()) {
67
68
queries_[k.std_str ()] = v.std_str ();
68
69
}
@@ -94,9 +95,9 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
94
95
GreedyHandler () {
95
96
muxer.set_default ([this ](const cloe::Request& q, cloe::Response& r) {
96
97
logger ()->debug (" 404 {}" , q.endpoint ());
97
- r.not_found (Json{
98
+ r.not_found (fable:: Json{
98
99
{" error" , " cannot find handler" },
99
- {" endpoints" , Json (this ->muxer .routes ())},
100
+ {" endpoints" , fable:: Json (this ->muxer .routes ())},
100
101
});
101
102
});
102
103
}
@@ -109,7 +110,8 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
109
110
* gets passed through. The muxer has a default endpoint, so the nominal case
110
111
* is that every request is passed to some handler from the muxer.
111
112
*/
112
- std::shared_ptr<OutgoingResponse> handle (const std::shared_ptr<IncomingRequest>& request) override {
113
+ std::shared_ptr<OutgoingResponse> handle (
114
+ const std::shared_ptr<IncomingRequest>& request) override {
113
115
auto to_response_impl = [](const cloe::Response& r) -> std::shared_ptr<OutgoingResponse> {
114
116
auto code = Status (static_cast <int >(r.status ()), " " );
115
117
auto type = cloe::as_cstr (r.type ());
@@ -127,11 +129,11 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
127
129
muxer.get (q.endpoint ()).first (q, r);
128
130
return to_response_impl (r);
129
131
} catch (const std::exception & e) {
130
- Response err;
132
+ cloe:: Response err;
131
133
err.error (StatusCode::SERVER_ERROR, std::string (e.what ()));
132
134
return to_response_impl (err);
133
135
} catch (...) {
134
- Response err;
136
+ cloe:: Response err;
135
137
err.error (StatusCode::SERVER_ERROR, std::string (" unknown error occurred" ));
136
138
return to_response_impl (err);
137
139
}
@@ -140,7 +142,7 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
140
142
/* *
141
143
* Add a handler for a specific endpoint.
142
144
*/
143
- void add (const std::string& key, Handler h) { muxer.add (key, h); }
145
+ void add (const std::string& key, cloe:: Handler h) { muxer.add (key, h); }
144
146
145
147
/* *
146
148
* Return a list of all registered endpoints.
@@ -150,11 +152,11 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
150
152
/* *
151
153
* Return endpoint data in json format.
152
154
*/
153
- cloe ::Json endpoints_to_json (const std::vector<std::string>& endpoints) const {
154
- cloe ::Json j;
155
+ fable ::Json endpoints_to_json (const std::vector<std::string>& endpoints) const {
156
+ fable ::Json j;
155
157
for (const auto & endpoint : endpoints) {
156
158
const RequestStub q;
157
- Response r;
159
+ cloe:: Response r;
158
160
try {
159
161
muxer.get (endpoint).first (q, r);
160
162
} catch (std::logic_error& e) {
@@ -163,7 +165,7 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
163
165
continue ;
164
166
}
165
167
if (r.status () == cloe::StatusCode::OK && r.type () == cloe::ContentType::JSON) {
166
- j[endpoint] = cloe ::Json (r.body ());
168
+ j[endpoint] = fable ::Json (r.body ());
167
169
}
168
170
}
169
171
return j;
@@ -173,7 +175,7 @@ class GreedyHandler : public oatpp::web::server::HttpRequestHandler {
173
175
cloe::Logger logger () { return cloe::logger::get (" cloe-server" ); }
174
176
175
177
private:
176
- Muxer<Handler> muxer;
178
+ Muxer<cloe:: Handler> muxer;
177
179
};
178
180
179
181
void Server::listen () {
@@ -191,11 +193,8 @@ void Server::listen() {
191
193
router->route (" DELETE" , " /*" , handler_);
192
194
193
195
auto handler = oatpp::web::server::HttpConnectionHandler::createShared (router);
194
- auto provider = oatpp::network::tcp::server::ConnectionProvider::createShared ({
195
- listen_addr_,
196
- static_cast <v_uint16>(listen_port_),
197
- oatpp::network::Address::IP_4
198
- });
196
+ auto provider = oatpp::network::tcp::server::ConnectionProvider::createShared (
197
+ {listen_addr_, static_cast <v_uint16>(listen_port_), oatpp::network::Address::IP_4});
199
198
200
199
server_ = oatpp::network::Server::createShared (provider, handler);
201
200
server_->run (true );
@@ -213,23 +212,27 @@ void Server::stop() {
213
212
listening_ = false ;
214
213
}
215
214
216
- void Server::add_handler (const std::string& key, Handler h) {
217
- handler_->add (key, std::move (h));
218
- }
215
+ void Server::add_handler (const std::string& key, cloe::Handler h) { handler_->add (key, std::move (h)); }
219
216
220
- std::vector<std::string> Server::endpoints () const {
221
- return handler_->endpoints ();
222
- }
217
+ std::vector<std::string> Server::endpoints () const { return handler_->endpoints (); }
223
218
224
- cloe ::Json Server::endpoints_to_json (const std::vector<std::string>& endpoints) const {
219
+ fable ::Json Server::endpoints_to_json (const std::vector<std::string>& endpoints) const {
225
220
return handler_->endpoints_to_json (endpoints);
226
221
}
227
222
228
223
Server::Server (const std::string& addr, int port)
229
- : listen_addr_(addr), listen_port_(port), listen_threads_(3 ), listening_(false ), handler_(new GreedyHandler()) {}
224
+ : listen_addr_(addr)
225
+ , listen_port_(port)
226
+ , listen_threads_(3 )
227
+ , listening_(false )
228
+ , handler_(new GreedyHandler()) {}
230
229
231
230
Server::Server ()
232
- : listen_addr_(" 127.0.0.1" ), listen_port_(8080 ), listen_threads_(3 ), listening_(false ), handler_(new GreedyHandler()) {}
231
+ : listen_addr_(" 127.0.0.1" )
232
+ , listen_port_(8080 )
233
+ , listen_threads_(3 )
234
+ , listening_(false )
235
+ , handler_(new GreedyHandler()) {}
233
236
234
237
Server::~Server () {
235
238
if (this ->is_listening ()) {
0 commit comments