1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 09:59:21 +02:00

renamed Route::handler to Route::f, added Route::h to register Handler

This commit is contained in:
Nikolay Kim
2017-12-04 14:07:53 -08:00
parent 03f7d95d88
commit f5d6179a34
19 changed files with 79 additions and 40 deletions

View File

@ -134,8 +134,8 @@ impl<S> ApplicationBuilder<S> where S: 'static {
/// fn main() {
/// let app = Application::default("/")
/// .resource("/test", |r| {
/// r.method(Method::GET).handler(|_| httpcodes::HTTPOk);
/// r.method(Method::HEAD).handler(|_| httpcodes::HTTPMethodNotAllowed);
/// r.method(Method::GET).f(|_| httpcodes::HTTPOk);
/// r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed);
/// })
/// .finish();
/// }

View File

@ -3,7 +3,7 @@
use http::{StatusCode, Error as HttpError};
use body::Body;
use route::{Reply, RouteHandler, FromRequest};
use route::{Reply, Handler, RouteHandler, FromRequest};
use httprequest::HttpRequest;
use httpresponse::{HttpResponse, HttpResponseBuilder};
@ -67,6 +67,14 @@ impl StaticResponse {
}
}
impl<S> Handler<S> for StaticResponse {
type Result = HttpResponse;
fn handle(&self, _: HttpRequest<S>) -> HttpResponse {
HttpResponse::new(self.0, Body::Empty)
}
}
impl<S> RouteHandler<S> for StaticResponse {
fn handle(&self, _: HttpRequest<S>) -> Reply {
Reply::response(HttpResponse::new(self.0, Body::Empty))

View File

@ -21,8 +21,8 @@ use middlewares::{Response, Middleware};
/// .header("X-Version", "0.2")
/// .finish())
/// .resource("/test", |r| {
/// r.method(Method::GET).handler(|_| httpcodes::HTTPOk);
/// r.method(Method::HEAD).handler(|_| httpcodes::HTTPMethodNotAllowed);
/// r.method(Method::GET).f(|_| httpcodes::HTTPOk);
/// r.method(Method::HEAD).f(|_| httpcodes::HTTPMethodNotAllowed);
/// })
/// .finish();
/// }

View File

@ -10,7 +10,10 @@ use httpcodes::HTTPNotFound;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
/// Resource route definition. Route uses builder-like pattern for configuration.
/// Resource route definition
///
/// Route uses builder-like pattern for configuration.
/// If handler is not explicitly set, default *404 Not Found* handler is used.
pub struct Route<S> {
preds: Vec<Box<Predicate<S>>>,
handler: Box<RouteHandler<S>>,
@ -55,16 +58,21 @@ impl<S: 'static> Route<S> {
self
}
/// Add method check to route. This method could be called multiple times.
pub fn method(&mut self, method: Method) -> &mut Self {
self.preds.push(pred::Method(method));
self
}
/// Set handler object. Usually call to this method is last call
/// during route configuration, because it does not return reference to self.
pub fn h<H: Handler<S>>(&mut self, handler: H) {
self.handler = Box::new(WrapHandler::new(handler));
}
/// Set handler function. Usually call to this method is last call
/// during route configuration, because it does not return reference to self.
pub fn handler<F, R>(&mut self, handler: F)
pub fn f<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static,
{
@ -99,7 +107,7 @@ impl<S: 'static> Route<S> {
/// fn main() {
/// let app = Application::default("/")
/// .resource(
/// "/", |r| r.route().method(Method::GET).handler(|r| HttpResponse::Ok()))
/// "/", |r| r.route().method(Method::GET).f(|r| HttpResponse::Ok()))
/// .finish();
/// }
pub struct Resource<S=()> {
@ -147,7 +155,7 @@ impl<S> Resource<S> where S: 'static {
/// "/", |r| r.route()
/// .p(pred::Any(vec![pred::Get(), pred::Put()]))
/// .p(pred::Header("Content-Type", "text/plain"))
/// .handler(|r| HttpResponse::Ok()))
/// .f(|r| HttpResponse::Ok()))
/// .finish();
/// }
pub fn route(&mut self) -> &mut Route<S> {

View File

@ -227,6 +227,27 @@ impl<S, R, F> RouteHandler<S> for AsyncHandler<S, R, F>
}
/// Json response helper
///
/// The `Json` type allows you to respond with well-formed JSON data: simply return a value of
/// type Json<T> where T is the type of a structure to serialize into *JSON*. The
/// type `T` must implement the `Serialize` trait from *serde*.
///
/// ```rust
/// # extern crate actix_web;
/// # #[macro_use] extern crate serde_derive;
/// # use actix_web::*;
/// #
/// #[derive(Serialize)]
/// struct MyObj {
/// name: String,
/// }
///
/// fn index(req: HttpRequest) -> Result<Json<MyObj>> {
/// Ok(Json(MyObj{name: req.match_info().query("name")?}))
/// }
/// # fn main() {}
/// ```
pub struct Json<T: Serialize> (pub T);
impl<T: Serialize> FromRequest for Json<T> {

View File

@ -43,7 +43,7 @@
//!
//! fn main() {
//! Application::default("/")
//! .resource("/ws/", |r| r.method(Method::GET).handler(ws_index)) // <- register websocket route
//! .resource("/ws/", |r| r.method(Method::GET).f(ws_index)) // <- register websocket route
//! .finish();
//! }
//! ```