Configuration and Environment Variables
Relevant source files
The following files were used as context for generating this wiki page:
The Smart Absen Face Recognition system utilizes a combination of a central config.py file and environment variables (.env file) to manage application settings. This approach separates configuration from the codebase, enhancing security and portability. Sensitive information, such as database credentials and secret keys, are stored in a local .env file, which is excluded from version control, while non-sensitive, default configurations may reside in config.py.
This system was introduced in version 2.0.0 as a major security and architectural update, moving away from hardcoding all values in config.py. The application now uses the python-dotenv library to load these variables at runtime.
Sources: README.md:16-17, SECURITY.md:4-6, CHANGELOG.md:14-15, CONTRIBUTING.md:37-38
Configuration Setup
Setting up the local configuration is a required step for development. The process involves creating a .env file from the provided template and populating it with the correct values for the local environment.
The following diagram illustrates the setup workflow for a new developer.
Step-by-Step Guide
- Copy the Template: In the project's root directory, copy the
.env.examplefile to a new file named.env.cp .env.example .env - Edit the
.envFile: Open the newly created.envfile and modify the variables with your local setup details, such as database credentials and a unique secret key.nano .env # Or use your preferred editor - Generate a Secret Key: For production or secure environments, generate a strong, random secret key.
Copy the output and paste it as the value forpython3 -c "import secrets; print(secrets.token_hex(32))"SECRET_KEYin your.envfile.
Sources: README.md:23-24, SECURITY.md:52-61, INSTALLATION.md:147-151
Configuration Loading Flow
The application loads configuration from multiple sources in a specific order. The config.py file is responsible for reading the .env file and making the variables available to the main Flask application (app.py).
This diagram shows how configuration values flow from the .env file to the application at runtime.
Sources: SECURITY.md:4-6, CHANGELOG.md:30, CLEANUP_REPORT.md:52
Environment Variables
All environment-specific settings and secrets are managed through the .env file. The table below details the variables defined in .env.example.
| Variable | Description | Example | Source(s) |
|---|---|---|---|
DB_HOST | The hostname or IP address of the MySQL database server. | localhost | README.md:83 |
DB_USER | The username for the MySQL database. | root | README.md:84 |
DB_PASSWORD | The password for the specified database user. | yourpassword | README.md:85 |
DB_NAME | The name of the database to use for the application. | smart_absen | README.md:86 |
SECRET_KEY | A secret key used by Flask for session management and security. | your-secret-key | README.md:89 |
FLASK_ENV | The Flask environment mode. | production | README.md:90 |
PORT | The port on which the Flask application will run. | 5001 | README.md:91 |
DEBUG | A boolean to enable or disable Flask's debug mode. | True | CONTRIBUTING.md:44 |
FACE_CONFIDENCE | The confidence threshold for face recognition. | 0.6 | README.md:94 |
MAX_FACES | The maximum number of faces to detect in a single frame. | 1 | README.md:95 |
Sources: README.md:82-95, CONTRIBUTING.md:41-44, SECURITY.md:59-62
Example .env File
This is an example of what a developer's local .env file might look like. This file must not be committed to version control.
# Database
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=yourpassword
DB_NAME=smart_absen
# App
SECRET_KEY=your-secret-key
FLASK_ENV=production
PORT=5001
# ML Config
FACE_CONFIDENCE=0.6
MAX_FACES=1
Sources: README.md:82-95
Hardcoded Configuration
Before the v2.0.0 refactor, all configuration was hardcoded in config.py. While most sensitive data has been moved to .env, some non-sensitive or default values may still reside in config.py. The INSTALLATION.md file provides insight into the previous structure of this file.
The following variables were previously defined directly in config.py:
| Variable | Description | Default Value |
|---|---|---|
DB_HOST | Host for the MySQL database. | localhost |
DB_USER | Username for the database. | root |
DB_PASSWORD | Password for the database. | your_password |
DB_NAME | Name of the database. | absensi_karyawan_db |
SECRET_KEY | Flask secret key. | your-secret-key-here |
DEBUG | Flask debug mode. | True |
HOST | Host for the application to listen on. | 0.0.0.0 |
PORT | Port for the application. | 5001 |
Sources: INSTALLATION.md:135-145
Security and Best Practices
The primary motivation for using environment variables is to enhance security by decoupling secrets from the source code.
- Exclusion from Git: The
.envfile is listed in.gitignoreto prevent it from being accidentally committed to the repository. This ensures that sensitive credentials are not exposed. - Environment Portability: Each environment (development, staging, production) can have its own
.envfile with different settings (e.g., different database hosts) without requiring any code changes. - Centralized Management:
config.pyacts as a single source of truth for configuration within the application, loading values from the environment and providing them to other modules likeapp.py.
Sources: SECURITY.md:4-12, CLEANUP_REPORT.md:40, 56, CONTRIBUTING.md:40