1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-01 02:54:36 +01:00
actix-web/src/router.rs

98 lines
2.4 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 as Recognizer};
use task::Task;
use route::{Payload, RouteHandler};
use resource::HttpResource;
use application::HttpApplication;
use httpcodes::HTTPNotFound;
2017-10-07 08:14:13 +02:00
use httpmessage::{HttpRequest, IntoHttpResponse};
2017-10-07 06:48:14 +02:00
pub trait HttpHandler: 'static {
fn handle(&self, req: HttpRequest, payload: Option<Payload>) -> Task;
}
pub struct RoutingMap {
apps: HashMap<String, Box<HttpHandler>>,
resources: HashMap<String, HttpResource>,
}
impl Default for RoutingMap {
fn default() -> Self {
RoutingMap {
apps: HashMap::new(),
resources: HashMap::new()
}
}
}
impl RoutingMap {
pub fn add<P, S: 'static>(&mut self, path: P, app: HttpApplication<S>)
where P: ToString
{
let path = path.to_string();
// we can not override registered resource
if self.apps.contains_key(&path) {
panic!("Resource is registered: {}", path);
}
// add application
self.apps.insert(path.clone(), app.prepare(path));
}
pub fn add_resource<P>(&mut self, path: P) -> &mut HttpResource
where P: ToString
{
let path = path.to_string();
// add resource
if !self.resources.contains_key(&path) {
self.resources.insert(path.clone(), HttpResource::default());
}
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 {
apps: HashMap<String, Box<HttpHandler>>,
resources: Recognizer<HttpResource>,
}
impl Router {
pub fn call(&self, req: HttpRequest, payload: Option<Payload>) -> Task
{
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)
}
}
2017-10-08 06:48:00 +02:00
Task::reply(IntoHttpResponse::response(HTTPNotFound, req))
2017-10-07 06:48:14 +02:00
}
}
}