1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-11 04:32:28 +00:00
actix-web/src/router.rs

145 lines
3.8 KiB
Rust
Raw Normal View History

2017-10-07 04:48:14 +00:00
use std::rc::Rc;
use std::string::ToString;
use std::collections::HashMap;
use route_recognizer::{Router as Recognizer};
use task::Task;
2017-10-09 03:16:48 +00:00
use payload::Payload;
use route::RouteHandler;
2017-10-08 21:56:51 +00:00
use resource::Resource;
use application::Application;
2017-10-07 04:48:14 +00:00
use httpcodes::HTTPNotFound;
use httprequest::HttpRequest;
2017-10-07 04:48:14 +00:00
2017-10-08 21:56:51 +00:00
pub(crate) trait Handler: 'static {
2017-10-09 03:16:48 +00:00
fn handle(&self, req: HttpRequest, payload: Payload) -> Task;
2017-10-07 04:48:14 +00:00
}
2017-10-08 06:59:57 +00:00
/// Request routing map
///
/// Route supports glob patterns: * for a single wildcard segment and :param
/// for matching storing that segment of the request url in the Params object,
/// which is stored in the request.
///
/// For instance, to route Get requests on any route matching /users/:userid/:friend and
/// store userid and friend in the exposed Params object:
///
/// ```rust,ignore
/// let mut router = RoutingMap::default();
///
/// router.add_resource("/users/:userid/:friendid").get::<MyRoute>();
/// ```
2017-10-07 04:48:14 +00:00
pub struct RoutingMap {
2017-10-08 21:56:51 +00:00
apps: HashMap<String, Box<Handler>>,
resources: HashMap<String, Resource>,
2017-10-07 04:48:14 +00:00
}
impl Default for RoutingMap {
fn default() -> Self {
RoutingMap {
apps: HashMap::new(),
resources: HashMap::new()
}
}
}
impl RoutingMap {
2017-10-08 21:56:51 +00:00
/// Add `Application` object with specific prefix.
2017-10-08 06:59:57 +00:00
/// Application prefixes all registered resources with specified prefix.
///
/// ```rust,ignore
///
/// struct MyRoute;
///
/// fn main() {
2017-10-08 21:56:51 +00:00
/// let mut app = Application::default();
2017-10-08 06:59:57 +00:00
/// app.add("/test")
/// .get::<MyRoute>()
/// .post::<MyRoute>();
///
/// let mut routes = RoutingMap::default();
/// routes.add("/pre", app);
/// }
/// ```
/// In this example, `MyRoute` route is available as `http://.../pre/test` url.
2017-10-08 21:56:51 +00:00
pub fn add<P, S: 'static>(&mut self, prefix: P, app: Application<S>)
2017-10-07 04:48:14 +00:00
where P: ToString
{
2017-10-08 06:59:57 +00:00
let prefix = prefix.to_string();
2017-10-07 04:48:14 +00:00
// we can not override registered resource
2017-10-08 06:59:57 +00:00
if self.apps.contains_key(&prefix) {
panic!("Resource is registered: {}", prefix);
2017-10-07 04:48:14 +00:00
}
// add application
2017-10-08 06:59:57 +00:00
self.apps.insert(prefix.clone(), app.prepare(prefix));
2017-10-07 04:48:14 +00:00
}
2017-10-08 21:56:51 +00:00
/// This method creates `Resource` for specified path
2017-10-08 06:59:57 +00:00
/// or returns mutable reference to resource object.
///
/// ```rust,ignore
///
/// struct MyRoute;
///
/// fn main() {
/// let mut routes = RoutingMap::default();
///
/// routes.add_resource("/test")
/// .post::<MyRoute>();
/// }
/// ```
/// In this example, `MyRoute` route is available as `http://.../test` url.
2017-10-08 21:56:51 +00:00
pub fn add_resource<P>(&mut self, path: P) -> &mut Resource
2017-10-07 04:48:14 +00:00
where P: ToString
{
let path = path.to_string();
// add resource
if !self.resources.contains_key(&path) {
2017-10-08 21:56:51 +00:00
self.resources.insert(path.clone(), Resource::default());
2017-10-07 04:48:14 +00:00
}
self.resources.get_mut(&path).unwrap()
}
pub(crate) fn into_router(self) -> Router {
let mut router = Recognizer::new();
for (path, resource) in self.resources {
router.add(path.as_str(), resource);
}
Router {
apps: self.apps,
resources: router,
}
}
}
pub(crate)
struct Router {
2017-10-08 21:56:51 +00:00
apps: HashMap<String, Box<Handler>>,
resources: Recognizer<Resource>,
2017-10-07 04:48:14 +00:00
}
impl Router {
2017-10-09 03:16:48 +00:00
pub fn call(&self, req: HttpRequest, payload: Payload) -> Task
2017-10-07 04:48:14 +00:00
{
if let Ok(h) = self.resources.recognize(req.path()) {
h.handler.handle(req.with_params(h.params), payload, Rc::new(()))
} else {
for (prefix, app) in &self.apps {
if req.path().starts_with(prefix) {
return app.handle(req, payload)
}
}
Task::reply(HTTPNotFound.response())
2017-10-07 04:48:14 +00:00
}
}
}