mirror of https://github.com/fafhrd91/actix-web
dedupe boxed http service aliases
This commit is contained in:
parent
21e3e1d620
commit
19096b9b36
|
@ -30,9 +30,9 @@ mod tests {
|
||||||
use actix_utils::future::poll_fn;
|
use actix_utils::future::poll_fn;
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
|
|
||||||
use super::{to_bytes, AnyBody as TestAnyBody, BodySize, MessageBody as _};
|
use super::{AnyBody as TestAnyBody, *};
|
||||||
|
|
||||||
impl AnyBody {
|
impl TestAnyBody {
|
||||||
pub(crate) fn get_ref(&self) -> &[u8] {
|
pub(crate) fn get_ref(&self) -> &[u8] {
|
||||||
match *self {
|
match *self {
|
||||||
AnyBody::Bytes(ref bin) => bin,
|
AnyBody::Bytes(ref bin) => bin,
|
||||||
|
|
|
@ -2,10 +2,7 @@ use std::{cell::RefCell, mem, rc::Rc};
|
||||||
|
|
||||||
use actix_http::{Extensions, Request};
|
use actix_http::{Extensions, Request};
|
||||||
use actix_router::{Path, ResourceDef, Router, Url};
|
use actix_router::{Path, ResourceDef, Router, Url};
|
||||||
use actix_service::{
|
use actix_service::{boxed, fn_service, Service, ServiceFactory};
|
||||||
boxed::{self, BoxService, BoxServiceFactory},
|
|
||||||
fn_service, Service, ServiceFactory,
|
|
||||||
};
|
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
|
||||||
|
@ -15,13 +12,13 @@ use crate::{
|
||||||
guard::Guard,
|
guard::Guard,
|
||||||
request::{HttpRequest, HttpRequestPool},
|
request::{HttpRequest, HttpRequestPool},
|
||||||
rmap::ResourceMap,
|
rmap::ResourceMap,
|
||||||
service::{AppServiceFactory, ServiceRequest, ServiceResponse},
|
service::{
|
||||||
|
AppServiceFactory, HttpService, HttpServiceFactory, ServiceRequest, ServiceResponse,
|
||||||
|
},
|
||||||
Error, HttpResponse,
|
Error, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
||||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
|
||||||
|
|
||||||
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
||||||
/// It also executes data factories.
|
/// It also executes data factories.
|
||||||
|
@ -39,7 +36,7 @@ where
|
||||||
pub(crate) extensions: RefCell<Option<Extensions>>,
|
pub(crate) extensions: RefCell<Option<Extensions>>,
|
||||||
pub(crate) async_data_factories: Rc<[FnDataFactory]>,
|
pub(crate) async_data_factories: Rc<[FnDataFactory]>,
|
||||||
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
|
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
|
||||||
pub(crate) default: Option<Rc<HttpNewService>>,
|
pub(crate) default: Option<Rc<HttpServiceFactory>>,
|
||||||
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
||||||
pub(crate) external: RefCell<Vec<ResourceDef>>,
|
pub(crate) external: RefCell<Vec<ResourceDef>>,
|
||||||
}
|
}
|
||||||
|
@ -230,8 +227,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppRoutingFactory {
|
pub struct AppRoutingFactory {
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
services: Rc<[(ResourceDef, HttpServiceFactory, RefCell<Option<Guards>>)]>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpServiceFactory>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
impl ServiceFactory<ServiceRequest> for AppRoutingFactory {
|
||||||
|
|
|
@ -5,9 +5,8 @@ use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::Extensions;
|
use actix_http::Extensions;
|
||||||
use actix_router::{IntoPatterns, Patterns};
|
use actix_router::{IntoPatterns, Patterns};
|
||||||
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply, apply_fn_factory, fn_service, IntoServiceFactory, Service, ServiceFactory,
|
apply, apply_fn_factory, boxed, fn_service, IntoServiceFactory, Service, ServiceFactory,
|
||||||
ServiceFactoryExt, Transform,
|
ServiceFactoryExt, Transform,
|
||||||
};
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
|
@ -20,13 +19,10 @@ use crate::{
|
||||||
handler::Handler,
|
handler::Handler,
|
||||||
responder::Responder,
|
responder::Responder,
|
||||||
route::{Route, RouteService},
|
route::{Route, RouteService},
|
||||||
service::{ServiceRequest, ServiceResponse},
|
service::{HttpService, HttpServiceFactory, ServiceRequest, ServiceResponse},
|
||||||
Error, FromRequest, HttpResponse,
|
Error, FromRequest, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
||||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
|
||||||
|
|
||||||
/// *Resource* is an entry in resources table which corresponds to requested URL.
|
/// *Resource* is an entry in resources table which corresponds to requested URL.
|
||||||
///
|
///
|
||||||
/// Resource in turn has at least one route.
|
/// Resource in turn has at least one route.
|
||||||
|
@ -56,7 +52,7 @@ pub struct Resource<T = ResourceEndpoint> {
|
||||||
routes: Vec<Route>,
|
routes: Vec<Route>,
|
||||||
app_data: Option<Extensions>,
|
app_data: Option<Extensions>,
|
||||||
guards: Vec<Box<dyn Guard>>,
|
guards: Vec<Box<dyn Guard>>,
|
||||||
default: HttpNewService,
|
default: HttpServiceFactory,
|
||||||
factory_ref: Rc<RefCell<Option<ResourceFactory>>>,
|
factory_ref: Rc<RefCell<Option<ResourceFactory>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,7 +418,7 @@ where
|
||||||
|
|
||||||
pub struct ResourceFactory {
|
pub struct ResourceFactory {
|
||||||
routes: Vec<Route>,
|
routes: Vec<Route>,
|
||||||
default: HttpNewService,
|
default: HttpServiceFactory,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
impl ServiceFactory<ServiceRequest> for ResourceFactory {
|
||||||
|
|
18
src/scope.rs
18
src/scope.rs
|
@ -3,9 +3,8 @@ use std::{cell::RefCell, fmt, future::Future, mem, rc::Rc};
|
||||||
use actix_http::Extensions;
|
use actix_http::Extensions;
|
||||||
use actix_router::{ResourceDef, Router};
|
use actix_router::{ResourceDef, Router};
|
||||||
use actix_service::{
|
use actix_service::{
|
||||||
apply, apply_fn_factory,
|
apply, apply_fn_factory, boxed, IntoServiceFactory, Service, ServiceFactory,
|
||||||
boxed::{self, BoxService, BoxServiceFactory},
|
ServiceFactoryExt, Transform,
|
||||||
IntoServiceFactory, Service, ServiceFactory, ServiceFactoryExt, Transform,
|
|
||||||
};
|
};
|
||||||
use futures_core::future::LocalBoxFuture;
|
use futures_core::future::LocalBoxFuture;
|
||||||
use futures_util::future::join_all;
|
use futures_util::future::join_all;
|
||||||
|
@ -16,13 +15,14 @@ use crate::{
|
||||||
dev::{AppService, HttpServiceFactory},
|
dev::{AppService, HttpServiceFactory},
|
||||||
guard::Guard,
|
guard::Guard,
|
||||||
rmap::ResourceMap,
|
rmap::ResourceMap,
|
||||||
service::{AppServiceFactory, ServiceFactoryWrapper, ServiceRequest, ServiceResponse},
|
service::{
|
||||||
|
AppServiceFactory, HttpService, HttpServiceFactory, ServiceFactoryWrapper,
|
||||||
|
ServiceRequest, ServiceResponse,
|
||||||
|
},
|
||||||
Error, Resource, Route,
|
Error, Resource, Route,
|
||||||
};
|
};
|
||||||
|
|
||||||
type Guards = Vec<Box<dyn Guard>>;
|
type Guards = Vec<Box<dyn Guard>>;
|
||||||
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
||||||
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
|
||||||
|
|
||||||
/// Resources scope.
|
/// Resources scope.
|
||||||
///
|
///
|
||||||
|
@ -58,7 +58,7 @@ pub struct Scope<T = ScopeEndpoint> {
|
||||||
app_data: Option<Extensions>,
|
app_data: Option<Extensions>,
|
||||||
services: Vec<Box<dyn AppServiceFactory>>,
|
services: Vec<Box<dyn AppServiceFactory>>,
|
||||||
guards: Vec<Box<dyn Guard>>,
|
guards: Vec<Box<dyn Guard>>,
|
||||||
default: Option<Rc<HttpNewService>>,
|
default: Option<Rc<HttpServiceFactory>>,
|
||||||
external: Vec<ResourceDef>,
|
external: Vec<ResourceDef>,
|
||||||
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
factory_ref: Rc<RefCell<Option<ScopeFactory>>>,
|
||||||
}
|
}
|
||||||
|
@ -470,8 +470,8 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ScopeFactory {
|
pub struct ScopeFactory {
|
||||||
services: Rc<[(ResourceDef, HttpNewService, RefCell<Option<Guards>>)]>,
|
services: Rc<[(ResourceDef, HttpServiceFactory, RefCell<Option<Guards>>)]>,
|
||||||
default: Rc<HttpNewService>,
|
default: Rc<HttpServiceFactory>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
impl ServiceFactory<ServiceRequest> for ScopeFactory {
|
||||||
|
|
|
@ -3,12 +3,15 @@ use std::rc::Rc;
|
||||||
use std::{fmt, net};
|
use std::{fmt, net};
|
||||||
|
|
||||||
use actix_http::{
|
use actix_http::{
|
||||||
body::{AnyBody, MessageBody},
|
body::{AnyBody, BoxBody, MessageBody},
|
||||||
http::{HeaderMap, Method, StatusCode, Uri, Version},
|
http::{HeaderMap, Method, StatusCode, Uri, Version},
|
||||||
Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead,
|
Extensions, HttpMessage, Payload, PayloadStream, RequestHead, Response, ResponseHead,
|
||||||
};
|
};
|
||||||
use actix_router::{IntoPatterns, Path, Patterns, Resource, ResourceDef, Url};
|
use actix_router::{IntoPatterns, Path, Patterns, Resource, ResourceDef, Url};
|
||||||
use actix_service::{IntoServiceFactory, ServiceFactory};
|
use actix_service::{
|
||||||
|
boxed::{BoxService, BoxServiceFactory},
|
||||||
|
IntoServiceFactory, ServiceFactory,
|
||||||
|
};
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
use cookie::{Cookie, ParseError as CookieParseError};
|
use cookie::{Cookie, ParseError as CookieParseError};
|
||||||
|
|
||||||
|
@ -21,6 +24,10 @@ use crate::{
|
||||||
Error, HttpRequest, HttpResponse,
|
Error, HttpRequest, HttpResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub(crate) type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
||||||
|
pub(crate) type HttpServiceFactory =
|
||||||
|
BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
||||||
|
|
||||||
pub trait HttpServiceFactory {
|
pub trait HttpServiceFactory {
|
||||||
fn register(self, config: &mut AppService);
|
fn register(self, config: &mut AppService);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue