upgrading to actix-v4-beta

This commit is contained in:
realaravinth
2021-06-30 20:13:12 +05:30
parent 9ed458ebfa
commit 9f940c317a
24 changed files with 728 additions and 1192 deletions

View File

@@ -16,8 +16,9 @@
*/
#![allow(clippy::type_complexity)]
use std::task::{Context, Poll};
//use std::task::{Context, Poll};
use actix_http::body::AnyBody;
use actix_identity::Identity;
use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse};
@@ -29,13 +30,12 @@ use crate::PAGES;
pub struct CheckLogin;
impl<S, B> Transform<S> for CheckLogin
impl<S> Transform<S, ServiceRequest> for CheckLogin
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S: Service<ServiceRequest, Response = ServiceResponse<AnyBody>, Error = Error>,
S::Future: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Response = ServiceResponse<AnyBody>;
type Error = Error;
type Transform = CheckLoginMiddleware<S>;
type InitError = ();
@@ -49,21 +49,41 @@ pub struct CheckLoginMiddleware<S> {
service: S,
}
impl<S, B> Service for CheckLoginMiddleware<S>
impl<S> Service<ServiceRequest> for CheckLoginMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S: Service<ServiceRequest, Response = ServiceResponse<AnyBody>, Error = Error>,
S::Future: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Response = ServiceResponse<AnyBody>;
type Error = Error;
type Future = Either<S::Future, Ready<Result<Self::Response, Self::Error>>>;
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
// fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
// self.service.poll_ready(cx)
// }
//
actix_service::forward_ready!(service);
fn call(&self, req: ServiceRequest) -> Self::Future {
// let (r, mut pl) = req.into_parts();
// // TODO investigate when the bellow statement will
// // return error
// if let Ok(Some(_)) = Identity::from_request(&r, &mut pl)
// .into_inner()
// .map(|x| x.identity())
// {
// let req = ServiceRequest::from_parts(r, pl);
// Either::Left(self.service.call(req))
// } else {
// let resp = actix_http::ResponseBuilder::new(http::StatusCode::FOUND)
// .insert_header((http::header::LOCATION, PAGES.auth.login))
// .finish();
// let req = ServiceRequest::from_parts(r, pl);
// Either::Right(ok(req.into_response(resp)))
// }
fn call(&mut self, req: ServiceRequest) -> Self::Future {
let (r, mut pl) = req.into_parts();
// TODO investigate when the bellow statement will
@@ -72,15 +92,14 @@ where
.into_inner()
.map(|x| x.identity())
{
let req = ServiceRequest::from_parts(r, pl).ok().unwrap();
let req = ServiceRequest::from_parts(r, pl);
Either::Left(self.service.call(req))
} else {
let req = ServiceRequest::from_parts(r, pl).ok().unwrap();
let req = ServiceRequest::from_parts(r, pl); //.ok().unwrap();
Either::Right(ok(req.into_response(
HttpResponse::Found()
.header(http::header::LOCATION, PAGES.auth.login)
.finish()
.into_body(),
.insert_header((http::header::LOCATION, PAGES.auth.login))
.finish(),
)))
}
}