Fawkes API  Fawkes Development Version
backendinfo-rest-api.cpp
1 
2 /***************************************************************************
3  * backendinfo-rest-api.cpp - Backend Info REST API
4  *
5  * Created: Mon Apr 09 15:43:14 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "backendinfo-rest-api.h"
23 
24 #include <webview/rest_api_manager.h>
25 
26 #include <set>
27 
28 using namespace fawkes;
29 
30 /** @class BackendInfoRestApi "skiller-rest-api.h"
31  * REST API backend for the image.
32  * @author Tim Niemueller
33  */
34 
35 /** Constructor. */
37 : Thread("BackendInfoRestApi", Thread::OPMODE_WAITFORWAKEUP)
38 {
39 }
40 
41 /** Destructor. */
43 {
44 }
45 
46 void
48 {
49  std::set<std::string> configs;
50  std::string prefix = "/webview/backends/";
51 
52  {
53  std::unique_ptr<Configuration::ValueIterator> i{config->search(prefix.c_str())};
54  while (i->next()) {
55  std::string cfg_name = std::string(i->path()).substr(prefix.length());
56  cfg_name = cfg_name.substr(0, cfg_name.find("/"));
57  configs.insert(cfg_name);
58  }
59  }
60  for (const auto &c : configs) {
61  Backend b;
62  b.set_kind("Backend");
64  b.set_id(c);
65  b.set_name(config->get_string(prefix + c + "/name"));
66  b.set_url(config->get_string(prefix + c + "/url"));
67 
68  std::string svc_prefix = prefix + c + "/services/";
69  std::unique_ptr<Configuration::ValueIterator> i{config->search(svc_prefix.c_str())};
70  while (i->next()) {
71  std::string svc_name = std::string(i->path()).substr(svc_prefix.length());
72  Service s;
73  s.set_name(svc_name);
74  s.set_url(i->get_string());
75  b.addto_services(std::move(s));
76  }
77  backends_.push_back(std::move(b));
78  }
79 
80  rest_api_ = new WebviewRestApi("backends", logger);
82  WebRequest::METHOD_GET, "/?", std::bind(&BackendInfoRestApi::cb_list_backends, this));
84 }
85 
86 void
88 {
90  delete rest_api_;
91 }
92 
93 void
95 {
96 }
97 
99 BackendInfoRestApi::cb_list_backends()
100 {
101  return backends_;
102 }
~BackendInfoRestApi()
Destructor.
BackendInfoRestApi()
Constructor.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
Backend representation for JSON transfer.
Definition: Backend.h:30
void set_kind(const std::string &kind)
Set kind value.
Definition: Backend.h:103
void set_id(const std::string &id)
Set id value.
Definition: Backend.h:137
static std::string api_version()
Get version of implemented API.
Definition: Backend.h:50
void set_apiVersion(const std::string &apiVersion)
Set apiVersion value.
Definition: Backend.h:120
void set_name(const std::string &name)
Set name value.
Definition: Backend.h:154
void addto_services(const std::shared_ptr< Service > &&services)
Add element to services array.
Definition: Backend.h:196
void set_url(const std::string &url)
Set url value.
Definition: Backend.h:171
Service representation for JSON transfer.
Definition: Service.h:28
void set_name(const std::string &name)
Set name value.
Definition: Service.h:101
void set_url(const std::string &url)
Set url value.
Definition: Service.h:118
void push_back(M &m)
Add item at the back of the container.
Definition: rest_array.h:123
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:41
Thread class encapsulation of pthreads.
Definition: thread.h:46
WebviewRestApiManager * webview_rest_api_manager
Webview REST API manager.
Definition: webview.h:55
void unregister_api(WebviewRestApi *api)
Remove a request processor.
void register_api(WebviewRestApi *api)
Add a REST API.
Webview REST API component.
Definition: rest_api.h:221
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
Definition: rest_api.cpp:85
Fawkes library namespace.