1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00
actix-extras/src/resource.rs

122 lines
3.2 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
use std::rc::Rc;
use std::marker::PhantomData;
use std::collections::HashMap;
use actix::Actor;
use http::Method;
use task::Task;
use route::{Route, Payload, RouteHandler};
use context::HttpContext;
use httpcodes::HTTPMethodNotAllowed;
2017-10-07 08:14:13 +02:00
use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse};
2017-10-07 06:48:14 +02:00
/// Resource
pub struct HttpResource<S=()> {
state: PhantomData<S>,
routes: HashMap<Method, Box<RouteHandler<S>>>,
default: Box<RouteHandler<S>>,
}
impl<S> Default for HttpResource<S> {
fn default() -> Self {
HttpResource {
state: PhantomData,
routes: HashMap::new(),
default: Box::new(HTTPMethodNotAllowed)}
}
}
impl<S> HttpResource<S> where S: 'static {
2017-10-07 08:14:13 +02:00
/// Register handler for specified method.
2017-10-07 06:48:14 +02:00
pub fn handler<H>(&mut self, method: Method, handler: H) -> &mut Self
where H: RouteHandler<S>
{
self.routes.insert(method, Box::new(handler));
self
}
2017-10-07 08:14:13 +02:00
/// Default handler is used if no matched route found.
/// By default `HTTPMethodNotAllowed` is used.
2017-10-07 06:48:14 +02:00
pub fn default_handler<H>(&mut self, handler: H) -> &mut Self
where H: RouteHandler<S>
{
self.default = Box::new(handler);
self
}
2017-10-07 08:14:13 +02:00
/// Handler for `GET` method.
2017-10-07 06:48:14 +02:00
pub fn get<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::GET, A::factory())
}
2017-10-07 08:14:13 +02:00
/// Handler for `POST` method.
2017-10-07 06:48:14 +02:00
pub fn post<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::POST, A::factory())
}
2017-10-07 08:14:13 +02:00
/// Handler for `PUR` method.
2017-10-07 06:48:14 +02:00
pub fn put<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::PUT, A::factory())
}
2017-10-07 08:14:13 +02:00
/// Handler for `METHOD` method.
2017-10-07 06:48:14 +02:00
pub fn delete<A>(&mut self) -> &mut Self where A: Route<State=S>
{
self.handler(Method::DELETE, A::factory())
}
}
impl<S: 'static> RouteHandler<S> for HttpResource<S> {
fn handle(&self, req: HttpRequest, payload: Option<Payload>, state: Rc<S>) -> Task {
if let Some(handler) = self.routes.get(req.method()) {
handler.handle(req, payload, state)
} else {
self.default.handle(req, payload, state)
}
}
}
#[cfg_attr(feature="cargo-clippy", allow(large_enum_variant))]
2017-10-07 08:14:13 +02:00
enum HttpMessageItem<A> where A: Actor<Context=HttpContext<A>> + Route {
2017-10-07 08:36:36 +02:00
Message(HttpResponse),
2017-10-07 06:48:14 +02:00
Actor(A),
}
2017-10-07 08:14:13 +02:00
pub struct HttpMessage<A: Actor<Context=HttpContext<A>> + Route> (HttpMessageItem<A>);
2017-10-07 06:48:14 +02:00
2017-10-07 08:14:13 +02:00
impl<A> HttpMessage<A> where A: Actor<Context=HttpContext<A>> + Route
2017-10-07 06:48:14 +02:00
{
/// Create async response
2017-10-07 08:36:36 +02:00
pub fn stream(act: A) -> Self {
2017-10-07 08:14:13 +02:00
HttpMessage(HttpMessageItem::Actor(act))
2017-10-07 06:48:14 +02:00
}
2017-10-07 08:36:36 +02:00
/// Create response with empty body
pub fn reply<I>(req: HttpRequest, msg: I) -> Self
2017-10-07 08:14:13 +02:00
where I: IntoHttpResponse
2017-10-07 06:48:14 +02:00
{
2017-10-07 08:36:36 +02:00
HttpMessage(HttpMessageItem::Message(msg.into_response(req)))
2017-10-07 06:48:14 +02:00
}
pub(crate) fn into(self, mut ctx: HttpContext<A>) -> Task {
match self.0 {
2017-10-07 08:36:36 +02:00
HttpMessageItem::Message(msg) => {
Task::reply(msg)
2017-10-07 08:14:13 +02:00
},
HttpMessageItem::Actor(act) => {
ctx.set_actor(act);
2017-10-07 06:48:14 +02:00
Task::with_stream(ctx)
}
}
}
}