2022-02-08 09:25:59 -08:00
|
|
|
use serde_derive::Deserialize;
|
|
|
|
|
use tokio::fs::File;
|
|
|
|
|
use tokio::io::AsyncReadExt;
|
|
|
|
|
use toml;
|
|
|
|
|
|
2022-02-08 17:08:17 -08:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
use crate::errors::Error;
|
|
|
|
|
|
2022-02-09 20:02:20 -08:00
|
|
|
#[derive(Clone, PartialEq, Deserialize, Hash, std::cmp::Eq, Debug, Copy)]
|
|
|
|
|
pub enum Role {
|
|
|
|
|
Primary,
|
|
|
|
|
Replica,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 13:15:53 -08:00
|
|
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Debug)]
|
2022-02-05 10:02:13 -08:00
|
|
|
pub struct Address {
|
|
|
|
|
pub host: String,
|
|
|
|
|
pub port: String,
|
2022-02-09 20:02:20 -08:00
|
|
|
pub role: Role,
|
2022-02-05 10:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
#[derive(Clone, PartialEq, Hash, std::cmp::Eq, Deserialize, Debug)]
|
2022-02-05 10:02:13 -08:00
|
|
|
pub struct User {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub password: String,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 09:25:59 -08:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct General {
|
|
|
|
|
pub host: String,
|
|
|
|
|
pub port: i16,
|
|
|
|
|
pub pool_size: u32,
|
|
|
|
|
pub pool_mode: String,
|
|
|
|
|
pub connect_timeout: u64,
|
|
|
|
|
pub healthcheck_timeout: u64,
|
|
|
|
|
pub ban_time: i64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct Shard {
|
2022-02-09 20:02:20 -08:00
|
|
|
pub servers: Vec<(String, u16, String)>,
|
2022-02-08 09:25:59 -08:00
|
|
|
pub database: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
|
|
|
|
pub struct Config {
|
|
|
|
|
pub general: General,
|
|
|
|
|
pub user: User,
|
|
|
|
|
pub shards: HashMap<String, Shard>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 06:51:31 -08:00
|
|
|
/// Parse the config.
|
2022-02-08 09:25:59 -08:00
|
|
|
pub async fn parse(path: &str) -> Result<Config, Error> {
|
|
|
|
|
let mut contents = String::new();
|
|
|
|
|
let mut file = match File::open(path).await {
|
|
|
|
|
Ok(file) => file,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
println!("> Config error: {:?}", err);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match file.read_to_string(&mut contents).await {
|
|
|
|
|
Ok(_) => (),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
println!("> Config error: {:?}", err);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let config: Config = match toml::from_str(&contents) {
|
|
|
|
|
Ok(config) => config,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
println!("> Config error: {:?}", err);
|
|
|
|
|
return Err(Error::BadConfig);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
|
}
|
2022-02-08 17:08:17 -08:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn test_config() {
|
|
|
|
|
let config = parse("pgcat.toml").await.unwrap();
|
|
|
|
|
assert_eq!(config.general.pool_size, 15);
|
|
|
|
|
assert_eq!(config.shards.len(), 3);
|
|
|
|
|
assert_eq!(config.shards["1"].servers[0].0, "127.0.0.1");
|
2022-02-09 20:02:20 -08:00
|
|
|
assert_eq!(config.shards["0"].servers[0].2, "primary");
|
2022-02-08 17:08:17 -08:00
|
|
|
}
|
|
|
|
|
}
|