2023-08-26 22:32:38 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-08-27 19:57:22 +02:00
|
|
|
use axum::async_trait;
|
2023-08-26 22:32:38 +02:00
|
|
|
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LeaseService(Arc<dyn LeaseServiceTrait + Send + Sync + 'static>);
|
|
|
|
|
|
|
|
impl std::ops::Deref for LeaseService {
|
|
|
|
type Target = Arc<dyn LeaseServiceTrait + Send + Sync + 'static>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LeaseService {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(Arc::new(DefaultLeaseService::default()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DefaultLeaseService {
|
|
|
|
leases: Arc<Mutex<Vec<String>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
pub trait LeaseServiceTrait {
|
|
|
|
async fn create_lease(&self) -> anyhow::Result<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl LeaseServiceTrait for DefaultLeaseService {
|
|
|
|
async fn create_lease(&self) -> anyhow::Result<String> {
|
|
|
|
let mut leases = self.leases.lock().await;
|
|
|
|
|
|
|
|
let lease = uuid::Uuid::new_v4().to_string();
|
|
|
|
|
|
|
|
leases.push(lease.clone());
|
|
|
|
|
|
|
|
Ok(lease)
|
|
|
|
}
|
|
|
|
}
|