-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat examples: add rocket dynamic template (shuttle-static-folder) example #38
feat examples: add rocket dynamic template (shuttle-static-folder) example #38
Conversation
last change just modified a code comment which wasn't actually true. I think this is done now so ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jhawkesworth! 🥳 This looks good to me, just missing a few commented out lines and a rustfmt run (which reminds me we should set up some basic CI for this repo).
@@ -0,0 +1,42 @@ | |||
#[macro_use] | |||
extern crate rocket; | |||
// use rocket::fs::{FileServer, relative}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// use rocket::fs::{FileServer, relative}; |
rocket/dyn_template_hbs/src/main.rs
Outdated
let figment = rocket::Config::figment() | ||
.merge(("template_dir", template_dir)); | ||
let rocket = rocket::custom(figment) | ||
// .mount("/", FileServer::from(relative!("templates"))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// .mount("/", FileServer::from(relative!("templates"))) |
Oh yes, let me sort out the formatting. Thanks for speedy review again.
…-------- Original Message --------
On 19 Apr 2023, 08:34, Oddbjørn Grødem wrote:
@oddgrd approved this pull request.
Thanks ***@***.***(https://github.com/jhawkesworth)! 🥳 This looks good to me, just missing a few commented out lines and a rustfmt run (which reminds me we should set up some basic CI for this repo).
---------------------------------------------------------------
In [rocket/dyn_template_hbs/src/main.rs](#38 (comment)):
> @@ -0,0 +1,42 @@
+#[macro_use]
+extern crate rocket;
+// use rocket::fs::{FileServer, relative};
⬇️ Suggested change
-// use rocket::fs::{FileServer, relative};
---------------------------------------------------------------
In [rocket/dyn_template_hbs/src/main.rs](#38 (comment)):
> +#[shuttle_runtime::main]
+async fn rocket(#[shuttle_static_folder::StaticFolder(folder="templates")] static_folder: PathBuf) -> shuttle_rocket::ShuttleRocket {
+
+ /* The provisioned static folder template directory will not be a sub folder
+ of the location of the executable so it is necessary to merge the
+ template_dir setting into the configuration at runtime so that dynamic templates work.
+
+ Note that shuttle does not include Rocket.toml
+ so merging config is the preferred way to modify any settings
+ that would otherwise be set in Rocket.toml
+ */
+ let template_dir = static_folder.to_str().unwrap();
+ let figment = rocket::Config::figment()
+ .merge(("template_dir", template_dir));
+ let rocket = rocket::custom(figment)
+ // .mount("/", FileServer::from(relative!("templates")))
⬇️ Suggested change
- // .mount("/", FileServer::from(relative!("templates")))
---------------------------------------------------------------
In [rocket/dyn_template_hbs/src/main.rs](#38 (comment)):
> + /* The provisioned static folder template directory will not be a sub folder
+ of the location of the executable so it is necessary to merge the
+ template_dir setting into the configuration at runtime so that dynamic templates work.
+
+ Note that shuttle does not include Rocket.toml
+ so merging config is the preferred way to modify any settings
+ that would otherwise be set in Rocket.toml
+ */
+ let template_dir = static_folder.to_str().unwrap();
+ let figment = rocket::Config::figment()
+ .merge(("template_dir", template_dir));
+ let rocket = rocket::custom(figment)
+ // .mount("/", FileServer::from(relative!("templates")))
+ .mount("/", routes![index, hello])
+ .attach(Template::fairing())
+ ;
It looks like you need to run rustfmt for this project.
—
Reply to this email directly, [view it on GitHub](#38 (review)), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/ACAMUA2NF5J7TJTRWFBPW7LXB6IRBANCNFSM6AAAAAAXA2GH7A).
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
…serving static content
@oddgrd I formatted the code but left the commented out use of FileServ (with explanatory comment). Hope that's ok. If you don't like it, just go ahead and take L3-4 and L40-41 out of src/main.rs. But I ran a test just now and rocket won't serve a favicon for example from the location configured as template_dir without FileServ mount. None of the other rocket examples show how to use FileServ for serving static content, so I think it is a reasonable thing to have an example to demonstrate. |
Okay, that's fine. Thanks again! |
This PR adds 2 example projects to the existing rocket examples, one for showing how to serve static files using shuttle-static-folder and Rocket's FileServ and the other to demonstrate how to use Rocket's dynamic templates mechanism (also needs shuttle-static-folder).
Once there are suitable examples I think shuttle-hq/shuttle#763 can be closed.