feat: boxygit add .

This commit is contained in:
hexlocation 2025-07-29 11:47:02 +02:00
parent ba8d1b9453
commit a8bc61f40c
15 changed files with 4700 additions and 0 deletions

81
src/config.rs Normal file
View file

@ -0,0 +1,81 @@
use std::error::Error;
use serde::{Deserialize, Serialize};
use tokio::{fs::File, io::AsyncReadExt};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Proxy {
pub listen: String,
pub port: u16,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Db {
pub host: String,
pub user: String,
pub port: Option<u16>,
pub password: Option<String>,
pub database: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Api {
pub listen: String,
pub port: u16,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Client {
pub name: String,
pub secret: String,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Host {
pub hostname: String,
pub address: String,
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Config {
pub db: Db,
pub proxy: Proxy,
pub api: Api,
pub clients: Vec<Client>,
pub hosts: Vec<Host>,
}
impl Db {
pub async fn to_string(&self) -> String {
let mut builder = String::new();
builder += format!(
"host={} port={} user={} dbname={}",
self.host,
self.port.unwrap_or(5432),
self.user,
self.database.clone().unwrap_or(self.user.clone()),
)
.as_str();
match &self.password {
Some(x) => builder += format!(" password={}", x).as_str(),
None => {}
}
builder
}
}
impl Config {
pub async fn get() -> Result<Self, Box<dyn Error>> {
let mut file = File::open("./config.yaml").await?;
let mut contents = String::new();
file.read_to_string(&mut contents).await?;
let config: Self = serde_yaml_bw::from_str::<Self>(&contents)?;
Ok(config)
}
}