From 7cd59c38d386c407e45b4bdc1a9ad652ad9aab5f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 10 Apr 2019 18:08:28 -0700 Subject: [PATCH] rename framed App --- actix-framed/src/app.rs | 32 +++++----- actix-framed/src/lib.rs | 2 +- actix-framed/src/route.rs | 103 ++++++++++-------------------- actix-framed/tests/test_server.rs | 6 +- 4 files changed, 56 insertions(+), 87 deletions(-) diff --git a/actix-framed/src/app.rs b/actix-framed/src/app.rs index 35486e5c..d8a273d7 100644 --- a/actix-framed/src/app.rs +++ b/actix-framed/src/app.rs @@ -23,23 +23,23 @@ pub trait HttpServiceFactory { } /// Application builder -pub struct App { +pub struct FramedApp { state: State, services: Vec<(String, BoxedHttpNewService>)>, } -impl App { +impl FramedApp { pub fn new() -> Self { - App { + FramedApp { state: State::new(()), services: Vec::new(), } } } -impl App { - pub fn with(state: S) -> App { - App { +impl FramedApp { + pub fn with(state: S) -> FramedApp { + FramedApp { services: Vec::new(), state: State::new(state), } @@ -69,13 +69,13 @@ impl App { } } -impl IntoNewService> for App +impl IntoNewService> for FramedApp where T: AsyncRead + AsyncWrite + 'static, S: 'static, { - fn into_new_service(self) -> AppFactory { - AppFactory { + fn into_new_service(self) -> FramedAppFactory { + FramedAppFactory { state: self.state, services: Rc::new(self.services), } @@ -83,12 +83,12 @@ where } #[derive(Clone)] -pub struct AppFactory { +pub struct FramedAppFactory { state: State, services: Rc>)>>, } -impl NewService for AppFactory +impl NewService for FramedAppFactory where T: AsyncRead + AsyncWrite + 'static, S: 'static, @@ -97,7 +97,7 @@ where type Response = (); type Error = Error; type InitError = (); - type Service = CloneableService>; + type Service = CloneableService>; type Future = CreateService; fn new_service(&self, _: &()) -> Self::Future { @@ -135,7 +135,7 @@ impl Future for CreateService where T: AsyncRead + AsyncWrite, { - type Item = CloneableService>; + type Item = CloneableService>; type Error = (); fn poll(&mut self) -> Poll { @@ -174,7 +174,7 @@ where } router }); - Ok(Async::Ready(CloneableService::new(AppService { + Ok(Async::Ready(CloneableService::new(FramedAppService { router: router.finish(), state: self.state.clone(), }))) @@ -184,12 +184,12 @@ where } } -pub struct AppService { +pub struct FramedAppService { state: State, router: Router>>, } -impl Service for AppService +impl Service for FramedAppService where T: AsyncRead + AsyncWrite, { diff --git a/actix-framed/src/lib.rs b/actix-framed/src/lib.rs index 6cc36446..a67b82e4 100644 --- a/actix-framed/src/lib.rs +++ b/actix-framed/src/lib.rs @@ -7,7 +7,7 @@ mod state; // re-export for convinience 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::route::FramedRoute; pub use self::state::State; diff --git a/actix-framed/src/route.rs b/actix-framed/src/route.rs index 4f5c4e69..c8d9d432 100644 --- a/actix-framed/src/route.rs +++ b/actix-framed/src/route.rs @@ -15,55 +15,58 @@ use crate::request::FramedRequest; /// /// Route uses builder-like pattern for configuration. /// If handler is not explicitly set, default *404 Not Found* handler is used. -pub struct FramedRoute { +pub struct FramedRoute { handler: F, pattern: String, methods: Vec, state: PhantomData<(Io, S, R)>, } -impl FramedRoute { - pub fn build(path: &str) -> FramedRouteBuilder { - FramedRouteBuilder::new(path) - } - - pub fn get(path: &str) -> FramedRouteBuilder { - FramedRouteBuilder::new(path).method(Method::GET) - } - - pub fn post(path: &str) -> FramedRouteBuilder { - FramedRouteBuilder::new(path).method(Method::POST) - } - - pub fn put(path: &str) -> FramedRouteBuilder { - FramedRouteBuilder::new(path).method(Method::PUT) - } - - pub fn delete(path: &str) -> FramedRouteBuilder { - FramedRouteBuilder::new(path).method(Method::DELETE) - } -} - -impl FramedRoute -where - F: FnMut(FramedRequest) -> R + Clone, - R: IntoFuture, - R::Future: 'static, - R::Error: fmt::Display, -{ - pub fn new(pattern: &str, handler: F) -> Self { +impl FramedRoute { + pub fn new(pattern: &str) -> Self { FramedRoute { - handler, + handler: (), pattern: pattern.to_string(), methods: Vec::new(), state: PhantomData, } } + pub fn get(path: &str) -> FramedRoute { + FramedRoute::new(path).method(Method::GET) + } + + pub fn post(path: &str) -> FramedRoute { + FramedRoute::new(path).method(Method::POST) + } + + pub fn put(path: &str) -> FramedRoute { + FramedRoute::new(path).method(Method::PUT) + } + + pub fn delete(path: &str) -> FramedRoute { + FramedRoute::new(path).method(Method::DELETE) + } + pub fn method(mut self, method: Method) -> Self { self.methods.push(method); self } + + pub fn to(self, handler: F) -> FramedRoute + where + F: FnMut(FramedRequest) -> R, + R: IntoFuture, + R::Future: 'static, + R::Error: fmt::Debug, + { + FramedRoute { + handler, + pattern: self.pattern, + methods: self.methods, + state: PhantomData, + } + } } impl HttpServiceFactory for FramedRoute @@ -151,39 +154,3 @@ where })) } } - -pub struct FramedRouteBuilder { - pattern: String, - methods: Vec, - state: PhantomData<(Io, S)>, -} - -impl FramedRouteBuilder { - fn new(path: &str) -> FramedRouteBuilder { - 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(self, handler: F) -> FramedRoute - where - F: FnMut(FramedRequest) -> R, - R: IntoFuture, - R::Future: 'static, - R::Error: fmt::Debug, - { - FramedRoute { - handler, - pattern: self.pattern, - methods: self.methods, - state: PhantomData, - } - } -} diff --git a/actix-framed/tests/test_server.rs b/actix-framed/tests/test_server.rs index 6a21b3fc..09d2c03c 100644 --- a/actix-framed/tests/test_server.rs +++ b/actix-framed/tests/test_server.rs @@ -6,7 +6,7 @@ use bytes::{Bytes, BytesMut}; use futures::future::{self, ok}; use futures::{Future, Sink, Stream}; -use actix_framed::{App, FramedRequest, FramedRoute}; +use actix_framed::{FramedApp, FramedRequest, FramedRoute}; fn ws_service( req: FramedRequest, @@ -40,7 +40,9 @@ fn service(msg: ws::Frame) -> impl Future { fn test_simple() { let mut srv = TestServer::new(|| { 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())) });