mabel/src/state.rs

57 lines
1.5 KiB
Rust

use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use url::Url;
#[derive(Deserialize, Serialize, Clone)]
pub struct Config {
pub bind: String,
pub database_url: String,
pub session_duration: i64, // in seconds
pub prune_interval: u64, // in seconds
pub base_url: String,
}
impl Config {
pub fn secure(&self) -> bool {
Url::parse(&self.base_url)
.map(|x| x.scheme().to_owned())
.unwrap_or("http".to_owned())
== "https"
}
pub fn domain(&self) -> String {
Url::parse(&self.base_url)
.map(|x| x.domain().unwrap_or("localhost").to_owned())
.unwrap_or("localhost".to_owned())
}
pub fn sanity_check(&self) {
// check if base url is valid
if let Err(e) = Url::parse(&self.base_url) {
tracing::warn!("base URL is not valid: {}, falling back to localhost", e);
}
// check that session duration is sensible
if self.session_duration < 100 {
tracing::warn!("session duration is too low, this might cause some headaches");
}
}
}
impl Default for Config {
fn default() -> Self {
Config {
bind: "127.0.0.1:3000".into(),
database_url: "postgres://artificiale:changeme@localhost/artificiale".into(),
session_duration: 3600, // 60min
prune_interval: 3600, // 60min
base_url: "http://localhost".into(),
}
}
}
pub struct AppState {
pub database: Pool<Postgres>,
pub config: Config,
}