29
29
#include " server/Server/ResponseGenerator.h"
30
30
#include " config.h"
31
31
#include " cubes_dump.h"
32
+ #include " server/Resources/Endpoint/Hateoas.h"
32
33
33
34
namespace polycube {
34
35
namespace polycubed {
@@ -39,9 +40,11 @@ std::string RestServer::blacklist_cert_path;
39
40
const std::string RestServer::base = " /polycube/v1/" ;
40
41
41
42
// start http server for Management APIs
42
- // Incapsultate a core object // TODO probably there are best ways...
43
+ // Encapsulate a core object // TODO probably there are best ways...
43
44
RestServer::RestServer (Pistache::Address addr, PolycubedCore &core)
44
45
: core(core),
46
+ host (addr.host()),
47
+ port(addr.port().toString()),
45
48
httpEndpoint_(std::make_unique<Pistache::Http::Endpoint>(addr)),
46
49
logger(spdlog::get(" polycubed" )) {
47
50
logger->info (" rest server listening on '{0}:{1}'" , addr.host (), addr.port ());
@@ -218,6 +221,15 @@ void RestServer::setup_routes() {
218
221
using Pistache::Rest::Routes::bind;
219
222
router_->options (base + std::string (" /" ),
220
223
bind (&RestServer::root_handler, this ));
224
+
225
+ /* binding root_handler in order to handle get at root.
226
+ * It's necessary to provide a way to reach the root to the client.
227
+ * Thanks this the client can explore the service using Hateoas.
228
+ *
229
+ * see server/Resources/Endpoint/Hateoas.h
230
+ */
231
+ router_->get (base + std::string (" /" ),
232
+ bind (&RestServer::get_root_handler, this ));
221
233
// servicectrls
222
234
router_->post (base + std::string (" /services" ),
223
235
bind (&RestServer::post_servicectrl, this ));
@@ -227,6 +239,7 @@ void RestServer::setup_routes() {
227
239
bind (&RestServer::get_servicectrl, this ));
228
240
router_->del (base + std::string (" /services/:name" ),
229
241
bind (&RestServer::delete_servicectrl, this ));
242
+ Hateoas::addRoute (" services" , base);
230
243
231
244
// cubes
232
245
router_->get (base + std::string (" /cubes" ),
@@ -242,6 +255,7 @@ void RestServer::setup_routes() {
242
255
243
256
router_->options (base + std::string (" /cubes/:cubeName" ),
244
257
bind (&RestServer::cube_help, this ));
258
+ Hateoas::addRoute (" cubes" , base);
245
259
246
260
// netdevs
247
261
router_->get (base + std::string (" /netdevs" ),
@@ -254,10 +268,12 @@ void RestServer::setup_routes() {
254
268
255
269
router_->options (base + std::string (" /netdevs/:netdevName" ),
256
270
bind (&RestServer::netdev_help, this ));
271
+ Hateoas::addRoute (" netdevs" , base);
257
272
258
273
// version
259
274
router_->get (base + std::string (" /version" ),
260
275
bind (&RestServer::get_version, this ));
276
+ Hateoas::addRoute (" version" , base);
261
277
262
278
// connect & disconnect
263
279
router_->post (base + std::string (" /connect" ),
@@ -267,10 +283,14 @@ void RestServer::setup_routes() {
267
283
268
284
router_->options (base + std::string (" /connect" ),
269
285
bind (&RestServer::connect_help, this ));
286
+ Hateoas::addRoute (" connect" , base);
287
+ Hateoas::addRoute (" disconnect" , base);
270
288
271
289
// attach & detach
272
290
router_->post (base + std::string (" /attach" ), bind (&RestServer::attach, this ));
273
291
router_->post (base + std::string (" /detach" ), bind (&RestServer::detach, this ));
292
+ Hateoas::addRoute (" attach" , base);
293
+ Hateoas::addRoute (" detach" , base);
274
294
275
295
// topology
276
296
router_->get (base + std::string (" /topology" ),
@@ -280,6 +300,7 @@ void RestServer::setup_routes() {
280
300
281
301
router_->options (base + std::string (" /topology" ),
282
302
bind (&RestServer::topology_help, this ));
303
+ Hateoas::addRoute (" topology" , base);
283
304
284
305
router_->addNotFoundHandler (bind (&RestServer::redirect, this ));
285
306
}
@@ -299,6 +320,14 @@ void RestServer::logJson(json j) {
299
320
#endif
300
321
}
301
322
323
+ void RestServer::get_root_handler (const Pistache::Rest::Request &request,
324
+ Pistache::Http::ResponseWriter response) {
325
+
326
+ auto js = Hateoas::HateoasSupport_root (request, host, port);
327
+ Rest::Server::ResponseGenerator::Generate ({{kOk ,
328
+ ::strdup (js.dump().c_str())}}, std::move (response));
329
+ }
330
+
302
331
void RestServer::root_handler (const Pistache::Rest::Request &request,
303
332
Pistache::Http::ResponseWriter response) {
304
333
auto help = request.query ().get (" help" ).getOrElse (" NO_HELP" );
@@ -894,5 +923,13 @@ void RestServer::redirect(const Pistache::Rest::Request &request,
894
923
response.send (Pistache::Http::Code::Permanent_Redirect);
895
924
}
896
925
926
+ const std::string &RestServer::getHost () {
927
+ return host;
928
+ }
929
+
930
+ const std::string &RestServer::getPort () {
931
+ return port;
932
+ }
933
+
897
934
} // namespace polycubed
898
935
} // namespace polycube
0 commit comments