1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

rename framed App

This commit is contained in:
Nikolay Kim 2019-04-10 18:08:28 -07:00
parent 8dc4a88aa6
commit 7cd59c38d3
4 changed files with 56 additions and 87 deletions

View File

@ -23,23 +23,23 @@ pub trait HttpServiceFactory {
} }
/// Application builder /// Application builder
pub struct App<T, S = ()> { pub struct FramedApp<T, S = ()> {
state: State<S>, state: State<S>,
services: Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>, services: Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>,
} }
impl<T: 'static> App<T, ()> { impl<T: 'static> FramedApp<T, ()> {
pub fn new() -> Self { pub fn new() -> Self {
App { FramedApp {
state: State::new(()), state: State::new(()),
services: Vec::new(), services: Vec::new(),
} }
} }
} }
impl<T: 'static, S: 'static> App<T, S> { impl<T: 'static, S: 'static> FramedApp<T, S> {
pub fn with(state: S) -> App<T, S> { pub fn with(state: S) -> FramedApp<T, S> {
App { FramedApp {
services: Vec::new(), services: Vec::new(),
state: State::new(state), state: State::new(state),
} }
@ -69,13 +69,13 @@ impl<T: 'static, S: 'static> App<T, S> {
} }
} }
impl<T, S> IntoNewService<AppFactory<T, S>> for App<T, S> impl<T, S> IntoNewService<FramedAppFactory<T, S>> for FramedApp<T, S>
where where
T: AsyncRead + AsyncWrite + 'static, T: AsyncRead + AsyncWrite + 'static,
S: 'static, S: 'static,
{ {
fn into_new_service(self) -> AppFactory<T, S> { fn into_new_service(self) -> FramedAppFactory<T, S> {
AppFactory { FramedAppFactory {
state: self.state, state: self.state,
services: Rc::new(self.services), services: Rc::new(self.services),
} }
@ -83,12 +83,12 @@ where
} }
#[derive(Clone)] #[derive(Clone)]
pub struct AppFactory<T, S> { pub struct FramedAppFactory<T, S> {
state: State<S>, state: State<S>,
services: Rc<Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>>, services: Rc<Vec<(String, BoxedHttpNewService<FramedRequest<T, S>>)>>,
} }
impl<T, S> NewService for AppFactory<T, S> impl<T, S> NewService for FramedAppFactory<T, S>
where where
T: AsyncRead + AsyncWrite + 'static, T: AsyncRead + AsyncWrite + 'static,
S: 'static, S: 'static,
@ -97,7 +97,7 @@ where
type Response = (); type Response = ();
type Error = Error; type Error = Error;
type InitError = (); type InitError = ();
type Service = CloneableService<AppService<T, S>>; type Service = CloneableService<FramedAppService<T, S>>;
type Future = CreateService<T, S>; type Future = CreateService<T, S>;
fn new_service(&self, _: &()) -> Self::Future { fn new_service(&self, _: &()) -> Self::Future {
@ -135,7 +135,7 @@ impl<S: 'static, T: 'static> Future for CreateService<T, S>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
{ {
type Item = CloneableService<AppService<T, S>>; type Item = CloneableService<FramedAppService<T, S>>;
type Error = (); type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> { fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
@ -174,7 +174,7 @@ where
} }
router router
}); });
Ok(Async::Ready(CloneableService::new(AppService { Ok(Async::Ready(CloneableService::new(FramedAppService {
router: router.finish(), router: router.finish(),
state: self.state.clone(), state: self.state.clone(),
}))) })))
@ -184,12 +184,12 @@ where
} }
} }
pub struct AppService<T, S> { pub struct FramedAppService<T, S> {
state: State<S>, state: State<S>,
router: Router<BoxedHttpService<FramedRequest<T, S>>>, router: Router<BoxedHttpService<FramedRequest<T, S>>>,
} }
impl<S: 'static, T: 'static> Service for AppService<T, S> impl<S: 'static, T: 'static> Service for FramedAppService<T, S>
where where
T: AsyncRead + AsyncWrite, T: AsyncRead + AsyncWrite,
{ {

View File

@ -7,7 +7,7 @@ mod state;
// re-export for convinience // re-export for convinience
pub use actix_http::{http, Error, HttpMessage, Response, ResponseError}; pub use actix_http::{http, Error, HttpMessage, Response, ResponseError};
pub use self::app::{App, AppService}; pub use self::app::{FramedApp, FramedAppService};
pub use self::request::FramedRequest; pub use self::request::FramedRequest;
pub use self::route::FramedRoute; pub use self::route::FramedRoute;
pub use self::state::State; pub use self::state::State;

View File

@ -15,55 +15,58 @@ use crate::request::FramedRequest;
/// ///
/// Route uses builder-like pattern for configuration. /// Route uses builder-like pattern for configuration.
/// If handler is not explicitly set, default *404 Not Found* handler is used. /// If handler is not explicitly set, default *404 Not Found* handler is used.
pub struct FramedRoute<Io, S, F, R> { pub struct FramedRoute<Io, S, F = (), R = ()> {
handler: F, handler: F,
pattern: String, pattern: String,
methods: Vec<Method>, methods: Vec<Method>,
state: PhantomData<(Io, S, R)>, state: PhantomData<(Io, S, R)>,
} }
impl<Io, S> FramedRoute<Io, S, (), ()> { impl<Io, S> FramedRoute<Io, S> {
pub fn build(path: &str) -> FramedRouteBuilder<Io, S> { pub fn new(pattern: &str) -> Self {
FramedRouteBuilder::new(path)
}
pub fn get(path: &str) -> FramedRouteBuilder<Io, S> {
FramedRouteBuilder::new(path).method(Method::GET)
}
pub fn post(path: &str) -> FramedRouteBuilder<Io, S> {
FramedRouteBuilder::new(path).method(Method::POST)
}
pub fn put(path: &str) -> FramedRouteBuilder<Io, S> {
FramedRouteBuilder::new(path).method(Method::PUT)
}
pub fn delete(path: &str) -> FramedRouteBuilder<Io, S> {
FramedRouteBuilder::new(path).method(Method::DELETE)
}
}
impl<Io, S, F, R> FramedRoute<Io, S, F, R>
where
F: FnMut(FramedRequest<Io, S>) -> R + Clone,
R: IntoFuture<Item = ()>,
R::Future: 'static,
R::Error: fmt::Display,
{
pub fn new(pattern: &str, handler: F) -> Self {
FramedRoute { FramedRoute {
handler, handler: (),
pattern: pattern.to_string(), pattern: pattern.to_string(),
methods: Vec::new(), methods: Vec::new(),
state: PhantomData, state: PhantomData,
} }
} }
pub fn get(path: &str) -> FramedRoute<Io, S> {
FramedRoute::new(path).method(Method::GET)
}
pub fn post(path: &str) -> FramedRoute<Io, S> {
FramedRoute::new(path).method(Method::POST)
}
pub fn put(path: &str) -> FramedRoute<Io, S> {
FramedRoute::new(path).method(Method::PUT)
}
pub fn delete(path: &str) -> FramedRoute<Io, S> {
FramedRoute::new(path).method(Method::DELETE)
}
pub fn method(mut self, method: Method) -> Self { pub fn method(mut self, method: Method) -> Self {
self.methods.push(method); self.methods.push(method);
self self
} }
pub fn to<F, R>(self, handler: F) -> FramedRoute<Io, S, F, R>
where
F: FnMut(FramedRequest<Io, S>) -> R,
R: IntoFuture<Item = ()>,
R::Future: 'static,
R::Error: fmt::Debug,
{
FramedRoute {
handler,
pattern: self.pattern,
methods: self.methods,
state: PhantomData,
}
}
} }
impl<Io, S, F, R> HttpServiceFactory for FramedRoute<Io, S, F, R> impl<Io, S, F, R> HttpServiceFactory for FramedRoute<Io, S, F, R>
@ -151,39 +154,3 @@ where
})) }))
} }
} }
pub struct FramedRouteBuilder<Io, S> {
pattern: String,
methods: Vec<Method>,
state: PhantomData<(Io, S)>,
}
impl<Io, S> FramedRouteBuilder<Io, S> {
fn new(path: &str) -> FramedRouteBuilder<Io, S> {
FramedRouteBuilder {
pattern: path.to_string(),
methods: Vec::new(),
state: PhantomData,
}
}
pub fn method(mut self, method: Method) -> Self {
self.methods.push(method);
self
}
pub fn to<F, R>(self, handler: F) -> FramedRoute<Io, S, F, R>
where
F: FnMut(FramedRequest<Io, S>) -> R,
R: IntoFuture<Item = ()>,
R::Future: 'static,
R::Error: fmt::Debug,
{
FramedRoute {
handler,
pattern: self.pattern,
methods: self.methods,
state: PhantomData,
}
}
}

View File

@ -6,7 +6,7 @@ use bytes::{Bytes, BytesMut};
use futures::future::{self, ok}; use futures::future::{self, ok};
use futures::{Future, Sink, Stream}; use futures::{Future, Sink, Stream};
use actix_framed::{App, FramedRequest, FramedRoute}; use actix_framed::{FramedApp, FramedRequest, FramedRoute};
fn ws_service<T: AsyncRead + AsyncWrite>( fn ws_service<T: AsyncRead + AsyncWrite>(
req: FramedRequest<T>, req: FramedRequest<T>,
@ -40,7 +40,9 @@ fn service(msg: ws::Frame) -> impl Future<Item = ws::Message, Error = Error> {
fn test_simple() { fn test_simple() {
let mut srv = TestServer::new(|| { let mut srv = TestServer::new(|| {
HttpService::build() HttpService::build()
.upgrade(App::new().service(FramedRoute::get("/index.html").to(ws_service))) .upgrade(
FramedApp::new().service(FramedRoute::get("/index.html").to(ws_service)),
)
.finish(|_| future::ok::<_, Error>(Response::NotFound())) .finish(|_| future::ok::<_, Error>(Response::NotFound()))
}); });