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-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-02 01:26:42 +00:00
|
|
|
prune_interval: 10, // 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
|
|
|
}
|