Database + Environment

Orchestrate your secrets, configuration, and database connections securely using NioFlow's integrated environment management.

01

Environment Configuration

NioFlow utilizes a built-in environment loader that automatically detects and parses .env files in your project root. This prevents hardcoding secrets like Supabase keys or database passwords in your source code or command-line history.

Library Credit & Compliance

NioFlow's environment management is powered by the excellent dotenv-java library authored by io.github.cdimascio. We use the official, unmodified library as a dependency to ensure full security and performance standards.

i

Setting up your .env file

Create a file named .env in your project root. NioFlow will automatically load these variables at startup.
.env example
1# Server Configuration
2PORT=8080
3JWT_SECRET=your_super_secret_signing_key_at_least_32_chars
4NIOFLOW_CORS_ORIGIN=http://localhost:3000
5NIOFLOW_CHAOS_ENABLED=false
6NIOFLOW_REPLAY_ENABLED=false
7NIOFLOW_WATCH=false
8
9# Postgres Configuration (Supabase)
10JDBC_URL=jdbc:postgresql://your-db-host.supabase.co:5432/postgres
11DB_USER=postgres
12DB_PASS=your_secure_database_password
13
14# MongoDB Configuration (Atlas)
15MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/nioflow
16
17# Management and Token Lifecycle (v1.4.0)
18NIOFLOW_METRICS_TOKEN=your-secure-management-token
19NIOFLOW_JWT_EXPIRATION_MS=900000
02

Built-in Persistence

NioFlow provides first-class support for both relational and document-based storage. You can initialize these directly on your application instance.

App-style Initialization
1NioFlowApp app = new NioFlowApp();
2
3// One-liner initialization from .env
4app.initPostgres();
5app.initMongo();
6
7app.listen(8080);
Accessing Connections
1// Get a Postgres Connection (HikariCP)
2try (Connection conn = Database.getPostgresConnection()) {
3 // SQL Logic
4}
5
6// Get MongoDB Client
7MongoClient mongo = Database.getMongoClient();
8MongoDatabase db = mongo.getDatabase("production");
!

Privacy & Security

Ensure that .env is added to your .gitignore file. NioFlow project generates this by default to prevent leaking credentials.

Safe Parameter Extraction (v1.4.0)

To prevent injection vulnerabilities and ensure data integrity, always use the type-safe path parameter extraction methods.

Safe Parameter Parsing
1app.get("/api/tasks/:id", ctx -> {
2 // Validates that :id is numeric; throws 400 if not.
3 long taskId = ctx.pathParamAsLong("id");
4
5 // Also available for Integer
6 int categoryId = ctx.pathParamAsInt("category");
7});

The Env API

The framework provides a unified io.github.jhanvi857.nioflow.Env class to access configuration values with ease.

Accessing configuration
1// Get String with fallback
2String origin = Env.get("NIOFLOW_CORS_ORIGIN", "http://localhost:3000");
3
4// Feature guards
5boolean chaosEnabled = Env.getAsBoolean("NIOFLOW_CHAOS_ENABLED", false);
6boolean replayEnabled = Env.getAsBoolean("NIOFLOW_REPLAY_ENABLED", false);
7boolean watchEnabled = Env.getAsBoolean("NIOFLOW_WATCH", false);
8
9// Get typed primitives
10int port = Env.getAsInt("PORT", 8080);
11boolean isDebug = Env.getAsBoolean("DEBUG_MODE", false);