Skip to content

Commit 0fa444e

Browse files
docs: update environment docs (#682)
1 parent 928d40e commit 0fa444e

File tree

3 files changed

+6452
-6456
lines changed

3 files changed

+6452
-6456
lines changed

docs/docs/basics/enviroments.md

Lines changed: 0 additions & 61 deletions
This file was deleted.

docs/docs/basics/environments.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
sidebar_position: 7
3+
title: 🌱 Environments
4+
---
5+
6+
# Environments 🌱
7+
8+
---
9+
10+
There are many ways that environments can be configured in a Dart Frog application. The
11+
easiest way is to use environment variables via the Dart SDK.
12+
13+
As seen in the [dependency injection docs](dependency-injection.md), middleware can be used to provide dependencies to an application.
14+
15+
Following this approach, the snippet shows how a database client can be configured with different environments.
16+
17+
```dart
18+
Handler middleware(Handler handler) {
19+
return handler
20+
.use(provider<CardsRepository>((_) {
21+
return DatabaseClient(
22+
dbUrl: Platform.environment['DB_URL'],
23+
dbUser: Platform.environment['DB_USER'],
24+
dbPassword: Platform.environment['DB_PASSWORD'],
25+
);
26+
}),
27+
);
28+
}
29+
```
30+
31+
When running the server, these environment variables can be passed along directly to Dart Frog commands:
32+
33+
Development server:
34+
35+
```bash
36+
DB_URL=... DB_USER=... DB_PASSWORD=... dart_frog server
37+
```
38+
39+
Production server:
40+
41+
```bash
42+
DB_URL=... DB_USER=... DB_PASSWORD=... dart build/server.dart
43+
```
44+
45+
These variables can also be exported in the current session:
46+
47+
```bash
48+
EXPORT DB_URL=...
49+
EXPORT DB_USER=...
50+
EXPORT DB_PASSWORD=...
51+
```
52+
53+
:::warning
54+
Accessing variables through `String.fromEnvironment` in a Dart Frog application will not work.`String.fromEnvironment` is meant to accesses variables set by the Dart
55+
compiler or runtime, which does not apply to a Dart Frog application. Instead, use
56+
`Platform.environment`.
57+
:::

0 commit comments

Comments
 (0)