Skip to content
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

wip explaining config crate #120

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions blueprint/config/README.md.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ pub struct Config {
}
```

TODO
- mention that database config is supposed to be changed by environment variables only i.e. `APP_DATABASE__URL`, `APP_DATABASE__NAME`.
- Add rationale i.e. avoid accidentally pushing database connection url to the repo.
- explain, possibly rename `app.toml` to `global.toml` alternatively it should say `server.toml` to match the `ServerConfig` struct name,
indicating its content changes only that particular struct.
- Add flow diagram that describes how config is built from different sources: app.toml -> environments/[env].toml -> APP_SERVER__NESTED_FIELD environment variables.
- Changing app.toml and environment files only changes the top level config and not inside Server. How to add and change Server config using config files?

Gerust uses [figment](https://crates.io/crates/figment) to populate the `Config` struct from environment variables and TOML files such that:

* the `ServerConfig` that contains interface and port to bind to, is populated from the `APP_SERVER__IP` and `APP_SERVER__PORT` environment variables.
{%- unless template_type == "minimal" %}
* the `DatabaseConfig` that contains the connection URL for the database is populated from the `APP_DATABASE__URL` environment variable.
{%- endunless %}
* any application-specific configuration values are read from the `app.toml` and environment-specific configuration files such that settings in the environment-specific configuration files override values for the same setting in `app.toml`.


2 changes: 2 additions & 0 deletions blueprint/config/environments/production.toml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# add config settings for the production environment here…
[server]
match_lifespan = 300
2 changes: 1 addition & 1 deletion blueprint/config/src/lib.rs.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ where
};

let config: T = Figment::new()
.merge(Serialized::defaults(ServerConfig::default()).key("server"))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test for this

.merge(Toml::file("config/app.toml"))
.merge(Toml::file(format!(
"config/environments/{}",
env_config_file
)))
.merge(Serialized::defaults(ServerConfig::default()).key("server"))
.merge(Env::prefixed("APP_").split("__"))
.extract()
.context("Could not read configuration!")?;
Expand Down