Added configuration file and loggign.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#[cfg(feature = "ssr")]
|
||||
use std::fs::read_to_string;
|
||||
#[cfg(feature = "ssr")]
|
||||
use std::net::SocketAddr;
|
||||
#[cfg(feature = "ssr")]
|
||||
use std::str::FromStr;
|
||||
#[cfg(feature = "ssr")]
|
||||
use serde::Deserialize;
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[derive(Deserialize)]
|
||||
pub struct Network {
|
||||
bind_ip: String,
|
||||
port: u16
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl Network {
|
||||
pub fn bind_address(&self) -> SocketAddr {
|
||||
SocketAddr::from_str(format!("{}:{}", &self.bind_ip, self.port).as_str()).expect("Can't parse bind IP address")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[derive(Deserialize)]
|
||||
pub struct Database {
|
||||
user: String,
|
||||
password: String,
|
||||
db_name: String,
|
||||
host: String,
|
||||
port: Option<u16>
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl Database {
|
||||
pub fn con_string(&self) -> String {
|
||||
format!("postgres://{}:{}@{}:{}/{}", self.user, self.password, self.host, self.port.unwrap_or(5432), self.db_name)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
#[derive(Deserialize)]
|
||||
pub struct Configuration {
|
||||
network: Network,
|
||||
database: Database
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
impl Configuration {
|
||||
pub fn network(&self) -> &Network {
|
||||
&self.network
|
||||
}
|
||||
pub fn database(&self) -> &Database {
|
||||
&self.database
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
pub fn load_config(path: &str) -> Configuration {
|
||||
toml::from_str(read_to_string(path).expect("Can not open config file").as_str()).expect("Can not parse config file")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user