2017-10-07 06:48:14 +02:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use task::Task;
|
2017-10-17 04:21:24 +02:00
|
|
|
use payload::Payload;
|
2017-10-15 23:17:41 +02:00
|
|
|
use route::{RouteHandler, FnHandler};
|
2017-10-08 23:56:51 +02:00
|
|
|
use resource::Resource;
|
2017-10-17 04:21:24 +02:00
|
|
|
use recognizer::{RouteRecognizer, check_pattern};
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-10-15 23:17:41 +02:00
|
|
|
use httpresponse::HttpResponse;
|
2017-11-04 20:33:14 +01:00
|
|
|
use channel::HttpHandler;
|
2017-11-25 07:15:52 +01:00
|
|
|
use pipeline::Pipeline;
|
2017-11-10 07:08:54 +01:00
|
|
|
use middlewares::Middleware;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
/// Application
|
2017-10-22 03:54:24 +02:00
|
|
|
pub struct Application<S> {
|
|
|
|
state: Rc<S>,
|
|
|
|
prefix: String,
|
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-22 03:54:24 +02:00
|
|
|
router: RouteRecognizer<Resource<S>>,
|
2017-10-22 07:59:09 +02:00
|
|
|
middlewares: Rc<Vec<Box<Middleware>>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
impl<S: 'static> Application<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
fn run(&self, req: &mut HttpRequest, payload: Payload) -> Task {
|
2017-10-22 03:54:24 +02:00
|
|
|
if let Some((params, h)) = self.router.recognize(req.path()) {
|
|
|
|
if let Some(params) = params {
|
2017-10-22 07:59:09 +02:00
|
|
|
req.set_match_info(params);
|
2017-10-22 03:54:24 +02:00
|
|
|
}
|
2017-10-22 07:59:09 +02:00
|
|
|
h.handle(req, payload, Rc::clone(&self.state))
|
2017-10-22 03:54:24 +02:00
|
|
|
} else {
|
|
|
|
for (prefix, handler) in &self.handlers {
|
|
|
|
if req.path().starts_with(prefix) {
|
|
|
|
return handler.handle(req, payload, Rc::clone(&self.state))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.default.handle(req, payload, Rc::clone(&self.state))
|
2017-10-10 08:07:32 +02:00
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
impl<S: 'static> HttpHandler for Application<S> {
|
|
|
|
|
|
|
|
fn prefix(&self) -> &str {
|
|
|
|
&self.prefix
|
|
|
|
}
|
|
|
|
|
2017-11-25 07:15:52 +01:00
|
|
|
fn handle(&self, req: HttpRequest, payload: Payload) -> Pipeline {
|
|
|
|
Pipeline::new(req, payload, Rc::clone(&self.middlewares),
|
|
|
|
&|req: &mut HttpRequest, payload: Payload| {self.run(req, payload)})
|
2017-10-22 07:59:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 23:17:41 +02:00
|
|
|
impl Application<()> {
|
|
|
|
|
|
|
|
/// Create default `ApplicationBuilder` with no state
|
2017-11-27 02:30:35 +01:00
|
|
|
pub fn default<T: Into<String>>(prefix: T) -> ApplicationBuilder<()> {
|
2017-10-15 23:17:41 +02:00
|
|
|
ApplicationBuilder {
|
|
|
|
parts: Some(ApplicationBuilderParts {
|
|
|
|
state: (),
|
2017-11-27 02:30:35 +01:00
|
|
|
prefix: prefix.into(),
|
2017-10-29 14:05:31 +01:00
|
|
|
default: Resource::default_not_found(),
|
2017-10-15 23:17:41 +02:00
|
|
|
handlers: HashMap::new(),
|
2017-10-22 07:59:09 +02:00
|
|
|
resources: HashMap::new(),
|
|
|
|
middlewares: Vec::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-22 03:54:24 +02:00
|
|
|
/// Create application builder with specific state. State is shared with all
|
|
|
|
/// routes within same application and could be
|
|
|
|
/// accessed with `HttpContext::state()` method.
|
2017-11-27 02:30:35 +01:00
|
|
|
pub fn build<T: Into<String>>(prefix: T, state: S) -> ApplicationBuilder<S> {
|
2017-10-15 23:17:41 +02:00
|
|
|
ApplicationBuilder {
|
|
|
|
parts: Some(ApplicationBuilderParts {
|
|
|
|
state: state,
|
2017-11-27 02:30:35 +01:00
|
|
|
prefix: prefix.into(),
|
2017-10-29 14:05:31 +01:00
|
|
|
default: Resource::default_not_found(),
|
2017-10-15 23:17:41 +02:00
|
|
|
handlers: HashMap::new(),
|
2017-10-22 07:59:09 +02:00
|
|
|
resources: HashMap::new(),
|
|
|
|
middlewares: Vec::new(),
|
|
|
|
})
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 23:17:41 +02:00
|
|
|
struct ApplicationBuilderParts<S> {
|
|
|
|
state: S,
|
2017-10-22 03:54:24 +02:00
|
|
|
prefix: String,
|
2017-10-15 23:17:41 +02:00
|
|
|
default: Resource<S>,
|
|
|
|
handlers: HashMap<String, Box<RouteHandler<S>>>,
|
|
|
|
resources: HashMap<String, Resource<S>>,
|
2017-10-22 07:59:09 +02:00
|
|
|
middlewares: Vec<Box<Middleware>>,
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-11-27 02:30:35 +01:00
|
|
|
/// Structure that follows the builder pattern for building `Application` structs.
|
2017-10-15 23:17:41 +02:00
|
|
|
pub struct ApplicationBuilder<S=()> {
|
|
|
|
parts: Option<ApplicationBuilderParts<S>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> ApplicationBuilder<S> where S: 'static {
|
|
|
|
|
|
|
|
/// Configure resource for specific path.
|
|
|
|
///
|
2017-10-22 03:54:24 +02:00
|
|
|
/// Resource may have variable path also. For instance, a resource with
|
|
|
|
/// the path */a/{name}/c* would match all incoming requests with paths
|
|
|
|
/// such as */a/b/c*, */a/1/c*, and */a/etc/c*.
|
|
|
|
///
|
|
|
|
/// A variable part is specified in the form `{identifier}`, where
|
|
|
|
/// the identifier can be used later in a request handler to access the matched
|
|
|
|
/// value for that part. This is done by looking up the identifier
|
|
|
|
/// in the `Params` object returned by `Request.match_info()` method.
|
|
|
|
///
|
|
|
|
/// By default, each part matches the regular expression `[^{}/]+`.
|
|
|
|
///
|
|
|
|
/// You can also specify a custom regex in the form `{identifier:regex}`:
|
|
|
|
///
|
|
|
|
/// For instance, to route Get requests on any route matching `/users/{userid}/{friend}` and
|
|
|
|
/// store userid and friend in the exposed Params object:
|
|
|
|
///
|
2017-10-15 23:17:41 +02:00
|
|
|
/// ```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 = ();
|
|
|
|
///
|
2017-10-22 17:14:23 +02:00
|
|
|
/// fn request(req: &mut HttpRequest,
|
2017-10-15 23:17:41 +02:00
|
|
|
/// payload: Payload,
|
2017-10-22 18:13:29 +02:00
|
|
|
/// ctx: &mut HttpContext<Self>) -> RouteResult<Self> {
|
2017-10-15 23:17:41 +02:00
|
|
|
/// Reply::reply(httpcodes::HTTPOk)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// fn main() {
|
2017-10-22 03:54:24 +02:00
|
|
|
/// let app = Application::default("/")
|
2017-10-15 23:17:41 +02:00
|
|
|
/// .resource("/test", |r| {
|
|
|
|
/// r.get::<MyRoute>();
|
|
|
|
/// r.handler(Method::HEAD, |req, payload, state| {
|
2017-10-30 05:39:59 +01:00
|
|
|
/// Ok(httpcodes::HTTPMethodNotAllowed)
|
2017-10-15 23:17:41 +02:00
|
|
|
/// });
|
|
|
|
/// })
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-11-27 02:30:35 +01:00
|
|
|
pub fn resource<F, P: Into<String>>(&mut self, path: P, f: F) -> &mut Self
|
2017-10-15 23:17:41 +02:00
|
|
|
where F: FnOnce(&mut Resource<S>) + 'static
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
|
|
|
|
|
|
|
// add resource
|
2017-11-27 02:30:35 +01:00
|
|
|
let path = path.into();
|
2017-10-15 23:17:41 +02:00
|
|
|
if !parts.resources.contains_key(&path) {
|
2017-10-17 04:21:24 +02:00
|
|
|
check_pattern(&path);
|
2017-10-15 23:17:41 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:35:50 +02:00
|
|
|
/// This method register handler for specified path prefix.
|
|
|
|
/// Any path that starts with this prefix matches handler.
|
2017-10-15 23:17:41 +02:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// extern crate actix_web;
|
|
|
|
/// use actix_web::*;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-10-22 03:54:24 +02:00
|
|
|
/// let app = Application::default("/")
|
2017-10-15 23:17:41 +02:00
|
|
|
/// .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
|
2017-10-22 07:59:09 +02:00
|
|
|
where F: Fn(&mut HttpRequest, Payload, &S) -> R + 'static,
|
2017-10-15 23:17:41 +02:00
|
|
|
R: Into<HttpResponse> + 'static,
|
2017-11-27 02:30:35 +01:00
|
|
|
P: Into<String>,
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
|
|
|
self.parts.as_mut().expect("Use after finish")
|
2017-11-27 02:30:35 +01:00
|
|
|
.handlers.insert(path.into(), Box::new(FnHandler::new(handler)));
|
2017-10-15 23:17:41 +02:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add path handler
|
|
|
|
pub fn route_handler<H, P>(&mut self, path: P, h: H) -> &mut Self
|
2017-11-27 02:30:35 +01:00
|
|
|
where H: RouteHandler<S> + 'static, P: Into<String>
|
2017-10-15 23:17:41 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
// add resource
|
|
|
|
let parts = self.parts.as_mut().expect("Use after finish");
|
2017-11-27 02:30:35 +01:00
|
|
|
let path = path.into();
|
2017-10-15 23:17:41 +02:00
|
|
|
if parts.handlers.contains_key(&path) {
|
|
|
|
panic!("Handler already registered: {:?}", path);
|
|
|
|
}
|
|
|
|
parts.handlers.insert(path, Box::new(h));
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-22 07:59:09 +02:00
|
|
|
/// Construct application
|
|
|
|
pub fn middleware<T>(&mut self, mw: T) -> &mut Self
|
|
|
|
where T: Middleware + 'static
|
|
|
|
{
|
|
|
|
self.parts.as_mut().expect("Use after finish")
|
|
|
|
.middlewares.push(Box::new(mw));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2017-10-15 23:17:41 +02:00
|
|
|
/// Construct application
|
|
|
|
pub fn finish(&mut self) -> Application<S> {
|
2017-10-22 03:54:24 +02:00
|
|
|
let parts = self.parts.take().expect("Use after finish");
|
|
|
|
|
|
|
|
let mut handlers = HashMap::new();
|
|
|
|
let prefix = if parts.prefix.ends_with('/') {
|
|
|
|
parts.prefix
|
|
|
|
} else {
|
|
|
|
parts.prefix + "/"
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut routes = Vec::new();
|
|
|
|
for (path, handler) in parts.resources {
|
|
|
|
routes.push((path, handler))
|
|
|
|
}
|
|
|
|
|
|
|
|
for (path, mut handler) in parts.handlers {
|
|
|
|
let path = prefix.clone() + path.trim_left_matches('/');
|
|
|
|
handler.set_prefix(path.clone());
|
|
|
|
handlers.insert(path, handler);
|
|
|
|
}
|
|
|
|
Application {
|
|
|
|
state: Rc::new(parts.state),
|
|
|
|
prefix: prefix.clone(),
|
|
|
|
default: parts.default,
|
|
|
|
handlers: handlers,
|
2017-10-22 07:59:09 +02:00
|
|
|
router: RouteRecognizer::new(prefix, routes),
|
|
|
|
middlewares: Rc::new(parts.middlewares),
|
|
|
|
}
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<S: 'static> From<ApplicationBuilder<S>> for Application<S> {
|
|
|
|
fn from(mut builder: ApplicationBuilder<S>) -> Application<S> {
|
|
|
|
builder.finish()
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
impl<S: 'static> Iterator for ApplicationBuilder<S> {
|
|
|
|
type Item = Application<S>;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-22 03:54:24 +02:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.parts.is_some() {
|
|
|
|
Some(self.finish())
|
2017-10-07 06:48:14 +02:00
|
|
|
} else {
|
2017-10-22 03:54:24 +02:00
|
|
|
None
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|