2929#include " server/Server/ResponseGenerator.h"
3030#include " config.h"
3131#include " cubes_dump.h"
32+ #include " server/Resources/Endpoint/Hateoas.h"
3233
3334namespace polycube {
3435namespace polycubed {
@@ -39,9 +40,11 @@ std::string RestServer::blacklist_cert_path;
3940const std::string RestServer::base = " /polycube/v1/" ;
4041
4142// 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...
4344RestServer::RestServer (Pistache::Address addr, PolycubedCore &core)
4445 : core(core),
46+ host (addr.host()),
47+ port(addr.port().toString()),
4548 httpEndpoint_(std::make_unique<Pistache::Http::Endpoint>(addr)),
4649 logger(spdlog::get(" polycubed" )) {
4750 logger->info (" rest server listening on '{0}:{1}'" , addr.host (), addr.port ());
@@ -218,6 +221,15 @@ void RestServer::setup_routes() {
218221 using Pistache::Rest::Routes::bind;
219222 router_->options (base + std::string (" /" ),
220223 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 ));
221233 // servicectrls
222234 router_->post (base + std::string (" /services" ),
223235 bind (&RestServer::post_servicectrl, this ));
@@ -227,6 +239,7 @@ void RestServer::setup_routes() {
227239 bind (&RestServer::get_servicectrl, this ));
228240 router_->del (base + std::string (" /services/:name" ),
229241 bind (&RestServer::delete_servicectrl, this ));
242+ Hateoas::addRoute (" services" , base);
230243
231244 // cubes
232245 router_->get (base + std::string (" /cubes" ),
@@ -242,6 +255,7 @@ void RestServer::setup_routes() {
242255
243256 router_->options (base + std::string (" /cubes/:cubeName" ),
244257 bind (&RestServer::cube_help, this ));
258+ Hateoas::addRoute (" cubes" , base);
245259
246260 // netdevs
247261 router_->get (base + std::string (" /netdevs" ),
@@ -254,10 +268,12 @@ void RestServer::setup_routes() {
254268
255269 router_->options (base + std::string (" /netdevs/:netdevName" ),
256270 bind (&RestServer::netdev_help, this ));
271+ Hateoas::addRoute (" netdevs" , base);
257272
258273 // version
259274 router_->get (base + std::string (" /version" ),
260275 bind (&RestServer::get_version, this ));
276+ Hateoas::addRoute (" version" , base);
261277
262278 // connect & disconnect
263279 router_->post (base + std::string (" /connect" ),
@@ -267,10 +283,14 @@ void RestServer::setup_routes() {
267283
268284 router_->options (base + std::string (" /connect" ),
269285 bind (&RestServer::connect_help, this ));
286+ Hateoas::addRoute (" connect" , base);
287+ Hateoas::addRoute (" disconnect" , base);
270288
271289 // attach & detach
272290 router_->post (base + std::string (" /attach" ), bind (&RestServer::attach, this ));
273291 router_->post (base + std::string (" /detach" ), bind (&RestServer::detach, this ));
292+ Hateoas::addRoute (" attach" , base);
293+ Hateoas::addRoute (" detach" , base);
274294
275295 // topology
276296 router_->get (base + std::string (" /topology" ),
@@ -280,6 +300,7 @@ void RestServer::setup_routes() {
280300
281301 router_->options (base + std::string (" /topology" ),
282302 bind (&RestServer::topology_help, this ));
303+ Hateoas::addRoute (" topology" , base);
283304
284305 router_->addNotFoundHandler (bind (&RestServer::redirect, this ));
285306}
@@ -299,6 +320,14 @@ void RestServer::logJson(json j) {
299320#endif
300321}
301322
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+
302331void RestServer::root_handler (const Pistache::Rest::Request &request,
303332 Pistache::Http::ResponseWriter response) {
304333 auto help = request.query ().get (" help" ).getOrElse (" NO_HELP" );
@@ -894,5 +923,13 @@ void RestServer::redirect(const Pistache::Rest::Request &request,
894923 response.send (Pistache::Http::Code::Permanent_Redirect);
895924}
896925
926+ const std::string &RestServer::getHost () {
927+ return host;
928+ }
929+
930+ const std::string &RestServer::getPort () {
931+ return port;
932+ }
933+
897934} // namespace polycubed
898935} // namespace polycube
0 commit comments