Dont split static path names.

A static filed might be called somename.suffix, but there is no
need to assume that, and absolutley no need to copy the strings
separatley and format them together again when we can just
borrow the relevant substring directly from the request.
This commit is contained in:
Rasmus Kaj 2018-02-10 01:00:48 +01:00
parent 28032e4026
commit 8dcc6359e2
2 changed files with 22 additions and 5 deletions

View File

@ -118,7 +118,7 @@ pub fn run(args: &ArgMatches) -> Result<(), Error> {
let mut server = Nickel::new(); let mut server = Nickel::new();
server.utilize(RequestLoggerMiddleware); server.utilize(RequestLoggerMiddleware);
wrap3!(server.get "/static/{}\\.{}", static_file: file, ext); wrap3!(server.get "/static/",.. static_file);
server.utilize(MemcacheMiddleware::new(vec![ server.utilize(MemcacheMiddleware::new(vec![
("tcp://127.0.0.1:11211".into(), 1), ("tcp://127.0.0.1:11211".into(), 1),
])); ]));
@ -405,13 +405,12 @@ fn place_all<'mw>(
} }
fn static_file<'mw>( fn static_file<'mw>(
_req: &mut Request, _req: &Request,
mut res: Response<'mw>, mut res: Response<'mw>,
name: String, path: &str,
ext: String,
) -> MiddlewareResult<'mw> { ) -> MiddlewareResult<'mw> {
use templates::statics::StaticFile; use templates::statics::StaticFile;
if let Some(s) = StaticFile::get(&format!("{}.{}", name, ext)) { if let Some(s) = StaticFile::get(path) {
res.set((ContentType(s.mime()), far_expires())); res.set((ContentType(s.mime()), far_expires()));
return res.send(s.content); return res.send(s.content);
} }

View File

@ -29,6 +29,24 @@ macro_rules! wrap3 {
stringify!($handler)); stringify!($handler));
$server.$method(matcher, wrapped); $server.$method(matcher, wrapped);
}}; }};
($server:ident.$method:ident $url:expr,.. $handler:ident) => {{
#[allow(unused_parens)]
fn wrapped<'mw>(req: &mut Request,
res: Response<'mw>)
-> MiddlewareResult<'mw> {
if let &Some(path) = &req.path_without_query() {
$handler(req, res, &path[$url.len()..])
} else {
res.not_found("Path missing")
}
}
let matcher = format!("{}**", $url);
info!("Route {} {} to {}",
stringify!($method),
matcher,
stringify!($handler));
$server.$method(matcher, wrapped);
}};
($server:ident.$method:ident $url:expr, $handler:ident) => { ($server:ident.$method:ident $url:expr, $handler:ident) => {
info!("Route {} {} to {}", info!("Route {} {} to {}",
stringify!($method), stringify!($method),