carllerche / tower-web

A fast, boilerplate free, web framework for Rust
MIT License
980 stars 51 forks source link

templates location #136

Closed bryanburgers closed 5 years ago

bryanburgers commented 5 years ago

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 using cargo 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

  1. ${TOWER_WEB_TEMPLATE_DIR} or other (suggested by @carllerche)
  2. ${CARGO_MANIFEST_DIR}/templates
  3. A templates folder under the current working directory

The 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 set

2. ${CARGO_MANIFEST_DIR}/templates

This is already what we're using. Nothing needs to change here.

3. templates under current working directory

I 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.

bryanburgers commented 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.

phrohdoh commented 5 years ago

https://github.com/carllerche/tower-web/issues/127#issuecomment-429175374 is asking a similar question.

carllerche commented 5 years ago

@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.

carllerche commented 5 years ago

CLosed by #139