use std::sync::Arc;
use crate::admin::helper::AdminTemplate;
use crate::auth::{guard::LoginGuard, session::AdminAuth};
use crate::middleware::security::RateLimiter;
use crate::utils::env::is_debug;
pub struct AdminConfig {
pub prefix: String,
pub hot_reload: bool,
pub site_title: String,
pub site_url: String,
pub enabled: bool,
pub auth: Option<Arc<dyn AdminAuth>>,
pub templates: AdminTemplate,
pub page_size: u64,
pub reset_password_url: Option<String>,
pub user_resources: std::collections::HashMap<String, Option<String>>,
pub reset_password_email_template: Option<String>,
pub resource_order: Vec<String>,
pub rate_limiter: Option<Arc<RateLimiter>>,
pub login_guard: Option<Arc<LoginGuard>>,
}
impl Clone for AdminConfig {
fn clone(&self) -> Self {
Self {
prefix: self.prefix.clone(),
hot_reload: self.hot_reload,
site_title: self.site_title.clone(),
site_url: self.site_url.clone(),
enabled: self.enabled,
auth: self.auth.clone(),
templates: self.templates.clone(),
page_size: self.page_size,
reset_password_url: self.reset_password_url.clone(),
user_resources: self.user_resources.clone(),
reset_password_email_template: self.reset_password_email_template.clone(),
resource_order: self.resource_order.clone(),
rate_limiter: self.rate_limiter.clone(),
login_guard: self.login_guard.clone(),
}
}
}
impl std::fmt::Debug for AdminConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AdminConfig")
.field("prefix", &self.prefix)
.field("hot_reload", &self.hot_reload)
.field("site_title", &self.site_title)
.field("site_url", &self.site_url)
.field("enabled", &self.enabled)
.field("auth", &self.auth.as_ref().map(|_| "<AdminAuth>"))
.field("templates", &self.templates)
.finish()
}
}
impl AdminConfig {
pub fn new() -> Self {
Self {
prefix: "/admin".to_string(),
hot_reload: is_debug(),
site_title: "Administration".to_string(),
site_url: "/".to_string(),
enabled: true,
auth: None,
templates: AdminTemplate::new(),
page_size: 10,
reset_password_url: None,
user_resources: std::collections::HashMap::new(),
reset_password_email_template: None,
resource_order: Vec::new(),
rate_limiter: None,
login_guard: None,
}
}
pub fn page_size(mut self, size: u64) -> Self {
self.page_size = size.max(1);
self
}
pub fn prefix(mut self, prefix: &str) -> Self {
self.prefix = prefix.to_string();
self
}
pub fn hot_reload(mut self, enabled: bool) -> Self {
self.hot_reload = enabled;
self
}
pub fn site_title(mut self, title: &str) -> Self {
self.site_title = title.to_string();
self
}
pub fn site_url(mut self, url: &str) -> Self {
self.site_url = url.to_string();
self
}
pub fn reset_password_url(mut self, url: &str) -> Self {
self.reset_password_url = Some(url.to_string());
self
}
pub fn auth<A: AdminAuth>(mut self, handler: A) -> Self {
self.auth = Some(Arc::new(handler));
self
}
pub fn disable(mut self) -> Self {
self.enabled = false;
self
}
pub fn user_resource(mut self, resource_key: &str) -> Self {
self.user_resources.insert(resource_key.to_string(), None);
self
}
pub fn reset_password_email_template(mut self, path: &str) -> Self {
self.reset_password_email_template = Some(path.to_string());
self
}
pub fn user_resource_with_template(mut self, resource_key: &str, email_template: &str) -> Self {
self.user_resources
.insert(resource_key.to_string(), Some(email_template.to_string()));
self
}
pub fn with_rate_limiter(mut self, limiter: RateLimiter) -> Self {
self.rate_limiter = Some(Arc::new(limiter));
self
}
pub fn with_login_guard(mut self, guard: LoginGuard) -> Self {
self.login_guard = Some(Arc::new(guard));
self
}
pub fn resource_order<I, S>(mut self, order: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.resource_order = order.into_iter().map(Into::into).collect();
self
}
}
impl Default for AdminConfig {
fn default() -> Self {
Self::new()
}
}