4 ms·
I turned my private website (not the one listed on my HN profile) into a single binary doing something similar to what OP is doing, except I didn't know about i
by codetrotter 9y ago
I turned my private website (not the one listed on my HN profile) into a single binary doing something similar to what OP is doing, except I didn't know about include_str!, only include!, so in my build.rs I first read in my static files and then I write them out as byte array declarations into intermediate Rust files which I then include! in my src/main.rs.
Thanks to this short blog post by OP I learned something that will let me simplify some of my code.
However the thing that I am doing is not completely useless, because I have been able to embed fonts, favicon and images as well with the method that I am using.
The code I have written for this is only the beginning but if anyone wants to look at my code in order to see how I did it here you go:
https://gist.github.com/ctsrc/4c4cc05254d12bbc8937a0ea385fcdad https://gist.github.com/ctsrc/4c4cc05254d12bbc8937a0ea385fcd...
It's far from great but it's a start. Let me know how you would do it better :)
Speaking of Rust and websites, in Python I have fallen in love with the pyTenjin templating engine. http://www.kuwata-lab.com/tenjin/pytenjin-users-guide.html http://www.kuwata-lab.com/tenjin/pytenjin-users-guide.html. Does anyone know of a templating engine like pyTenjin but for Rust?
- masklinn 9y ago> However the thing that I am doing is not completely useless, because I have been able to embed fonts, favicon and images as well with the method that I am using. FWIW there's also an include_bytes! macro to embed binary data right into the executable without going through a separate rustification step.
- deleted 9y ago[deleted]
- codetrotter 9y agoI actually already use the include_bytes! macro within the build.rs. Looking at the source I see that I create arrays of arrays of bytes, not just arrays of bytes as opposed to what I originally said. It was a few months since I wrote that code so I hope I would be forgiven for not remembering such details ;) My eventual plan that I remember now was to make it so that I could reference static files (CSS, JS, icons and images, fonts) in my static HTML files and HTML templates, and build.rs would automatically include the bytes of those files.
- codetrotter 9y agoI 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/