Skip to main content

systemprompt_users/repository/banned_ip/
mod.rs

1//! Persistence for IP bans.
2//!
3//! [`BannedIpRepository`] reads and writes the `banned_ips` table, supporting
4//! temporary and permanent bans with offense metadata. Ban inputs are carried
5//! by [`BanIpParams`] / [`BanIpWithMetadataParams`] with a [`BanDuration`], and
6//! lookups return [`BannedIp`].
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11mod listing;
12mod queries;
13mod types;
14
15use std::sync::Arc;
16
17use crate::error::Result;
18use sqlx::PgPool;
19use systemprompt_database::DbPool;
20
21pub use types::{BanDuration, BanIpParams, BanIpWithMetadataParams, BannedIp};
22
23#[derive(Clone, Debug)]
24pub struct BannedIpRepository {
25    pool: Arc<PgPool>,
26    write_pool: Arc<PgPool>,
27}
28
29impl BannedIpRepository {
30    pub fn new(db: &DbPool) -> Result<Self> {
31        let pool = db.pool_arc()?;
32        let write_pool = db.write_pool_arc()?;
33        Ok(Self { pool, write_pool })
34    }
35
36    pub fn from_pool(pool: Arc<PgPool>) -> Self {
37        let write_pool = Arc::clone(&pool);
38        Self { pool, write_pool }
39    }
40}