13 ms·
I have now rewritten my code so that I include bytes directly in my src/main.rs like so: #[get("/fonts/<fname>")] fn fonts<'a> (fname: String) -> Resul
by codetrotter 9y ago
I have now rewritten my code so that I include bytes directly in my src/main.rs like so:
#[get("/fonts/<fname>")]
fn fonts<'a> (fname: String) -> Result<Content<Vec<u8>>, NotFound<String>>
{
match fname.as_ref()
{
"autobahn.woff" => Ok(Content(ContentType::WOFF,
include_bytes!("../static/fonts/Autobahn/autobahn.woff").to_vec())),
"autobahn.ttf" => Ok(Content(ContentType::TTF,
include_bytes!("../static/fonts/Autobahn/autobahn.ttf").to_vec())),
"grobe-deutschmeister.woff" => Ok(Content(ContentType::WOFF,
include_bytes!("../static/fonts/Grobe-Deutschmeister/grobe-deutschmeister.woff").to_vec())),
"grobe-deutschmeister.ttf" => Ok(Content(ContentType::TTF,
include_bytes!("../static/fonts/Grobe-Deutschmeister/grobe-deutschmeister.ttf").to_vec())),
_ => Err(NotFound(format!("No such file: /fonts/{}", fname)))
}
}
In the meantime since I first wrote my original code, the web framework I was using called Iron [1] has become unmaintained and they now urge people to pick a different framework. I chose to use Rocket because it seemed to have the features I want and the documentation seemed good enough to get started (and it was).
For templating I found Askama [2], which I am using from git master in order to be able to use it together with Rocket. https://github.com/djc/askama/issues/71 https://github.com/djc/askama/issues/71
Eventually I will generate the routes for the included bytes with build.rs again. My new code is better both short-term and for when I will create said build.rs.
Thank you masklinn for your comment about include_bytes! which made me look at my code again and rewrite it.
[1]: https://rocket.rs/ https://rocket.rs/
[2]: https://docs.rs/crate/askama/ https://docs.rs/crate/askama/