mod build;
use axum::Router;
use tower_sessions::cookie::time::Duration;
use super::staging::{AdminStaging, CoreStaging, MiddlewareStaging, StaticStaging};
use crate::auth::{
PasswordResetAdapter, PasswordResetConfig, PasswordResetStaging, session::UserEntity,
};
use crate::config::RuniqueConfig;
use crate::utils::runique_log::RuniqueLog;
#[cfg(feature = "orm")]
use crate::db::DatabaseConfig;
#[cfg(feature = "orm")]
use sea_orm::DatabaseConnection;
#[doc = include_str!("../../../doc-tests/builder/builder_basic.md")]
pub struct RuniqueAppBuilder {
pub(super) config: RuniqueConfig,
pub(super) core: CoreStaging,
pub(super) middleware: MiddlewareStaging,
pub(super) statics: StaticStaging,
pub(super) router: Option<Router>,
pub(super) admin: AdminStaging,
pub(super) password_reset: Option<PasswordResetStaging>,
}
impl RuniqueAppBuilder {
pub fn new(config: RuniqueConfig) -> Self {
let middleware = MiddlewareStaging::from_config(&config);
Self {
config,
core: CoreStaging::new(),
middleware,
statics: StaticStaging::new(),
router: None,
admin: AdminStaging::new(),
password_reset: None,
}
}
pub fn core(mut self, f: impl FnOnce(CoreStaging) -> CoreStaging) -> Self {
self.core = f(self.core);
self
}
#[cfg(feature = "orm")]
pub fn with_database(mut self, db: DatabaseConnection) -> Self {
self.core = self.core.with_database(db);
self
}
#[cfg(feature = "orm")]
pub fn with_database_config(mut self, config: DatabaseConfig) -> Self {
self.core = self.core.with_database_config(config);
self
}
pub fn with_log(mut self, f: impl FnOnce(RuniqueLog) -> RuniqueLog) -> Self {
self.config.log = f(RuniqueLog::new());
self
}
pub fn routes(mut self, router: Router) -> Self {
self.router = Some(router);
self
}
pub fn middleware(mut self, f: impl FnOnce(MiddlewareStaging) -> MiddlewareStaging) -> Self {
self.middleware = f(self.middleware);
self
}
pub fn with_session_duration(mut self, duration: Duration) -> Self {
self.middleware = self.middleware.with_session_duration(duration);
self
}
pub fn with_error_handler(mut self, enable: bool) -> Self {
self.middleware = self.middleware.with_debug_errors(enable);
self
}
pub fn static_files(mut self, f: impl FnOnce(StaticStaging) -> StaticStaging) -> Self {
self.statics = f(self.statics);
self
}
pub fn with_mailer(self, config: crate::utils::mailer::MailerConfig) -> Self {
crate::utils::mailer::mailer_init(config);
self
}
pub fn with_mailer_from_env(self) -> Self {
crate::utils::mailer::mailer_init_from_env();
self
}
pub fn statics(mut self) -> Self {
self.statics = self.statics.enable();
self
}
pub fn no_statics(mut self) -> Self {
self.statics = self.statics.disable();
self
}
pub fn with_admin(mut self, f: impl FnOnce(AdminStaging) -> AdminStaging) -> Self {
self.admin = f(self.admin.enable());
self
}
pub fn with_password_reset<E: UserEntity + 'static>(
mut self,
f: impl FnOnce(PasswordResetConfig) -> PasswordResetConfig,
) -> Self {
let config = f(PasswordResetConfig::default());
self.password_reset = Some(PasswordResetStaging {
handler: Box::new(PasswordResetAdapter::<E>::new()),
config,
});
self
}
}