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; use httpmessage::{HttpRequest, HttpResponse, IntoHttpResponse}; /// Resource pub struct HttpResource { state: PhantomData, routes: HashMap>>, default: Box>, } impl Default for HttpResource { fn default() -> Self { HttpResource { state: PhantomData, routes: HashMap::new(), default: Box::new(HTTPMethodNotAllowed)} } } impl HttpResource where S: 'static { /// Register handler for specified method. pub fn handler(&mut self, method: Method, handler: H) -> &mut Self where H: RouteHandler { self.routes.insert(method, Box::new(handler)); self } /// Default handler is used if no matched route found. /// By default `HTTPMethodNotAllowed` is used. pub fn default_handler(&mut self, handler: H) -> &mut Self where H: RouteHandler { self.default = Box::new(handler); self } /// Handler for `GET` method. pub fn get(&mut self) -> &mut Self where A: Route { self.handler(Method::GET, A::factory()) } /// Handler for `POST` method. pub fn post(&mut self) -> &mut Self where A: Route { self.handler(Method::POST, A::factory()) } /// Handler for `PUR` method. pub fn put(&mut self) -> &mut Self where A: Route { self.handler(Method::PUT, A::factory()) } /// Handler for `METHOD` method. pub fn delete(&mut self) -> &mut Self where A: Route { self.handler(Method::DELETE, A::factory()) } } impl RouteHandler for HttpResource { fn handle(&self, req: HttpRequest, payload: Option, state: Rc) -> 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))] enum HttpMessageItem where A: Actor> + Route { Message(HttpResponse), Actor(A), } pub struct HttpMessage> + Route> (HttpMessageItem); impl HttpMessage where A: Actor> + Route { /// Create async response pub fn stream(act: A) -> Self { HttpMessage(HttpMessageItem::Actor(act)) } /// Send response pub fn reply(msg: HttpResponse) -> Self { HttpMessage(HttpMessageItem::Message(msg)) } /// Send response pub fn reply_with(req: HttpRequest, msg: I) -> Self { HttpMessage(HttpMessageItem::Message(msg.response(req))) } pub(crate) fn into(self, mut ctx: HttpContext) -> Task { match self.0 { HttpMessageItem::Message(msg) => { Task::reply(msg) }, HttpMessageItem::Actor(act) => { ctx.set_actor(act); Task::with_stream(ctx) } } } }