Allow override of some config via env variables

This commit is contained in:
Michael 2019-07-02 01:58:23 +01:00
parent 08d306f6e9
commit fbb83704a0
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C
3 changed files with 26 additions and 3 deletions

View File

@ -5,6 +5,8 @@
#include <router_contact.hpp> #include <router_contact.hpp>
#include <util/fs.hpp> #include <util/fs.hpp>
#include <absl/strings/str_cat.h>
#include <cstdlib>
#include <functional> #include <functional>
#include <string> #include <string>
#include <utility> #include <utility>
@ -14,6 +16,22 @@ namespace llarp
{ {
struct ConfigParser; struct ConfigParser;
template < typename Type >
Type
fromEnv(const Type& val, string_view envNameSuffix)
{
std::string envName = absl::StrCat("LOKINET_", envNameSuffix);
auto ptr = std::getenv(envName.c_str());
if(ptr)
{
return ptr;
}
else
{
return val;
}
}
class RouterConfig class RouterConfig
{ {
private: private:
@ -67,16 +85,21 @@ namespace llarp
class NetworkConfig class NetworkConfig
{ {
public:
using NetConfig = std::unordered_multimap< std::string, std::string >;
private: private:
absl::optional< bool > m_enableProfiling; absl::optional< bool > m_enableProfiling;
std::string m_routerProfilesFile = "profiles.dat"; std::string m_routerProfilesFile = "profiles.dat";
std::string m_strictConnect; std::string m_strictConnect;
NetConfig m_netConfig;
public: public:
// clang-format off // clang-format off
const absl::optional< bool >& enableProfiling() const { return m_enableProfiling; } const absl::optional< bool >& enableProfiling() const { return m_enableProfiling; }
const std::string& routerProfilesFile() const { return m_routerProfilesFile; } const std::string& routerProfilesFile() const { return m_routerProfilesFile; }
const std::string& strictConnect() const { return m_strictConnect; } const std::string& strictConnect() const { return m_strictConnect; }
const NetConfig& netConfig() const { return m_netConfig; }
// clang-format on // clang-format on
void void

View File

@ -808,7 +808,7 @@ namespace llarp
} }
// set network config // set network config
netConfig = conf->network.netConfig; netConfig = conf->network.netConfig();
// Network config // Network config
if(conf->network.enableProfiling().has_value()) if(conf->network.enableProfiling().has_value())

View File

@ -92,9 +92,9 @@ metric-tank-host=52.80.56.123:2003
ASSERT_TRUE(config.LoadFromString(text)); ASSERT_TRUE(config.LoadFromString(text));
{ {
using kv = decltype(config.network.netConfig)::value_type; using kv = NetworkConfig::NetConfig::value_type;
ASSERT_THAT(config.network.netConfig, ASSERT_THAT(config.network.netConfig(),
UnorderedElementsAre(kv("ifname", "cluster-1"), UnorderedElementsAre(kv("ifname", "cluster-1"),
kv("ifaddr", "10.101.0.1/16"))); kv("ifaddr", "10.101.0.1/16")));
} }