//! 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, pub jwt: Arc, /// Şifre değiştirmede mevcut şifre denemeleri (kullanıcı başına) pub login_limiter: Arc, /// Giriş / kayıt: IP başına üst üste 3 hata → 15 dk kilit pub guard: Arc, pub captcha: Arc, } 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 } }