a2eff1a984
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
//! Tüm route'ların paylaştığı uygulama durumu.
|
||
|
||
use std::{sync::Arc, time::Duration};
|
||
|
||
use mongodb::Database;
|
||
|
||
use crate::{
|
||
auth::{captcha::Captcha, guard::AttemptGuard, jwt::Jwt, limiter::LoginLimiter},
|
||
config::Config,
|
||
};
|
||
|
||
/// `Clone` ucuzdur: `Database` ve `Arc` içte referans sayımı kullanır, veri kopyalanmaz.
|
||
#[derive(Clone)]
|
||
pub struct AppState {
|
||
pub db: Database,
|
||
pub config: Arc<Config>,
|
||
pub jwt: Arc<Jwt>,
|
||
/// Şifre değiştirmede mevcut şifre denemeleri (kullanıcı başına)
|
||
pub login_limiter: Arc<LoginLimiter>,
|
||
/// Giriş / kayıt: IP başına üst üste 3 hata → 15 dk kilit
|
||
pub guard: Arc<AttemptGuard>,
|
||
pub captcha: Arc<Captcha>,
|
||
}
|
||
|
||
impl AppState {
|
||
pub fn new(db: Database, config: Config) -> Self {
|
||
Self {
|
||
db,
|
||
jwt: Arc::new(Jwt::new(&config.jwt_secret)),
|
||
login_limiter: Arc::new(LoginLimiter::new(5, Duration::from_secs(15 * 60))),
|
||
guard: Arc::new(AttemptGuard::default()),
|
||
captcha: Arc::new(Captcha::from_secret(config.turnstile_secret_key.as_deref())),
|
||
config: Arc::new(config),
|
||
}
|
||
}
|
||
}
|
||
|
||
impl AppState {
|
||
/// Testler: bot doğrulamasını ağsız sabit sonuçla değiştir
|
||
pub fn with_captcha(mut self, captcha: Captcha) -> Self {
|
||
self.captcha = Arc::new(captcha);
|
||
self
|
||
}
|
||
}
|