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-09 05:16:48 +02:00
|
|
|
use route::RouteHandler;
|
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;
|
2017-10-07 06:48:14 +02:00
|
|
|
use httpmessage::HttpRequest;
|
|
|
|
|
|
|
|
|
|
|
|
/// 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>,
|
|
|
|
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();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(
|
|
|
|
InnerApplication {
|
|
|
|
state: Rc::new(self.state),
|
|
|
|
default: self.default,
|
|
|
|
router: router }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl Default for Application<()> {
|
2017-10-07 08:14:13 +02:00
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
/// Create default `Application` with no state
|
|
|
|
fn default() -> Self {
|
|
|
|
Application {
|
2017-10-07 06:48:14 +02:00
|
|
|
state: (),
|
2017-10-08 23:56:51 +02:00
|
|
|
default: Resource::default(),
|
2017-10-07 06:48:14 +02:00
|
|
|
resources: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-07 06:48:14 +02:00
|
|
|
resources: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Add resource by path.
|
2017-10-08 23:56:51 +02:00
|
|
|
pub fn add<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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate)
|
|
|
|
struct InnerApplication<S> {
|
|
|
|
state: Rc<S>,
|
2017-10-08 23:56:51 +02:00
|
|
|
default: Resource<S>,
|
|
|
|
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()) {
|
|
|
|
h.handler.handle(req.with_params(h.params), payload, Rc::clone(&self.state))
|
|
|
|
} else {
|
|
|
|
self.default.handle(req, payload, Rc::clone(&self.state))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|