Overview
CompanyFlow uses environment variables for configuration. The application loads variables from a.env file in development and from system environment variables in production.
Configuration Loading
The application usesgodotenv to load the .env file. From config/config.go:15:
If no
.env file is found, the application will use system environment variables. This is the recommended approach for production deployments.Required Environment Variables
Configure Database Variables
Add your PostgreSQL database configuration:
.env
PostgreSQL server hostname or IP address
PostgreSQL server port (default: 5432)
Database user with access to the CompanyFlow database
Password for the database user
Name of the database (default: companyflow)
SSL mode for database connection. Options:
disable, require, verify-ca, verify-fullConfigure Authentication
Add JWT secret for authentication:Generate a secure JWT secret:
.env
Secret key for signing JWT tokens. Use a strong, random string.
Complete Configuration Example
Here’s a complete.env file example:
Environment Variable Validation
CompanyFlow validates required environment variables at startup. Fromconfig/config.go:50:
CORS Configuration
The CORS middleware is configured inmain.go:46. It allows:
- Origin: Specified in
CORS_ORIGIN(default:http://localhost:3000) - Methods:
GET, POST, PUT, DELETE, OPTIONS - Headers:
Content-Type, Authorization - Credentials: Enabled
Multiple CORS Origins
To support multiple origins, you’ll need to modify the CORS middleware inmain.go:44. Example:
Testing Environment Variables
For testing, you can create a separate.env.test file:
.env.test
Production Deployment
Platform-Specific Configuration
Troubleshooting
Missing Environment Variable Error
If you see this error:.env file exists and contains all required variables.
.env File Not Loading
If your.env file isn’t being loaded:
- Verify the file is named exactly
.env(not.env.txt) - Ensure it’s in the root directory where you run
go run main.go - Check file permissions:
chmod 644 .env
CORS Errors
If you see CORS errors in the browser:- Verify
CORS_ORIGINmatches your frontend URL exactly (including protocol) - Ensure the frontend is making requests to the correct API URL
- Check that preflight OPTIONS requests are being handled