1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
extern crate envconfig;
extern crate influxdb;
use envconfig::Envconfig;
use envconfig::Error;
use influxdb::Client;
#[derive(Envconfig)]
pub struct Config {
#[envconfig(from = "INFLUXDB_HOST")]
pub host: String,
#[envconfig(from = "INFLUXDB_PORT", default = "8086")]
pub port: u16,
#[envconfig(from = "INFLUXDB_USE_TLS", default = "false")]
pub tls: bool,
#[envconfig(from = "INFLUXDB_USERNAME")]
pub username: Option<String>,
#[envconfig(from = "INFLUXDB_PASSWORD", default = "")]
pub password: String,
#[envconfig(from = "INFLUXDB_DATABASE", default = "example")]
pub database: String,
}
impl Config {
pub fn into_client(self) -> Client {
let client = Client::new(self.get_url(), self.database);
match self.username {
None => client,
Some(v) => client.with_auth(v, self.password)
}
}
fn get_url(&self) -> String {
match self.tls {
true => format!("https://{}:{}", self.host, self.port),
false => format!("http://{}:{}", self.host, self.port)
}
}
}
#[inline]
pub fn from_env() -> Result<Config, Error> {
Config::init_from_env()
}
#[inline]
pub fn client_from_env() -> Result<Client, Error> {
Ok(from_env()?.into_client())
}