virthttp  0.0
libvirt http interface
config.hpp
Go to the documentation of this file.
1 //
2 // Created by hugo1 on 12.02.2019.
3 //
4 
5 #pragma once
6 
7 #include <string_view>
8 #include <INIReader.h>
9 #include "logger.hpp"
10 
15 class IniConfig {
16  private:
17  std::string default_http_auth_key = "123456789abcdefgh";
18 
19  public:
24 
25  IniConfig() = default;
26  IniConfig(std::string_view config_file_loc) { init(config_file_loc); }
27 
28  void init(std::string_view config_file_loc) {
29  config_file = config_file_loc;
30  INIReader reader(config_file);
31 
32  logger.setColored(reader.GetBoolean("wrapperd", "color", false));
33  logger.setQuiet(reader.GetBoolean("wrapperd", "quiet", false));
34  logger.setDebug(reader.GetBoolean("wrapperd", "debug", false));
35 
36  if (reader.ParseError() < 0) {
37  logger.warning("Can't load config from ", config_file);
38  logger.info("Using default config");
39  logger.info("Libvirtd : qemu:///system");
40  logger.info("HTTP Wrapper : http://0.0.0.0:8081/");
41  } else
42  logger.info("Config loaded from ", config_file);
43 
44  http_address = reader.Get("http_server", "address", "0.0.0.0");
45  http_port = reader.GetInteger("http_server", "port", 8081);
46  http_doc_root = reader.Get("http_server", "doc_root", ".");
47  http_threads = reader.GetInteger("http_server", "threads", 1);
48  http_auth_key_required = reader.GetBoolean("http_server", "auth-key-required", true);
49  http_auth_key = reader.Get("http_server", "auth-key", default_http_auth_key);
50  if (http_auth_key_required && (http_auth_key == default_http_auth_key))
51  logger.warning("Using default HTTP Auth Key: ", default_http_auth_key);
52 
53  connDRIV = reader.Get("libvirtd", "driver", "qemu");
54  connTRANS = reader.Get("libvirtd", "transport", "");
55  connUNAME = reader.Get("libvirtd", "username", "");
56  connHOST = reader.Get("libvirtd", "hostname", "");
57  connPORT = reader.Get("libvirtd", "port", "");
58  connPATH = reader.Get("libvirtd", "path", "system");
59  connEXTP = reader.Get("libvirtd", "extras", "");
60  buildConnURI();
61  buildHttpURI();
62  }
63 
64  void buildConnURI() {
65  connURI.clear();
66  connURI.reserve(64); // Take some extra space, since we're over SSO anyway
67  connURI.append(connDRIV);
68  if (!connTRANS.empty())
69  connURI += '+' + connTRANS;
70  connURI += "://";
71  if (!connUNAME.empty())
72  connURI += connUNAME + '@';
73  connURI += connHOST;
74  if (!connPORT.empty())
75  connURI += ':' + connPORT;
76  connURI += '/' + connPATH;
77  if (!connEXTP.empty())
78  connURI += '?' + connEXTP;
79  }
80 
81  void buildHttpURI() {
82  httpURI.clear();
83  httpURI.reserve(64);
84  httpURI.append("http://");
85  httpURI += http_address + ":" + std::to_string(http_port);
86  }
87 
88  [[nodiscard]] const std::string& getConnURI() const noexcept { return connURI; }
89 
90  [[nodiscard]] const std::string& getHttpURI() const noexcept { return httpURI; }
91 
92  [[nodiscard]] bool isHTTPAuthRequired() const noexcept { return http_auth_key_required; }
93 };
IniConfig(std::string_view config_file_loc)
Definition: config.hpp:26
long http_threads
Definition: config.hpp:22
std::string httpURI
Definition: config.hpp:20
bool http_auth_key_required
Definition: config.hpp:23
void setColored(bool b)
Definition: logger.hpp:50
const std::string & getConnURI() const noexcept
Definition: config.hpp:88
std::string connPORT
Definition: config.hpp:20
void setDebug(bool b)
Definition: logger.hpp:49
std::string config_file
Definition: config.hpp:20
void setQuiet(bool b)
Definition: logger.hpp:48
std::string connEXTP
Definition: config.hpp:20
void buildHttpURI()
Definition: config.hpp:81
Logger logger
Definition: logger.hpp:58
Definition: config.hpp:15
std::string http_doc_root
Definition: config.hpp:20
std::string connPATH
Definition: config.hpp:20
std::string connHOST
Definition: config.hpp:20
void init(std::string_view config_file_loc)
Definition: config.hpp:28
bool isHTTPAuthRequired() const noexcept
Definition: config.hpp:92
void buildConnURI()
Definition: config.hpp:64
std::string http_address
Definition: config.hpp:20
void info(Ts...msg)
Definition: logger.hpp:34
std::string connURI
Definition: config.hpp:20
std::string connTRANS
Definition: config.hpp:20
const std::string & getHttpURI() const noexcept
Definition: config.hpp:90
void warning(Ts...msg)
Definition: logger.hpp:16
std::string connDRIV
Definition: config.hpp:20
std::string connUNAME
Definition: config.hpp:20
IniConfig()=default
std::string http_auth_key
Definition: config.hpp:20
long http_port
Definition: config.hpp:22