Skip to content

Commit

Permalink
refactor service registration process; unify services and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Mar 6, 2019
1 parent 5cde4dc commit fe22e83
Show file tree
Hide file tree
Showing 18 changed files with 846 additions and 780 deletions.
26 changes: 11 additions & 15 deletions actix-session/src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl CookieSessionInner {
///
/// ```rust
/// use actix_session::CookieSession;
/// use actix_web::{App, HttpResponse, HttpServer};
/// use actix_web::{web, App, HttpResponse, HttpServer};
///
/// fn main() {
/// let app = App::new().middleware(
Expand All @@ -183,7 +183,7 @@ impl CookieSessionInner {
/// .name("actix_session")
/// .path("/")
/// .secure(true))
/// .resource("/", |r| r.to(|| HttpResponse::Ok()));
/// .service(web::resource("/").to(|| HttpResponse::Ok()));
/// }
/// ```
pub struct CookieSession(Rc<CookieSessionInner>);
Expand Down Expand Up @@ -314,19 +314,17 @@ where
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{test, App};
use actix_web::{test, web, App};

#[test]
fn cookie_session() {
let mut app = test::init_service(
App::new()
.middleware(CookieSession::signed(&[0; 32]).secure(false))
.resource("/", |r| {
r.to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
})
}),
.service(web::resource("/").to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
})),
);

let request = test::TestRequest::get().to_request();
Expand All @@ -342,12 +340,10 @@ mod tests {
let mut app = test::init_service(
App::new()
.middleware(CookieSession::signed(&[0; 32]).secure(false))
.resource("/", |r| {
r.to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
})
}),
.service(web::resource("/").to(|ses: Session| {
let _ = ses.set("counter", 100);
"test"
})),
);

let request = test::TestRequest::get().to_request();
Expand Down
14 changes: 6 additions & 8 deletions actix-session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! extractor allows us to get or set session data.
//!
//! ```rust
//! use actix_web::{App, HttpServer, HttpResponse, Error};
//! use actix_web::{web, App, HttpServer, HttpResponse, Error};
//! use actix_session::{Session, CookieSession};
//!
//! fn index(session: Session) -> Result<&'static str, Error> {
Expand All @@ -29,19 +29,17 @@
//! }
//!
//! fn main() -> std::io::Result<()> {
//! let sys = actix_rt::System::new("example"); // <- create Actix runtime
//!
//! # std::thread::spawn(||
//! HttpServer::new(
//! || App::new().middleware(
//! CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
//! .secure(false)
//! )
//! .resource("/", |r| r.to(|| HttpResponse::Ok())))
//! .service(web::resource("/").to(|| HttpResponse::Ok())))
//! .bind("127.0.0.1:59880")?
//! .start();
//! # actix_rt::System::current().stop();
//! sys.run();
//! Ok(())
//! .run()
//! # );
//! # Ok(())
//! }
//! ```
use std::cell::RefCell;
Expand Down
30 changes: 18 additions & 12 deletions actix-staticfiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use v_htmlescape::escape as escape_html_entity;

use actix_http::error::{Error, ErrorInternalServerError};
use actix_service::{boxed::BoxedNewService, NewService, Service};
use actix_web::dev::{self, HttpServiceFactory, ResourceDef, Url};
use actix_web::dev::{self, AppConfig, HttpServiceFactory, ResourceDef, Url};
use actix_web::{
blocking, FromRequest, HttpRequest, HttpResponse, Responder, ServiceFromRequest,
ServiceRequest, ServiceResponse,
Expand Down Expand Up @@ -226,7 +226,7 @@ fn directory_listing(
/// }
/// ```
pub struct StaticFiles<S, C = DefaultConfig> {
path: ResourceDef,
path: String,
directory: PathBuf,
index: Option<String>,
show_index: bool,
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
}

StaticFiles {
path: ResourceDef::root_prefix(path),
path: path.to_string(),
directory: dir,
index: None,
show_index: false,
Expand Down Expand Up @@ -300,15 +300,21 @@ impl<S: 'static, C: StaticFileConfig> StaticFiles<S, C> {
}
}

impl<P, C: StaticFileConfig + 'static> HttpServiceFactory<P> for StaticFiles<P, C> {
type Factory = Self;

fn rdef(&self) -> &ResourceDef {
&self.path
}

fn create(self) -> Self {
self
impl<P, C> HttpServiceFactory<P> for StaticFiles<P, C>
where
P: 'static,
C: StaticFileConfig + 'static,
{
fn register(self, config: &mut AppConfig<P>) {
if self.default.borrow().is_none() {
*self.default.borrow_mut() = Some(config.default_service());
}
let rdef = if config.is_root() {
ResourceDef::root_prefix(&self.path)
} else {
ResourceDef::prefix(&self.path)
};
config.register_service(rdef, None, self)
}
}

Expand Down
28 changes: 14 additions & 14 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ fn main() -> std::io::Result<()> {
App::new()
.middleware(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
.middleware(middleware::Compress::default())
.resource("/resource1/index.html", |r| r.route(web::get().to(index)))
.resource("/resource2/index.html", |r| {
r.middleware(
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
)
.default_resource(|r| {
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
})
.route(web::method(Method::GET).to_async(index_async))
})
.resource("/test1.html", |r| r.to(|| "Test\r\n"))
.resource("/", |r| r.to(no_params))
.service(web::resource("/resource1/index.html").route(web::get().to(index)))
.service(
web::resource("/resource2/index.html")
.middleware(
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
)
.default_resource(|r| {
r.route(web::route().to(|| HttpResponse::MethodNotAllowed()))
})
.route(web::method(Method::GET).to_async(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))
.service(web::resource("/").to(no_params))
})
.bind("127.0.0.1:8080")?
.workers(1)
.start();

let _ = sys.run();
Ok(())
sys.run()
}
Loading

0 comments on commit fe22e83

Please sign in to comment.