1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00
actix-web/src/application.rs

309 lines
8.8 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
use std::rc::Rc;
use std::string::ToString;
use std::collections::HashMap;
use route_recognizer::Router;
use task::Task;
2017-10-15 23:17:41 +02:00
use route::{RouteHandler, FnHandler};
2017-10-08 23:56:51 +02:00
use router::Handler;
use resource::Resource;
2017-10-09 05:16:48 +02:00
use payload::Payload;
use httprequest::HttpRequest;
2017-10-15 23:17:41 +02:00
use httpresponse::HttpResponse;
2017-10-07 06:48:14 +02:00
/// Application
2017-10-08 23:56:51 +02:00
pub struct Application<S=()> {
2017-10-07 06:48:14 +02:00
state: S,
2017-10-08 23:56:51 +02:00
default: Resource<S>,
2017-10-10 08:07:32 +02:00
handlers: HashMap<String, Box<RouteHandler<S>>>,
2017-10-08 23:56:51 +02:00
resources: HashMap<String, Resource<S>>,
2017-10-07 06:48:14 +02:00
}
2017-10-08 23:56:51 +02:00
impl<S> Application<S> where S: 'static
2017-10-07 06:48:14 +02:00
{
2017-10-08 23:56:51 +02:00
pub(crate) fn prepare(self, prefix: String) -> Box<Handler> {
2017-10-07 06:48:14 +02:00
let mut router = Router::new();
2017-10-10 08:07:32 +02:00
let mut handlers = HashMap::new();
2017-10-07 06:48:14 +02:00
let prefix = if prefix.ends_with('/') {prefix } else { prefix + "/" };
for (path, handler) in self.resources {
let path = prefix.clone() + path.trim_left_matches('/');
router.add(path.as_str(), handler);
}
2017-10-10 08:07:32 +02:00
for (path, mut handler) in self.handlers {
let path = prefix.clone() + path.trim_left_matches('/');
handler.set_prefix(path.clone());
handlers.insert(path, handler);
}
2017-10-07 06:48:14 +02:00
Box::new(
InnerApplication {
state: Rc::new(self.state),
default: self.default,
2017-10-10 08:07:32 +02:00
handlers: handlers,
2017-10-07 06:48:14 +02:00
router: router }
)
}
}
2017-10-07 08:14:13 +02:00
2017-10-15 23:17:41 +02:00
impl Application<()> {
/// Create default `ApplicationBuilder` with no state
pub fn default() -> ApplicationBuilder<()> {
ApplicationBuilder {
parts: Some(ApplicationBuilderParts {
state: (),
default: Resource::default(),
handlers: HashMap::new(),
resources: HashMap::new()})
2017-10-07 06:48:14 +02:00
}
}
}
2017-10-08 23:56:51 +02:00
impl<S> Application<S> where S: 'static {
2017-10-07 06:48:14 +02:00
2017-10-15 23:17:41 +02:00
/// Create application builder
pub fn builder(state: S) -> ApplicationBuilder<S> {
ApplicationBuilder {
parts: Some(ApplicationBuilderParts {
state: state,
default: Resource::default(),
handlers: HashMap::new(),
resources: HashMap::new()})
}
}
2017-10-07 08:14:13 +02:00
/// Create http application with specific state. State is shared with all
/// routes within same application and could be
/// accessed with `HttpContext::state()` method.
2017-10-08 23:56:51 +02:00
pub fn new(state: S) -> Application<S> {
Application {
2017-10-07 06:48:14 +02:00
state: state,
2017-10-08 23:56:51 +02:00
default: Resource::default(),
2017-10-10 08:07:32 +02:00
handlers: HashMap::new(),
2017-10-07 06:48:14 +02:00
resources: HashMap::new(),
}
}
2017-10-07 08:14:13 +02:00
/// Add resource by path.
2017-10-15 23:17:41 +02:00
pub fn resource<P: ToString>(&mut self, path: P) -> &mut Resource<S>
2017-10-07 06:48:14 +02:00
{
let path = path.to_string();
// add resource
if !self.resources.contains_key(&path) {
2017-10-08 23:56:51 +02:00
self.resources.insert(path.clone(), Resource::default());
2017-10-07 06:48:14 +02:00
}
self.resources.get_mut(&path).unwrap()
}
2017-10-15 23:17:41 +02:00
/// This method register handler for specified path.
///
/// ```rust
/// extern crate actix_web;
/// use actix_web::*;
///
/// fn main() {
/// let mut app = Application::new(());
///
/// app.handler("/test", |req, payload, state| {
/// httpcodes::HTTPOk
/// });
/// }
/// ```
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
R: Into<HttpResponse> + 'static,
P: ToString,
{
self.handlers.insert(path.to_string(), Box::new(FnHandler::new(handler)));
self
}
2017-10-10 08:07:32 +02:00
/// Add path handler
2017-10-15 23:17:41 +02:00
pub fn route_handler<H, P>(&mut self, path: P, h: H)
2017-10-10 08:07:32 +02:00
where H: RouteHandler<S> + 'static, P: ToString
{
let path = path.to_string();
// add resource
if self.handlers.contains_key(&path) {
panic!("Handler already registered: {:?}", path);
}
self.handlers.insert(path, Box::new(h));
}
2017-10-08 23:56:51 +02:00
/// Default resource is used if no matches route could be found.
pub fn default_resource(&mut self) -> &mut Resource<S> {
2017-10-07 06:48:14 +02:00
&mut self.default
}
}
2017-10-15 23:17:41 +02:00
struct ApplicationBuilderParts<S> {
state: S,
default: Resource<S>,
handlers: HashMap<String, Box<RouteHandler<S>>>,
resources: HashMap<String, Resource<S>>,
}
impl<S> From<ApplicationBuilderParts<S>> for Application<S> {
fn from(b: ApplicationBuilderParts<S>) -> Self {
Application {
state: b.state,
default: b.default,
handlers: b.handlers,
resources: b.resources,
}
}
}
/// Application builder
pub struct ApplicationBuilder<S=()> {
parts: Option<ApplicationBuilderParts<S>>,
}
impl<S> ApplicationBuilder<S> where S: 'static {
/// Configure resource for specific path.
///
/// ```rust
/// extern crate actix;
/// extern crate actix_web;
2017-10-16 00:10:35 +02:00
///
/// use actix::*;
2017-10-15 23:17:41 +02:00
/// use actix_web::*;
///
/// struct MyRoute;
///
/// impl Actor for MyRoute {
/// type Context = HttpContext<Self>;
/// }
///
/// impl Route for MyRoute {
/// type State = ();
///
/// fn request(req: HttpRequest,
/// payload: Payload,
/// ctx: &mut HttpContext<Self>) -> Reply<Self> {
/// Reply::reply(httpcodes::HTTPOk)
/// }
/// }
/// fn main() {
/// let app = Application::default()
/// .resource("/test", |r| {
/// r.get::<MyRoute>();
/// r.handler(Method::HEAD, |req, payload, state| {
/// httpcodes::HTTPMethodNotAllowed
/// });
/// })
/// .finish();
/// }
/// ```
pub fn resource<F, P: ToString>(&mut self, path: P, f: F) -> &mut Self
where F: FnOnce(&mut Resource<S>) + 'static
{
{
let parts = self.parts.as_mut().expect("Use after finish");
// add resource
let path = path.to_string();
if !parts.resources.contains_key(&path) {
parts.resources.insert(path.clone(), Resource::default());
}
f(parts.resources.get_mut(&path).unwrap());
}
self
}
/// Default resource is used if no matches route could be found.
pub fn default_resource<F>(&mut self, f: F) -> &mut Self
where F: FnOnce(&mut Resource<S>) + 'static
{
{
let parts = self.parts.as_mut().expect("Use after finish");
f(&mut parts.default);
}
self
}
/// This method register handler for specified path.
///
/// ```rust
/// extern crate actix_web;
/// use actix_web::*;
///
/// fn main() {
/// let app = Application::default()
/// .handler("/test", |req, payload, state| {
/// match *req.method() {
/// Method::GET => httpcodes::HTTPOk,
/// Method::POST => httpcodes::HTTPMethodNotAllowed,
/// _ => httpcodes::HTTPNotFound,
/// }
/// })
/// .finish();
/// }
/// ```
pub fn handler<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
where F: Fn(HttpRequest, Payload, &S) -> R + 'static,
R: Into<HttpResponse> + 'static,
P: ToString,
{
self.parts.as_mut().expect("Use after finish")
.handlers.insert(path.to_string(), Box::new(FnHandler::new(handler)));
self
}
/// Add path handler
pub fn route_handler<H, P>(&mut self, path: P, h: H) -> &mut Self
where H: RouteHandler<S> + 'static, P: ToString
{
{
// add resource
let parts = self.parts.as_mut().expect("Use after finish");
let path = path.to_string();
if parts.handlers.contains_key(&path) {
panic!("Handler already registered: {:?}", path);
}
parts.handlers.insert(path, Box::new(h));
}
self
}
/// Construct application
pub fn finish(&mut self) -> Application<S> {
self.parts.take().expect("Use after finish").into()
}
}
2017-10-07 06:48:14 +02:00
pub(crate)
struct InnerApplication<S> {
state: Rc<S>,
2017-10-08 23:56:51 +02:00
default: Resource<S>,
2017-10-10 08:07:32 +02:00
handlers: HashMap<String, Box<RouteHandler<S>>>,
2017-10-08 23:56:51 +02:00
router: Router<Resource<S>>,
2017-10-07 06:48:14 +02:00
}
2017-10-08 23:56:51 +02:00
impl<S: 'static> Handler for InnerApplication<S> {
2017-10-07 06:48:14 +02:00
2017-10-09 05:16:48 +02:00
fn handle(&self, req: HttpRequest, payload: Payload) -> Task {
2017-10-07 06:48:14 +02:00
if let Ok(h) = self.router.recognize(req.path()) {
2017-10-16 19:20:16 +02:00
h.handler.handle(
req.with_match_info(h.params), payload, Rc::clone(&self.state))
2017-10-07 06:48:14 +02:00
} else {
2017-10-10 08:07:32 +02:00
for (prefix, handler) in &self.handlers {
if req.path().starts_with(prefix) {
return handler.handle(req, payload, Rc::clone(&self.state))
}
}
2017-10-07 06:48:14 +02:00
self.default.handle(req, payload, Rc::clone(&self.state))
}
}
}