mabel/src/state.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

2023-06-30 13:02:20 +00:00
use serde::{Deserialize, Serialize};
2023-06-29 12:08:59 +00:00
use sqlx::{Pool, Postgres};
2023-07-01 13:02:40 +00:00
use url::Url;
2023-06-29 12:08:59 +00:00
2023-06-30 13:02:20 +00:00
#[derive(Deserialize, Serialize, Clone)]
pub struct Config {
pub bind: String,
pub database_url: String,
2023-07-02 01:26:42 +00:00
pub session_duration: i64, // in seconds
pub prune_interval: u64, // in seconds
2023-07-01 13:02:40 +00:00
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())
}
2023-07-06 08:43:33 +00:00
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");
}
}
2023-06-30 13:02:20 +00:00
}
impl Default for Config {
fn default() -> Self {
Config {
2023-07-01 13:02:40 +00:00
bind: "127.0.0.1:3000".into(),
database_url: "postgres://artificiale:changeme@localhost/artificiale".into(),
2023-07-01 14:15:19 +00:00
session_duration: 3600, // 60min
2023-07-06 08:43:33 +00:00
prune_interval: 3600, // 60min
2023-07-01 13:02:40 +00:00
base_url: "http://localhost".into(),
2023-06-30 13:02:20 +00:00
}
}
}
2023-06-29 12:08:59 +00:00
pub struct AppState {
pub database: Pool<Postgres>,
2023-06-30 13:02:20 +00:00
pub config: Config,
2023-06-29 12:08:59 +00:00
}