Environment Variables
Overview
Section titled “Overview”SPEAR uses environment variables for configuration, allowing flexible deployment across different environments. Variables can be set via .env files, system environment, or systemd service configuration.
Required Variables
Section titled “Required Variables”SPEAR_ENCRYPTION_KEY
Section titled “SPEAR_ENCRYPTION_KEY”Required - AES encryption key for sensitive data.
SPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234| Requirement | Details |
|---|---|
| Length | Must be exactly 16, 24, or 32 characters |
| Purpose | Encrypts API keys and sensitive data in the database |
| Recommendation | Use 32 characters for AES-256 encryption |
Validation: The application validates key length on startup and will fail if invalid:
Encryption key must be 16, 24, or 32 characters long. Current length: XGenerate a secure key:
# Generate 32-character key (recommended)openssl rand -base64 24
# Or using /dev/urandomhead -c 32 /dev/urandom | base64 | head -c 32Application Settings
Section titled “Application Settings”SPEAR_PORT
Section titled “SPEAR_PORT”Application port for the HTTP server.
SPEAR_PORT=8090| Default | 8090 |
|---|---|
| Range | 1-65535 |
SPEAR_VERSION
Section titled “SPEAR_VERSION”Software version identifier (typically set during build).
SPEAR_VERSION=1.0.0OpenAI Configuration
Section titled “OpenAI Configuration”All OpenAI variables are optional and enable AI-powered features like text generation and grammar checking.
OPENAI_API_KEY
Section titled “OPENAI_API_KEY”API key for OpenAI services.
OPENAI_API_KEY=sk-...Get your key at: https://platform.openai.com/api-keys
OPENAI_MODEL
Section titled “OPENAI_MODEL”Model selection for AI features.
OPENAI_MODEL=gpt-3.5-turbo| Default | gpt-3.5-turbo |
|---|---|
| Options | gpt-4, gpt-4-turbo, gpt-3.5-turbo |
OPENAI_API_URL
Section titled “OPENAI_API_URL”Custom endpoint URL for OpenAI-compatible APIs (e.g., Azure OpenAI).
OPENAI_API_URL=https://api.openai.com/v1/chat/completions| Default | https://api.openai.com/v1/chat/completions |
|---|
OPENAI_MAX_TOKENS
Section titled “OPENAI_MAX_TOKENS”Maximum tokens for AI responses.
OPENAI_MAX_TOKENS=1000| Default | 1000 |
|---|
OPENAI_TEMPERATURE
Section titled “OPENAI_TEMPERATURE”Controls creativity/randomness of AI responses.
OPENAI_TEMPERATURE=0.7| Default | 0.7 |
|---|---|
| Range | 0.0 (focused) to 1.0 (creative) |
Development Settings
Section titled “Development Settings”These variables are used for development and testing environments.
SPEAR_ADMIN_EMAIL
Section titled “SPEAR_ADMIN_EMAIL”Admin email for seeding development data.
SPEAR_ADMIN_EMAIL=admin@example.comSPEAR_ADMIN_PASSWORD
Section titled “SPEAR_ADMIN_PASSWORD”Admin password for seeding development data.
SPEAR_ADMIN_PASSWORD=yourpasswordBuild-Time Variables
Section titled “Build-Time Variables”These variables are required when building SPEAR from source.
UPDATER_PUBLIC_KEY
Section titled “UPDATER_PUBLIC_KEY”Required for building - Public key for update signature verification.
UPDATER_PUBLIC_KEY=<base64-encoded-public-key>Extract from your private key:
go run scripts/extract-public-key.go <private_key_b64>CGO_ENABLED
Section titled “CGO_ENABLED”Set to 0 for static builds.
CGO_ENABLED=0GOOS / GOARCH
Section titled “GOOS / GOARCH”Cross-compilation targets.
GOOS=linuxGOARCH=amd64| GOOS | GOARCH Options |
|---|---|
| linux | amd64, arm64, arm |
| windows | amd64, arm64 |
| darwin | amd64, arm64 |
Configuration Methods
Section titled “Configuration Methods”Method 1: .env File
Section titled “Method 1: .env File”Create a .env file in the project root:
# Copy example and editcp .env.example .envnano .envExample .env file:
# RequiredSPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234
# ApplicationSPEAR_PORT=8090
# OpenAI (optional)OPENAI_API_KEY=sk-...OPENAI_MODEL=gpt-3.5-turboOPENAI_MAX_TOKENS=1000OPENAI_TEMPERATURE=0.7Method 2: System Environment Variables
Section titled “Method 2: System Environment Variables”Export variables in your shell profile (~/.bashrc, ~/.zshrc):
export SPEAR_ENCRYPTION_KEY="your32characterencryptionkey1234"export SPEAR_PORT=8090Method 3: Systemd Service File
Section titled “Method 3: Systemd Service File”The deployment script automatically configures environment variables in the systemd service file at /etc/systemd/system/spear.service:
[Service]Environment=SPEAR_ENCRYPTION_KEY=<your-key>Environment=SPEAR_VERSION=<version>Environment=SPEAR_PORT=<port>To modify after deployment:
# Edit the service filesudo systemctl edit spear
# Add overrides in the editor:[Service]Environment=OPENAI_API_KEY=sk-...
# Reload and restartsudo systemctl daemon-reloadsudo systemctl restart spearComplete Example
Section titled “Complete Example”Here’s a complete .env file for production:
# =============================================================================# Application Settings (Required)# =============================================================================
# AES encryption key - MUST be 16, 24, or 32 characters# Generate with: openssl rand -base64 24SPEAR_ENCRYPTION_KEY=your32characterencryptionkey1234
# Application portSPEAR_PORT=8090
# =============================================================================# OpenAI API Configuration (Optional - for AI-powered features)# =============================================================================
# OpenAI API Key# Get your key at: https://platform.openai.com/api-keysOPENAI_API_KEY=sk-...
# Model selection (optional, defaults to gpt-3.5-turbo)# Options: gpt-4, gpt-4-turbo, gpt-3.5-turboOPENAI_MODEL=gpt-3.5-turbo
# Custom endpoint URL (optional, for Azure OpenAI or compatible APIs)OPENAI_API_URL=https://api.openai.com/v1/chat/completions
# Max tokens (optional, defaults to 1000)OPENAI_MAX_TOKENS=1000
# Temperature - Lower = more focused, Higher = more creative# Range: 0.0 to 1.0 (optional, defaults to 0.7)OPENAI_TEMPERATURE=0.7Security Best Practices
Section titled “Security Best Practices”Never Commit .env Files
Section titled “Never Commit .env Files”Add to .gitignore:
.env.env.local.env.productionUse Strong Encryption Keys
Section titled “Use Strong Encryption Keys”Always use 32 characters for maximum security (AES-256):
# Generate secure keyopenssl rand -base64 24Restrict File Permissions
Section titled “Restrict File Permissions”# Restrict .env file permissionschmod 600 .env
# For systemd service overridessudo chmod 600 /etc/systemd/system/spear.service.d/*.confRotate Keys Periodically
Section titled “Rotate Keys Periodically”When rotating the encryption key:
- Export encrypted data
- Update the encryption key
- Re-encrypt sensitive data
- Restart the service
Environment-Specific Files
Section titled “Environment-Specific Files”Use separate files for different environments:
.env # Local development.env.staging # Staging environment.env.production # Production (never commit)Validation
Section titled “Validation”Verify your environment configuration:
# Check encryption key lengthecho -n "$SPEAR_ENCRYPTION_KEY" | wc -c
# Verify OpenAI connectivity (if configured)curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY"
# Check loaded environment in running servicesudo systemctl show spear --property=EnvironmentTroubleshooting
Section titled “Troubleshooting”Encryption Key Errors
Section titled “Encryption Key Errors”Error: Encryption key must be 16, 24, or 32 characters longSolution: Check key length and regenerate if necessary:
# Check current lengthecho -n "$SPEAR_ENCRYPTION_KEY" | wc -c
# Generate new 32-character keyexport SPEAR_ENCRYPTION_KEY=$(openssl rand -base64 24)OpenAI Connection Issues
Section titled “OpenAI Connection Issues”Error: Failed to connect to OpenAI APISolutions:
- Verify API key is valid
- Check network connectivity
- Verify custom URL if using
OPENAI_API_URL
Service Not Reading Environment Variables
Section titled “Service Not Reading Environment Variables”Solution: Ensure variables are in the systemd service file:
# Check current service environmentsudo systemctl show spear --property=Environment
# Reload after changessudo systemctl daemon-reloadsudo systemctl restart spear