Skip to content

Commit

Permalink
moved middleware to correct dir
Browse files Browse the repository at this point in the history
  • Loading branch information
vxcall committed Sep 5, 2024
1 parent 66ece65 commit bb704c8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 65 deletions.
67 changes: 2 additions & 65 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use actix_web::dev::{forward_ready, ServiceRequest, ServiceResponse, Transform};
use actix_web::Error;
use actix_web::{dev::Service, middleware::Logger, web, App, HttpServer};
use actix_web::{middleware::Logger, web, App, HttpServer};
use anyhow::Result;
use aws_sdk_bedrockruntime as bedrock;
use aws_sdk_dynamodb::Client;
use futures_util::future::LocalBoxFuture;
use redis::aio::MultiplexedConnection;
use routes::middlewares::inactivity_middleware::{InactivityMiddleware, LastActivityTime};
use utils::app_state::AppState;
use utils::global_variables::SHUTDOWN_DURATION;

Expand All @@ -34,67 +32,6 @@ impl RedisClient {
}
}

struct LastActivityTime(Mutex<Instant>);

struct InactivityMiddleware {
last_activity: Arc<LastActivityTime>,
shutdown_duration: Duration,
}

impl<S, B> Transform<S, ServiceRequest> for InactivityMiddleware
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = InactivityMiddlewareService<S>;
type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
std::future::ready(Ok(InactivityMiddlewareService {
service,
last_activity: self.last_activity.clone(),
shutdown_duration: self.shutdown_duration,
}))
}
}

struct InactivityMiddlewareService<S> {
service: S,
last_activity: Arc<LastActivityTime>,
#[allow(dead_code)]
shutdown_duration: Duration,
}

impl<S, B> Service<ServiceRequest> for InactivityMiddlewareService<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

forward_ready!(service);

fn call(&self, req: ServiceRequest) -> Self::Future {
let last_activity = self.last_activity.clone();
let mut last_activity = last_activity.0.lock().unwrap();
*last_activity = Instant::now();
drop(last_activity);

let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
Ok(res)
})
}
}

#[actix_web::main]
async fn main() -> Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
Expand Down
67 changes: 67 additions & 0 deletions src/routes/middlewares/inactivity_middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::sync::{Arc, Mutex};
use std::time::Instant;

use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform};
use actix_web::Error;
use futures_util::future::LocalBoxFuture;
use std::time::Duration;

pub struct LastActivityTime(pub Mutex<Instant>);
pub struct InactivityMiddleware {
pub last_activity: Arc<LastActivityTime>,
pub shutdown_duration: Duration,
}

impl<S, B> Transform<S, ServiceRequest> for InactivityMiddleware
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = InactivityMiddlewareService<S>;
type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;

fn new_transform(&self, service: S) -> Self::Future {
std::future::ready(Ok(InactivityMiddlewareService {
service,
last_activity: self.last_activity.clone(),
shutdown_duration: self.shutdown_duration,
}))
}
}

pub struct InactivityMiddlewareService<S> {
pub service: S,
pub last_activity: Arc<LastActivityTime>,
#[allow(dead_code)]
pub shutdown_duration: Duration,
}

impl<S, B> Service<ServiceRequest> for InactivityMiddlewareService<S>
where
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

forward_ready!(service);

fn call(&self, req: ServiceRequest) -> Self::Future {
let last_activity = self.last_activity.clone();
let mut last_activity = last_activity.0.lock().unwrap();
*last_activity = Instant::now();
drop(last_activity);

let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
Ok(res)
})
}
}
1 change: 1 addition & 0 deletions src/routes/middlewares/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod auth_middleware;
pub mod inactivity_middleware;

0 comments on commit bb704c8

Please sign in to comment.