Overview
Showdown Trivia is configured through environment variables defined in a.env file or set in your deployment environment. The application validates all required configuration at startup and will fail with specific error messages if any required values are missing or invalid.
Required Environment Variables
All of the following environment variables must be set for the application to start:DB_URL
Type: StringRequired: Yes Description: MongoDB connection string including authentication credentials and database name. Format:
PORT
Type: IntegerRequired: Yes
Default: None (must be explicitly set) Description: The port number on which the web server will listen for HTTP requests. Examples:
The application will exit with error
ErrInvalidPort if this value is not a valid integer.8080- Standard alternative HTTP port (default in Docker)3000- Common development port80- Standard HTTP port (requires root/admin privileges)
LOG_LEVEL
Type: String (enum)Required: Yes Description: Controls the verbosity of application logging. Determines which log messages are output based on their severity level. Valid values:
debug- Most verbose; includes all log levelsinfo- Informational messages and abovewarn- Warnings and errors onlyerror- Error messages only
- Development (
ENV=dev): Uses text format with source file locations, forcesdebuglevel - Production (
ENV=prod): Uses JSON format with source locations, respects the configured level
ENV
Type: String (enum)Required: Yes Description: Specifies the runtime environment, which affects logging format and behavior. Valid values:
dev- Development modeprod- Production mode
| Aspect | dev | prod |
|---|---|---|
| Log Format | Text (human-readable) | JSON (machine-parseable) |
| Log Level Override | Always debug | Uses LOG_LEVEL setting |
| Source Info | Included | Included |
Complete Configuration Examples
Development (.env)
Production (.env)
Docker Compose (compose.yaml)
Configuration Loading
The application loads configuration in the following order:Environment variables read
The application reads all four required environment variables using
os.Getenv().Validation
Each variable is validated:
ENV: Must be “dev” or “prod”DB_URL: Must not be emptyLOG_LEVEL: Must be one of the valid log levelsPORT: Must be a valid integer
Configuration object created
If all validations pass, a
Config struct is created with the parsed values.Loading Environment Files
With Makefile
The Makefile automatically loads variables from.env using the load_env.sh script:
Manually
Source the environment file before running:With Docker
Docker Compose automatically reads environment variables from thecompose.yaml file.
Validation Errors
The application performs strict validation and will exit with specific errors:| Error | Cause | Solution |
|---|---|---|
ErrInvalidDbUrl | DB_URL is empty | Set valid MongoDB connection string |
ErrInvalidPort | PORT is not a number | Set PORT to a valid integer |
ErrLogLevel | LOG_LEVEL is empty | Set LOG_LEVEL to one of the valid values |
ErrInvalidLevel | LOG_LEVEL value is invalid | Use: debug, info, warn, or error |
ErrInvliadEnv | ENV is missing or invalid | Set ENV to “dev” or “prod” |
Configuration in Code
The configuration is managed by theconfig package located in:
MongoDB Configuration Details
Connection String Parameters
The MongoDB connection string supports various parameters:authSource- Database for authentication (usually “admin”)retryWrites=true- Enable retryable writesw=majority- Write concern levelmaxPoolSize- Maximum connection pool sizeminPoolSize- Minimum connection pool size
Docker Compose MongoDB Settings
When using the providedcompose.yaml, MongoDB is configured with:
The hostname
mongo refers to the Docker Compose service name and is resolved by Docker’s internal DNS.Security Best Practices
Recommendations
- Use strong passwords for MongoDB authentication
- Rotate credentials regularly in production
- Use secrets management systems (e.g., HashiCorp Vault, AWS Secrets Manager)
- Set ENV=prod in production to use JSON logging
- Use LOG_LEVEL=info or warn in production to reduce log volume
- Restrict PORT access with firewalls and security groups
- Enable MongoDB authentication even in development
- Use connection pooling parameters for production MongoDB connections
Environment-Specific Tips
Development
- Use
debuglevel for detailed logging - Point to local MongoDB instance
- Use text-formatted logs for easier reading
Staging
- Use
infolevel to see important events - Use production-like settings
- Test with JSON logs
Production
- Use
warnorerrorto minimize log volume - Use managed MongoDB service (Atlas, etc.)
- Enable retryable writes and appropriate write concern
- Use JSON logs for log aggregation tools
Troubleshooting
Application won’t start
-
Check all four required variables are set:
-
Verify
.envfile exists and is being loaded - Check for validation error messages in the output
Environment variables not loading
-
Ensure
load_env.shhas correct permissions: -
Source the file manually:
-
Verify
.envfile format (no quotes around values needed)
MongoDB connection fails
-
Verify MongoDB is running:
- Check authentication credentials
- Ensure the database specified in the URL exists
- Verify network connectivity to the MongoDB host
Next Steps
Local Setup
Set up your local development environment
Docker Deployment
Deploy with Docker Compose