Closed bryanburgers closed 5 years ago
Another option would be to check for the existence of $TOWER_WEB_TEMPLATE_DIR
or $CARGO_MANIFEST_DIR
, and of neither exists, panic!
with a useful message.
https://github.com/carllerche/tower-web/issues/127#issuecomment-429175374 is asking a similar question.
@bryanburgers Thanks for this. I commented on the PR.
I think you picked the right defaults. My only thought is, if TOWER_WEB_TEMPLATE_DIR
is provided but the dir does not exist, that should result in a panic.
CLosed by #139
When using handlebar templates, it's clear from the documentation where to put the templates during development.
However, it is unclear how to run a binary that uses templates in production (not using
cargo
).Currently, the templates are found at
${CARGO_MANIFEST_DIR}/templates
, which works well when usingcargo run
because Cargo sets$CARGO_MANIFEST_DIR
. It doesn't work well in production because people usually don't have this Cargo-specific variable set.We should figure where to find the templates when a binary is run outside of a cargo environment. I suggest we have a sequence of fallbacks, like
${TOWER_WEB_TEMPLATE_DIR}
or other (suggested by @carllerche)${CARGO_MANIFEST_DIR}/templates
templates
folder under the current working directoryThe first one found would be the one that tower-web uses.
Also note that we probably want to default to something sensible, but not try to give too much flexibility. An application author can still use
Handlebars::new_with_registry
to get exactly the functionality they want.1.
${TOWER_WEB_TEMPLATE_DIR}
What's the best name for this variable?
Would we want this variable to point to the templates directory itself? Or a folder in which
templates
can be found? The first makes a lot of sense on its own, but the second would be more consistent with${CARGO_MANIFEST_DIR}/templates
and${PWD}/templates
.E.g., if I have my index template at
/opt/awesome-web-app/templates/index.hbs
, should I setTOWER_WEB_TEMPLATE_DIR=/opt/awesome-web-app
orTOWER_WEB_TEMPLATE_DIR=/opt/awesome-web-app/templates
?2.
${CARGO_MANIFEST_DIR}/templates
This is already what we're using. Nothing needs to change here.
3.
templates
under current working directoryI suggest this because it was one of the first things I tried when trying to get this to work. Maybe other people might also make this assumption?
Also, when running in production, it's usually pretty easy to make systemd/upstart/sysv run an application in a given working directory, so it would be pretty easy for the person deploying the application to be able to get this right.