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
59
60
use std::{fs::File, io::Read};
use serde::Deserialize;
#[derive(Deserialize, Debug, Clone)]
pub struct Config {
pub server: ServerConfig,
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
pub database: DatabaseConfig,
}
#[derive(Deserialize, Debug, Clone)]
pub struct ServerConfig {
pub base_url: String,
pub host: String,
pub port: u16,
pub num_threads: usize,
}
#[derive(Deserialize, Debug, Clone)]
pub struct DatabaseConfig {
/// A database connection URL used to connect to the database.
pub url: String,
/// Minimum idle connection count maintained by the pool. Shouldn't be greater than `max_size`.
///
/// Defaults to None.
pub min_idle: Option<u32>,
/// Maximum number of connections managed by the database connection pool.
///
/// Defaults to 10.
pub max_size: Option<u32>,
/// Maximum lifetime of connections in the database connection pool in seconds.
///
/// Defaults to 30 minutes.
pub max_lifetime: Option<u64>,
/// Idle timeout used by the database connection pool in seconds.
///
/// Defaults to 10 minutes.
pub idle_timeout: Option<u64>,
/// Connection timeout used by the pool in seconds.
///
/// Defaults to 30 seconds.
pub connection_timeout: Option<u64>,
}
impl Config {
/// Tries to read and parse the config from the filesystem.
///
/// ```no_run
/// use snx::Config;
///
/// let config = Config::try_from_fs().unwrap();
/// ```
pub fn try_from_fs() -> anyhow::Result<Self> {
let mut contents = String::new();
File::open("./snx.toml")?.read_to_string(&mut contents)?;
Ok(toml::from_str::<Config>(&contents)?)
}
}