Skip to content

Commit aa1e635

Browse files
committed
Added support for .env files
1 parent 76b835e commit aa1e635

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ and does not even need to listen on port 80 to do so.
7979
- The `cookie` component now supports setting an explicit expiration date for cookies.
8080
- The `cookie` component now supports setting the `SameSite` attribute of cookies, and defaults to `SameSite=Strict` for all cookies. What this means in practice is that cookies set by SQLPage will not be sent to your website if the user is coming from another website. This prevents someone from tricking your users into executing SQLPage queries on your website by sending them a malicious link.
8181
- Bugfix: setting `min` or `max` to `0` in a number field in the `form` component now works as expected.
82+
- Added support for `.env` files to set SQLPage's [environment variables](./configuration.md#environment-variables).
8283

8384
## 0.16.1 (2023-11-22)
8485

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ rand = "0.8.5"
4848
actix-multipart = "0.6.1"
4949
base64 = "0.21.5"
5050
rustls-acme = "0.7.7"
51+
dotenvy = "0.15.7"
5152

5253
[build-dependencies]
5354
awc = { version = "3", features = ["rustls"] }

configuration.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ on a [JSON](https://en.wikipedia.org/wiki/JSON) file placed in `sqlpage/sqlpage.
2424

2525
You can find an example configuration file in [`sqlpage/sqlpage.json`](./sqlpage/sqlpage.json).
2626

27+
Multiple configuration file formats are supported:
28+
you can use a [`.json5`](https://json5.org/) file, a [`.toml`](https://toml.io/) file, or a [`.yaml`](https://en.wikipedia.org/wiki/YAML#Syntax) file.
29+
2730
## Environment variables
2831

2932
All the parameters above can be set through environment variables.
@@ -36,7 +39,9 @@ The environment variable name can optionally be prefixed with `SQLPAGE_`.
3639
Additionnally, when troubleshooting, you can set the [`RUST_LOG`](https://docs.rs/env_logger/latest/env_logger/#enabling-logging)
3740
environment variable to `sqlpage=debug` to get more detailed logs and see exactly what SQLPage is doing.
3841

39-
### Example
42+
If you have a `.env` file in the current directory or in any of its parent directories, SQLPage will automatically load environment variables from it.
43+
44+
### Example `.env` file
4045

4146
```bash
4247
DATABASE_URL="sqlite:///path/to/my_database.db?mode=rwc"

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn start() -> anyhow::Result<()> {
1717
log::debug!("Starting with the following configuration: {app_config:?}");
1818
let state = AppState::init(&app_config).await?;
1919
webserver::database::migrations::apply(&state.db).await?;
20-
log::info!("Starting server on {}", app_config.listen_on);
20+
log::debug!("Starting server on {}", app_config.listen_on);
2121
let (r, _) = tokio::join!(
2222
webserver::http::run_server(&app_config, state),
2323
log_welcome_message(&app_config)
@@ -49,8 +49,18 @@ async fn log_welcome_message(config: &AppConfig) {
4949
}
5050

5151
fn init_logging() {
52-
let env = env_logger::Env::new().default_filter_or("info");
52+
let load_env = dotenvy::dotenv();
53+
54+
let env = env_logger::Env::new().default_filter_or("sqlpage=info");
5355
let mut logging = env_logger::Builder::from_env(env);
5456
logging.format_timestamp_millis();
5557
logging.init();
56-
}
58+
59+
match load_env {
60+
Ok(path) => log::info!("Loaded environment variables from {path:?}"),
61+
Err(dotenvy::Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => log::debug!(
62+
"No .env file found, using only environment variables and configuration files"
63+
),
64+
Err(e) => log::error!("Error loading .env file: {}", e),
65+
}
66+
}

0 commit comments

Comments
 (0)