LiveTemplate supports configuration via environment variables following the 12-factor app methodology. All configuration variables use the LVT_ prefix.
# Set connection limits
export LVT_MAX_CONNECTIONS=10000
export LVT_MAX_CONNECTIONS_PER_GROUP=100
# Configure allowed origins for WebSocket connections
export LVT_ALLOWED_ORIGINS="https://example.com,https://app.example.com"
# Set shutdown timeout
export LVT_SHUTDOWN_TIMEOUT=30s
# Configure logging
export LVT_LOG_LEVEL=info
package main
import (
"log"
"github.com/livetemplate/livetemplate"
)
func main() {
// Load configuration from environment variables
envConfig, err := livetemplate.LoadEnvConfig()
if err != nil {
log.Fatal("Failed to load config:", err)
}
// Validate configuration
if err := envConfig.Validate(); err != nil {
log.Fatal("Invalid config:", err)
}
// Create template with environment-based configuration
tmpl := livetemplate.New("app", envConfig.ToOptions()...)
// ... rest of your application
}
LVT_MAX_CONNECTIONSMaximum number of concurrent WebSocket connections across all users.
0 (unlimited)LVT_MAX_CONNECTIONS=10000Use case: Prevent resource exhaustion and OOM kills by limiting total connections.
LVT_MAX_CONNECTIONS_PER_GROUPMaximum number of connections per session group (user/session).
0 (unlimited)LVT_MAX_CONNECTIONS_PER_GROUP=100Use case: Prevent a single user from exhausting all connection slots (DoS protection).
LVT_ALLOWED_ORIGINSComma-separated list of allowed WebSocket origins for CORS.
LVT_ALLOWED_ORIGINS="https://example.com,https://app.example.com"Use case: Security - prevent unauthorized domains from connecting to your WebSocket endpoint.
Note: Whitespace around commas is automatically trimmed.
LVT_WEBSOCKET_DISABLEDDisable WebSocket connections (HTTP-only mode).
falseLVT_WEBSOCKET_DISABLED=truetrue, false, 1, 0, yes, no, on, off (case-insensitive)Use case: Testing or deployments where WebSocket is not available.
LVT_WS_BUFFER_SIZEWebSocket send buffer size per connection (async message queuing).
50LVT_WS_BUFFER_SIZE=100Use case: Tune WebSocket backpressure behavior. Larger buffers handle burst traffic; smaller buffers reduce memory per connection.
Recommended values:
10-2550 (default)100-1000Note: This variable is loaded directly in New(), not via LoadEnvConfig().
LVT_DEV_MODEEnable development mode.
falseLVT_DEV_MODE=truetrue, false, 1, 0, yes, no, on, off (case-insensitive)Features when enabled:
Use case: Local development and debugging.
LVT_LOADING_DISABLEDDisable the automatic loading indicator on page load.
falseLVT_LOADING_DISABLED=truetrue, false, 1, 0, yes, no, on, off (case-insensitive)Use case: Custom loading indicators or SSR scenarios.
LVT_PROGRESSIVE_ENHANCEMENTEnable non-JS form submission support via POST-Redirect-GET pattern.
trueLVT_PROGRESSIVE_ENHANCEMENT=falsetrue, false, 1, 0, yes, no, on, off (case-insensitive)LoadEnvConfig()Use case: When enabled, HTTP form submissions from non-JavaScript clients receive full HTML page responses. Disable if you only support WebSocket-capable clients.
LVT_TEMPLATE_BASE_DIRBase directory for template auto-discovery.
runtime.Caller detection)LVT_TEMPLATE_BASE_DIR=./templatesUse case: Override automatic template directory detection. Useful in containerized deployments where runtime.Caller may resolve to an unexpected path.
LVT_SHUTDOWN_TIMEOUTMaximum duration to wait for graceful shutdown before forcing close.
30sLVT_SHUTDOWN_TIMEOUT=45s30s, 1m, 500ms, 1h30m)Use case: Control how long to wait for active connections to close during deployment.
Recommended values:
10s30s - 60s2m - 5mLVT_LOG_LEVELLogging verbosity level.
infoLVT_LOG_LEVEL=debugdebug, info, warn, error (case-insensitive)Levels:
debug: Verbose logging for developmentinfo: Standard operational logging (default)warn: Warnings and errorserror: Errors onlyLVT_METRICS_ENABLEDEnable Prometheus metrics export.
trueLVT_METRICS_ENABLED=falsetrue, false, 1, 0, yes, no, on, off (case-insensitive)Use case: Disable metrics in development or testing environments.
Note: When disabled, the /metrics endpoint will still exist but return empty results.
export LVT_DEV_MODE=true
export LVT_LOG_LEVEL=debug
export LVT_SHUTDOWN_TIMEOUT=10s
export LVT_MAX_CONNECTIONS=10000
export LVT_MAX_CONNECTIONS_PER_GROUP=100
export LVT_ALLOWED_ORIGINS="https://example.com,https://app.example.com"
export LVT_SHUTDOWN_TIMEOUT=30s
export LVT_LOG_LEVEL=info
export LVT_METRICS_ENABLED=true
export LVT_WS_BUFFER_SIZE=100
export LVT_MAX_CONNECTIONS=5000
export LVT_MAX_CONNECTIONS_PER_GROUP=50
export LVT_ALLOWED_ORIGINS="https://secure.example.com"
export LVT_SHUTDOWN_TIMEOUT=45s
export LVT_LOG_LEVEL=warn
export LVT_WEBSOCKET_DISABLED=true
export LVT_LOADING_DISABLED=true
export LVT_METRICS_ENABLED=false
export LVT_LOG_LEVEL=error
version: '3.8'
services:
app:
image: myapp:latest
environment:
LVT_MAX_CONNECTIONS: "10000"
LVT_MAX_CONNECTIONS_PER_GROUP: "100"
LVT_ALLOWED_ORIGINS: "https://example.com"
LVT_SHUTDOWN_TIMEOUT: "30s"
LVT_LOG_LEVEL: "info"
LVT_METRICS_ENABLED: "true"
LVT_WS_BUFFER_SIZE: "100"
ports:
- "8080:8080"
apiVersion: v1
kind: ConfigMap
metadata:
name: livetemplate-config
data:
LVT_MAX_CONNECTIONS: "10000"
LVT_MAX_CONNECTIONS_PER_GROUP: "100"
LVT_ALLOWED_ORIGINS: "https://example.com,https://app.example.com"
LVT_SHUTDOWN_TIMEOUT: "30s"
LVT_LOG_LEVEL: "info"
LVT_METRICS_ENABLED: "true"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
template:
spec:
containers:
- name: app
image: myapp:latest
envFrom:
- configMapRef:
name: livetemplate-config
You can also configure LiveTemplate programmatically using options:
tmpl := livetemplate.New("app",
livetemplate.WithMaxConnections(10000),
livetemplate.WithMaxConnectionsPerGroup(100),
livetemplate.WithAllowedOrigins([]string{"https://example.com"}),
livetemplate.WithDevMode(false),
)
| Option | Description |
|---|---|
WithUpgrader(upgrader) |
Custom *websocket.Upgrader for WebSocket connections |
WithUpload(name, config) |
Configure file upload fields (see Upload Reference) |
WithPubSubBroadcaster(broadcaster) |
Redis pub/sub for distributed deployments |
WithComponentTemplates(sets...) |
Register component template sets |
WithIgnoreTemplateDirs(dirs...) |
Skip directories during template discovery |
WithPermissiveOriginCheck() |
Bypass origin check (dev only) |
WithProgressiveEnhancement(enabled) |
Non-JS form submission support (default: true) |
WithCookieMaxAge(duration) |
Session cookie max age (default: 365 days) |
Note: Programmatic configuration takes precedence over environment variables.
Configuration is automatically validated when loaded:
envConfig, err := livetemplate.LoadEnvConfig()
if err != nil {
// Handle invalid environment variable format
log.Fatal(err)
}
// Explicit validation
if err := envConfig.Validate(); err != nil {
// Handle invalid configuration values
log.Fatal(err)
}
Common validation errors:
LVT_MAX_CONNECTIONS to prevent OOMLVT_ALLOWED_ORIGINS in production for securityLVT_METRICS_ENABLED=true for observabilityValidate() before running your applicationCause: Value is not a valid integer or is negative.
Solution: Use a non-negative integer: LVT_MAX_CONNECTIONS=10000
Cause: Value is not in Go duration format.
Solution: Use Go duration format: LVT_SHUTDOWN_TIMEOUT=30s
Cause: Origin not in LVT_ALLOWED_ORIGINS.
Solution: Add your domain to allowed origins or set LVT_DEV_MODE=true for development.
Cause: LVT_METRICS_ENABLED=false
Solution: Set LVT_METRICS_ENABLED=true (default)