1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 21:39:26 +00:00
actix-web/src/resource.rs

122 lines
3.7 KiB
Rust
Raw Normal View History

2017-10-07 04:48:14 +00:00
use std::marker::PhantomData;
use std::collections::HashMap;
use http::Method;
2017-11-30 22:42:20 +00:00
use futures::Future;
2017-10-07 04:48:14 +00:00
use error::Error;
use route::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
use httprequest::HttpRequest;
2017-11-30 22:42:20 +00:00
use httpresponse::HttpResponse;
2017-10-29 13:05:31 +00:00
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
2017-10-07 04:48:14 +00:00
2017-10-08 06:59:57 +00:00
/// Http resource
///
2017-10-08 21:56:51 +00:00
/// `Resource` is an entry in route table which corresponds to requested URL.
2017-10-08 06:59:57 +00:00
///
/// Resource in turn has at least one route.
/// Route corresponds to handling HTTP method by calling route handler.
///
2017-11-29 21:26:55 +00:00
/// ```rust
/// extern crate actix_web;
2017-10-08 06:59:57 +00:00
///
/// fn main() {
2017-11-29 21:26:55 +00:00
/// let app = actix_web::Application::default("/")
/// .resource("/", |r| r.get(|_| actix_web::HttpResponse::Ok()))
2017-10-15 21:17:41 +00:00
/// .finish();
2017-10-08 06:59:57 +00:00
/// }
2017-10-08 21:56:51 +00:00
pub struct Resource<S=()> {
2017-10-17 02:21:24 +00:00
name: String,
2017-10-07 04:48:14 +00:00
state: PhantomData<S>,
routes: HashMap<Method, Box<RouteHandler<S>>>,
default: Box<RouteHandler<S>>,
}
2017-10-08 21:56:51 +00:00
impl<S> Default for Resource<S> {
2017-10-07 04:48:14 +00:00
fn default() -> Self {
2017-10-08 21:56:51 +00:00
Resource {
2017-10-17 02:21:24 +00:00
name: String::new(),
2017-10-07 04:48:14 +00:00
state: PhantomData,
routes: HashMap::new(),
default: Box::new(HTTPMethodNotAllowed)}
}
}
2017-10-08 21:56:51 +00:00
impl<S> Resource<S> where S: 'static {
2017-10-07 04:48:14 +00:00
2017-10-29 13:05:31 +00:00
pub(crate) fn default_not_found() -> Self {
Resource {
name: String::new(),
state: PhantomData,
routes: HashMap::new(),
default: Box::new(HTTPNotFound)}
}
2017-10-17 02:21:24 +00:00
/// Set resource name
pub fn name<T: Into<String>>(&mut self, name: T) {
2017-11-27 01:30:35 +00:00
self.name = name.into();
2017-10-17 02:21:24 +00:00
}
2017-10-07 06:14:13 +00:00
/// Register handler for specified method.
2017-10-15 21:17:41 +00:00
pub fn handler<F, R>(&mut self, method: Method, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static,
2017-10-15 21:17:41 +00:00
{
2017-11-29 21:26:55 +00:00
self.routes.insert(method, Box::new(WrapHandler::new(handler)));
2017-10-15 21:17:41 +00:00
}
/// Register async handler for specified method.
pub fn async<F, R>(&mut self, method: Method, handler: F)
2017-11-27 05:18:38 +00:00
where F: Fn(HttpRequest<S>) -> R + 'static,
2017-11-30 22:42:20 +00:00
R: Future<Item=HttpResponse, Error=Error> + 'static,
2017-10-15 21:17:41 +00:00
{
self.routes.insert(method, Box::new(AsyncHandler::new(handler)));
2017-10-15 21:17:41 +00:00
}
2017-10-07 06:14:13 +00:00
/// Default handler is used if no matched route found.
/// By default `HTTPMethodNotAllowed` is used.
2017-11-29 21:26:55 +00:00
pub fn default_handler<H>(&mut self, handler: H) where H: Handler<S>
2017-10-07 04:48:14 +00:00
{
2017-11-29 21:26:55 +00:00
self.default = Box::new(WrapHandler::new(handler));
2017-10-07 04:48:14 +00:00
}
2017-11-29 21:41:51 +00:00
/// Register handler for `GET` method.
2017-11-29 18:31:24 +00:00
pub fn get<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static, {
2017-11-29 21:26:55 +00:00
self.routes.insert(Method::GET, Box::new(WrapHandler::new(handler)));
2017-10-07 04:48:14 +00:00
}
2017-11-29 21:41:51 +00:00
/// Register handler for `POST` method.
2017-11-29 18:31:24 +00:00
pub fn post<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static, {
2017-11-29 21:26:55 +00:00
self.routes.insert(Method::POST, Box::new(WrapHandler::new(handler)));
2017-10-07 04:48:14 +00:00
}
2017-11-29 21:41:51 +00:00
/// Register handler for `PUT` method.
2017-11-29 18:31:24 +00:00
pub fn put<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static, {
2017-11-29 21:26:55 +00:00
self.routes.insert(Method::PUT, Box::new(WrapHandler::new(handler)));
2017-10-07 04:48:14 +00:00
}
2017-11-29 21:41:51 +00:00
/// Register handler for `DELETE` method.
2017-11-29 18:31:24 +00:00
pub fn delete<F, R>(&mut self, handler: F)
where F: Fn(HttpRequest<S>) -> R + 'static,
R: FromRequest + 'static, {
2017-11-29 21:26:55 +00:00
self.routes.insert(Method::DELETE, Box::new(WrapHandler::new(handler)));
2017-10-07 04:48:14 +00:00
}
}
2017-10-08 21:56:51 +00:00
impl<S: 'static> RouteHandler<S> for Resource<S> {
2017-10-07 04:48:14 +00:00
2017-11-30 23:13:56 +00:00
fn handle(&self, req: HttpRequest<S>) -> Reply {
2017-10-07 04:48:14 +00:00
if let Some(handler) = self.routes.get(req.method()) {
2017-11-30 23:13:56 +00:00
handler.handle(req)
2017-10-07 04:48:14 +00:00
} else {
2017-11-30 23:13:56 +00:00
self.default.handle(req)
2017-10-07 04:48:14 +00:00
}
}
}