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