Skip to main content

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 uses godotenv 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

1

Create .env File

Create a .env file in the root directory of your project:
Never commit your .env file to version control. Add it to .gitignore to prevent accidental commits.
2

Configure Database Variables

Add your PostgreSQL database configuration:
.env
string
required
PostgreSQL server hostname or IP address
string
required
PostgreSQL server port (default: 5432)
string
required
Database user with access to the CompanyFlow database
string
required
Password for the database user
string
required
Name of the database (default: companyflow)
string
required
SSL mode for database connection. Options: disable, require, verify-ca, verify-full
3

Configure Server Variables

Add server configuration:
.env
string
default:"8080"
Port number for the HTTP server
string
default:"http://localhost:3000"
Allowed origin for CORS requests. Set to your frontend application URL.
4

Configure Authentication

Add JWT secret for authentication:
.env
string
required
Secret key for signing JWT tokens. Use a strong, random string.
Generate a secure JWT secret using a cryptographically secure random generator. Never use simple strings like “secret” or “password”.
Generate a secure JWT secret:

Complete Configuration Example

Here’s a complete .env file example:

Environment Variable Validation

CompanyFlow validates required environment variables at startup. From config/config.go:50:
If any required variable is missing, the application will fail to start with a clear error message.

CORS Configuration

The CORS middleware is configured in main.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 in main.go:44. Example:

Testing Environment Variables

For testing, you can create a separate .env.test file:
.env.test
Load it before running tests:

Production Deployment

Never use .env files in production. Use your platform’s environment variable configuration instead.

Platform-Specific Configuration

Troubleshooting

Missing Environment Variable Error

If you see this error:
Ensure your .env file exists and contains all required variables.

.env File Not Loading

If your .env file isn’t being loaded:
  1. Verify the file is named exactly .env (not .env.txt)
  2. Ensure it’s in the root directory where you run go run main.go
  3. Check file permissions: chmod 644 .env

CORS Errors

If you see CORS errors in the browser:
  1. Verify CORS_ORIGIN matches your frontend URL exactly (including protocol)
  2. Ensure the frontend is making requests to the correct API URL
  3. Check that preflight OPTIONS requests are being handled

Next Steps

After configuring environment variables:
  1. Set up your database
  2. Run database migrations
  3. Test your configuration