refactor: group middleware
This commit is contained in:
parent
6434ddd5fb
commit
87fc7e29d8
@ -1,5 +1,4 @@
|
||||
pub mod routes;
|
||||
pub mod utils;
|
||||
pub mod redirect;
|
||||
|
||||
pub use routes::init;
|
||||
|
@ -10,7 +10,6 @@ use actix_web::{web, App, HttpServer};
|
||||
use dotenv::dotenv;
|
||||
use sqlx::postgres::PgPool;
|
||||
|
||||
use crate::auth::redirect;
|
||||
use crate::auth::utils::generate_salt_and_hash_plain_password;
|
||||
use crate::models::User;
|
||||
use crate::postgres_session_store::SqlxPostgresqlSessionStore;
|
||||
@ -19,9 +18,9 @@ mod auth;
|
||||
mod calendar;
|
||||
mod endpoints;
|
||||
mod models;
|
||||
mod middleware;
|
||||
|
||||
mod postgres_session_store;
|
||||
mod load_current_user_from_db;
|
||||
|
||||
pub enum Command {
|
||||
Migrate,
|
||||
@ -120,8 +119,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
.configure(auth::init)
|
||||
.configure(calendar::init)
|
||||
.configure(endpoints::init)
|
||||
.wrap(redirect::CheckLogin)
|
||||
.wrap(load_current_user_from_db::LoadUser)
|
||||
.wrap(middleware::RedirectToLogin)
|
||||
.wrap(middleware::LoadCurrentUser)
|
||||
.wrap(
|
||||
IdentityMiddleware::builder()
|
||||
.visit_deadline(Some(Duration::from_secs(300)))
|
||||
|
@ -13,30 +13,30 @@ use sqlx::PgPool;
|
||||
|
||||
use crate::models::User;
|
||||
|
||||
pub struct LoadUser;
|
||||
pub struct LoadCurrentUser;
|
||||
|
||||
impl<S, B> Transform<S, ServiceRequest> for LoadUser
|
||||
impl<S, B> Transform<S, ServiceRequest> for LoadCurrentUser
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
{
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = LoadUserMiddleware<S>;
|
||||
type Transform = LoadCurrentUserMiddleware<S>;
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(LoadUserMiddleware {
|
||||
ready(Ok(LoadCurrentUserMiddleware {
|
||||
service: Rc::new(service),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LoadUserMiddleware<S> {
|
||||
pub struct LoadCurrentUserMiddleware<S> {
|
||||
service: Rc<S>,
|
||||
}
|
||||
|
||||
impl<S, B> Service<ServiceRequest> for LoadUserMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for LoadCurrentUserMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
{
|
5
src/middleware/mod.rs
Normal file
5
src/middleware/mod.rs
Normal file
@ -0,0 +1,5 @@
|
||||
mod redirect_to_login;
|
||||
mod load_current_user_from_db;
|
||||
|
||||
pub use redirect_to_login::RedirectToLogin;
|
||||
pub use load_current_user_from_db::LoadCurrentUser;
|
@ -8,9 +8,9 @@ use actix_web::{
|
||||
};
|
||||
use futures_util::future::LocalBoxFuture;
|
||||
|
||||
pub struct CheckLogin;
|
||||
pub struct RedirectToLogin;
|
||||
|
||||
impl<S, B> Transform<S, ServiceRequest> for CheckLogin
|
||||
impl<S, B> Transform<S, ServiceRequest> for RedirectToLogin
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
||||
@ -19,18 +19,18 @@ where
|
||||
type Response = ServiceResponse<EitherBody<B>>;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = CheckLoginMiddleware<S>;
|
||||
type Transform = RedirectToLoginMiddleware<S>;
|
||||
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
ready(Ok(CheckLoginMiddleware { service }))
|
||||
ready(Ok(RedirectToLoginMiddleware { service }))
|
||||
}
|
||||
}
|
||||
pub struct CheckLoginMiddleware<S> {
|
||||
pub struct RedirectToLoginMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S, B> Service<ServiceRequest> for CheckLoginMiddleware<S>
|
||||
impl<S, B> Service<ServiceRequest> for RedirectToLoginMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
||||
S::Future: 'static,
|
Loading…
x
Reference in New Issue
Block a user