Files
kakeibo/backend/src/state.rs
T

45 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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
}
}