rebuild file structur + simple sqlite db endpoint
This commit is contained in:
35
src/utils/db_pool.rs
Normal file
35
src/utils/db_pool.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use std::sync::Arc;
|
||||
use dashmap::DashMap;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
|
||||
type HotelId = i32; // or i32 if you want numeric ids
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HotelPools {
|
||||
pools: Arc<DashMap<HotelId, Pool<SqliteConnectionManager>>>,
|
||||
}
|
||||
|
||||
impl HotelPools {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
pools: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_pool(&self, hotel_id: i32) -> Pool<SqliteConnectionManager> {
|
||||
if let Some(pool) = self.pools.get(&hotel_id) {
|
||||
return pool.clone();
|
||||
}
|
||||
|
||||
let db_path = format!("db/{}.sqlite", hotel_id);
|
||||
let manager = SqliteConnectionManager::file(db_path);
|
||||
let pool = Pool::builder()
|
||||
.max_size(5) // adjust based on load
|
||||
.build(manager)
|
||||
.expect("Failed to build pool");
|
||||
|
||||
self.pools.insert(hotel_id, pool.clone());
|
||||
pool
|
||||
}
|
||||
}
|
||||
0
src/utils/dpPool.rs
Normal file
0
src/utils/dpPool.rs
Normal file
10
src/utils/hash.rs
Normal file
10
src/utils/hash.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
use bcryp::{hash, verify, DEFAULT_COST};
|
||||
|
||||
pub fn bcrypt_hash(password: &str) -> Result<String, BcryptError> {
|
||||
hash(password, 5)
|
||||
}
|
||||
|
||||
pub fn bcrypt_verify(password: &str, hashed_password: &str) -> Result<String, BcryptError> {
|
||||
verify(password, hashed_password)
|
||||
}
|
||||
|
||||
1
src/utils/mod.rs
Normal file
1
src/utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod db_pool;
|
||||
44
src/utils/schema_extractor.rs
Normal file
44
src/utils/schema_extractor.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use axum::{
|
||||
async_traits,
|
||||
extract::FromRequestParts,
|
||||
http::request::Parts,
|
||||
};
|
||||
use jsonwebtoken::{decode, DecodingKey, Validation};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Claims {
|
||||
schema: String
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct UserSchema(pub String);
|
||||
|
||||
#[async_traits]
|
||||
impl<S> FromRequestParts<S> for UserSchema
|
||||
where
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = axum::http::StatusCode;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
_state: &S,
|
||||
) -> Reslult<Self, Self::Rejection> {
|
||||
let auth_header = parts
|
||||
.headers
|
||||
.get("authorizaton")
|
||||
.and_then(|h| h.to_str().ok())
|
||||
.ok_or(axum::http::StatusCode::UNAUTHORIZED)?;
|
||||
let token = auth_header.trim_start_matches("Bearer ");
|
||||
|
||||
let data = decode::<Claims>(
|
||||
token,
|
||||
&DecodingKey::from_secret("mysecret".as_ref()), // load from config later
|
||||
&Validation::default(),
|
||||
)
|
||||
.map_err(|_| axum::http::StatusCode::UNAUTHORIZED)?;
|
||||
|
||||
Ok(TenantSchema(data.claims.schema))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user