Fundamentals
Environment
Protect your sensitive data
Environment files in Mineral are used to store configuration variables and sensitive data that your application needs to function properly.
These files provide a convenient and secure way to manage different configurations for various environments (e.g., development, staging, production) without hardcoding the values directly into your source code.
Environment variables are mandatory for starting your application.
Mineral follows the widely-used convention of using .env files to define environment variables. These files are plain text files and typically have a key-value pair format, with each line representing a separate variable assignment.
# Mineral minimale configuration
TOKEN: Nzg1ODgxOTk1NDc2ODYwOTc5.GPW_Ur.nrzqlBbnZ-Vbq6yCXd-e5xQTTpS0APP0qzASbp
LOG_LEVEL: info
DEBUGGER: true
# Or else configurations
DB_CONNECTION: pg
DB_HOST: localhost
DB_PORT: 5432
DB_NAME: mineral_database
DB_USER: postgres
DB_PASSWORD: postgres
The data in your environment file is automatically injected into the EnvironmentService
service.
Access your environment data
Because of its paradigm and software structure, the Mineral framework allows you to access any data in your application at any time during its life cycle as long as the requested service has been initialized beforehand.
Although the EnvironmentService
service is the first to be initialized, you cannot call it until the kernel has been created.
By using environment files and the EnvironmentService class, you can securely manage and access sensitive data while keeping your codebase clean and portable.
It's important to note that environment files should never be committed to version control systems like Git. Instead, they should be kept separate and securely distributed to authorized personnel during deployment.
Let's consider the following environment.
MY_CHANNEL_ID: 989981174829039657
In a hypothetical event using the MessageCreate
event, we want to access a specific channel from our channel id stored in our environment variables
First of all, we need to import a mixin
allowing the EnvironmentService
service to be injected into our class, which will then be accessible via the environment
variable.
import 'package:mineral/framework.dart';
import 'package:mineral/core/events.dart';
class NewMessage extends MineralEvent<MessageCreateEvent> with Environment 👈 {
Future<void> handle(MessageCreateEvent event) async {
final channelId = environment.getOrFail('MY_CHANNEL_ID');👈
final channel = event.message.channel.guild.channels.getOrFail(channelId);
if (channel is TextChannel) {
final author = event.message.author;
await channel.send(content: 'Hello $author');
}
}
}
In our example, it is important to note that there are two ways of accessing our data.
environment.get('KEY')
which hypothetically retrieves our value associated with our key who can be null
if not exist.
environment.getOrFail('KEY')
which allows you to retrieve a value which must already exist, otherwise an Exception
will be thrown. The default can be modified using the message
key.