album/src/photosdirmiddleware.rs
2018-07-15 14:34:55 +02:00

42 lines
993 B
Rust

use nickel::{Continue, Middleware, MiddlewareResult, Request, Response};
use photosdir::PhotosDir;
use plugin::Extensible;
use std::path::PathBuf;
use typemap::Key;
pub struct PhotosDirMiddleware {
dir: PathBuf,
}
impl PhotosDirMiddleware {
pub fn new(dir: PathBuf) -> Self {
PhotosDirMiddleware { dir }
}
}
impl Key for PhotosDirMiddleware {
type Value = PhotosDir;
}
impl<D> Middleware<D> for PhotosDirMiddleware {
fn invoke<'mw, 'conn>(
&self,
req: &mut Request<'mw, 'conn, D>,
res: Response<'mw, D>,
) -> MiddlewareResult<'mw, D> {
req.extensions_mut()
.insert::<PhotosDirMiddleware>(PhotosDir::new(self.dir.clone()));
Ok(Continue(res))
}
}
pub trait PhotosDirRequestExtensions {
fn photos(&self) -> &PhotosDir;
}
impl<'a, 'b, D> PhotosDirRequestExtensions for Request<'a, 'b, D> {
fn photos(&self) -> &PhotosDir {
self.extensions().get::<PhotosDirMiddleware>().unwrap()
}
}