Skip to content

Commit 9b07fdb

Browse files
committed
feat(config): add EnvironmentConfig class for managing environment variables
1 parent 2b8419b commit 9b07fdb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'dart:io';
2+
3+
/// {@template environment_config}
4+
/// A utility class for accessing environment variables.
5+
///
6+
/// This class provides a centralized way to read configuration values
7+
/// from the environment, ensuring that critical settings like database
8+
/// connection strings are managed outside of the source code.
9+
/// {@endtemplate}
10+
abstract final class EnvironmentConfig {
11+
/// Retrieves the PostgreSQL database connection URI from the environment.
12+
///
13+
/// The value is read from the `DATABASE_URL` environment variable.
14+
///
15+
/// Throws a [StateError] if the `DATABASE_URL` environment variable is not
16+
/// set, as the application cannot function without it.
17+
static String get databaseUrl {
18+
final dbUrl = Platform.environment['DATABASE_URL'];
19+
if (dbUrl == null || dbUrl.isEmpty) {
20+
throw StateError(
21+
'FATAL: DATABASE_URL environment variable is not set. '
22+
'The application cannot start without a database connection.',
23+
);
24+
}
25+
return dbUrl;
26+
}
27+
28+
/// Retrieves the current environment mode (e.g., 'development', 'production').
29+
///
30+
/// The value is read from the `ENV` environment variable.
31+
/// Defaults to 'production' if the variable is not set.
32+
static String get environment => Platform.environment['ENV'] ?? 'production';
33+
}

0 commit comments

Comments
 (0)