dotenv-mbt is a utility library for loading environment variables from .env
files in MoonBit applications. Inspired by Rust's dotenvy crate, it provides a simple and efficient way to manage configuration through environment files.
🚀 Key Features
- 🔄 Environment Loading – Loads variables from
.env
files into your application - 🔍 Variable Substitution – Supports variable substitution in env files
- 🛠️ Easy to Use – Simple API for quick integration
- 🔒 Safe – Non-modifying API that doesn't affect the global environment
- 🌟 MoonBit Native – Designed specifically for the MoonBit language
moon add ShellWen/dotenv
dotenv-mbt provides a simple way to load environment variables from files into your MoonBit applications.
An environment file, or env file, is a plain text file consisting of key-value pairs:
.env
HOST=localhost
PORT=3000
DATABASE_URL=mysql://user:pass@localhost/dbname
Common names for env files are .env
, .env.dev
, .env.prod
, but any name can be used. The default path for this library is .env
.
The simplest way to use dotenv-mbt
is with the EnvLoader
:
fn main {
let env_map = @dotenv.EnvLoader::new?().unwrap().load?().unwrap()
let host = env_map.get("HOST").unwrap()
println("HOST=\{host}")
}
dotenv-mbt offers several ways to configure the environment loader:
// Load from a specific file path
let loader1 = @dotenv.EnvLoader::from_path?("./.env.dev").unwrap()
// Default loader (equivalent to with_path("./.env"))
let loader2 = @dotenv.EnvLoader::new?().unwrap()
After loading the environment, you can access variables using the get
method:
let env_map = @dotenv.EnvLoader::new?().unwrap().load?().unwrap()
// Get a variable (returns Option)
let host = env_map.get("HOST").unwrap()
// Get with default value if not found
let port = match env_map.get("PORT") {
Some(p) => p
None => "8080"
}
// Check if a variable exists
if env_map.get("DATABASE_URL").is_some() {
// Do something with database URL
}
dotenv-mbt supports variable substitution in env files:
.env
HOST=localhost
URL=http://${HOST}:3000
When loaded, URL
will contain http://localhost:3000
.
dotenv-mbt uses MoonBit's Result
type for error handling:
match @dotenv.EnvLoader::new?().unwrap().load?() {
Ok(env) =>
// Use environment variables
println("Loaded successfully")
Err(e) =>
// Handle error
println("Error loading .env file")
}
fn main {
// Load environment from .env file
let env = @dotenv.EnvLoader::new?().unwrap().load?().unwrap()
// Access variables
let host = env.get("HOST").unwrap()
let port = match env.get("PORT") {
Some(p) => p
None => "8080" // Default to 8080 if not set
}
// Use variables in your application
println("Server running at \{host}:\{port}")
// Check if a variable exists
if env.get("DEBUG").is_some() {
println("Debug mode enabled")
}
}
dotenv-mbt
- Core library for loading and parsing environment files.
This project is inspired by dotenvy, a well-maintained fork of the dotenv crate for Rust. While the API and implementation details are adapted for MoonBit, we follow similar principles and parsing rules.
The README of this project is inspired by NyaSearch.
This project is licensed under the Apache-2.0 License. See LICENSE for details.
- GitHub Issues: Report an issue
👋 If you like this project, give it a ⭐! Happy coding! 🚀