# WarpDrive Comprehensive Configuration
# ============================================
# This is the primary configuration file for WarpDrive.
# Set WARPDRIVE_CONFIG=config.toml to use this file.
#
# All sections are documented with [REQUIRED] or [OPTIONAL] tags.
# Optional features are included but commented out - uncomment to enable.
# ============================================

# ============================================
# Server Configuration [REQUIRED]
# ============================================
[server]
http_port = 8080
https_port = 443
worker_threads = 4

# ============================================
# Cluster Configuration [OPTIONAL]
# ============================================
# Enable for multi-instance deployments with leader election
[cluster]
# Cluster name (optional, will auto-generate from hostname if not set)
# Must be max 16 chars: alphanumeric + dash/underscore
name = "prod-cluster"

# Instance ID (optional, will generate UUID if not set)
# instance_id = "web-01"

# Redis connection for leader election in hooks
# Required if any hooks use require_leader = true
# Uncomment to enable leader election:
# redis_url = "redis://localhost:6379/1"

# ============================================
# Lifecycle Hooks [OPTIONAL]
# ============================================
# Hooks execute at specific lifecycle phases.
# These examples use simple echo commands - replace with your actual commands.
# Phases: pre_start, cluster_ready, pre_shutdown, post_shutdown

# Pre-start hook: Run before starting upstreams
[[hooks]]
name = "init-check"
phase = "pre_start"
command = "echo 'Initializing WarpDrive...'"
timeout_secs = 10
require_leader = false
continue_on_failure = false

# Pre-start hook with leader election (requires redis_url above)
# [[hooks]]
# name = "db-migrate"
# phase = "pre_start"
# command = "bundle exec rails db:migrate"
# working_dir = "."
# timeout_secs = 300
# require_leader = true  # Only one instance runs this
# continue_on_failure = false

# Cluster-ready hook: Run after all processes are started
[[hooks]]
name = "ready-notification"
phase = "cluster_ready"
command = "echo 'WarpDrive cluster ready'"
timeout_secs = 10
require_leader = false
continue_on_failure = true

# Pre-shutdown hook: Run before stopping processes
[[hooks]]
name = "drain-connections"
phase = "pre_shutdown"
command = "echo 'Draining connections...'"
timeout_secs = 30
continue_on_failure = true

# Post-shutdown hook: Cleanup after processes stopped
[[hooks]]
name = "cleanup"
phase = "post_shutdown"
command = "echo 'Cleanup complete'"
timeout_secs = 10
continue_on_failure = true

# ============================================
# Upstream Services [REQUIRED]
# ============================================
# Define your backend services here.
# Each upstream can optionally have a managed process.

# Main application server
[upstreams.app]
protocol = "http"
socket = "unix:/tmp/warpdrive-app.sock"

# Managed Sinatra process with Puma
[upstreams.app.process]
command = "sh"
args = ["-c", "cd /Users/seuros/Projects/ecosystem/warpdrive/tests/app && bundle exec puma -b unix:///tmp/warpdrive-app.sock config.ru"]
env = { SERVER = "Puma" }
restart = { on_failure = { max_attempts = 3, strategy = { exponential = { base_delay_secs = 1, multiplier = 2.0, max_delay_secs = 30 } } } }
log_stdout = "logs/app.stdout.log"
log_stderr = "logs/app.stderr.log"

# Note: Health checks not supported for Unix socket upstreams (requires host+port)

# WebSocket server example (Unix socket)
# [upstreams.cable]
# protocol = "ws"
# socket = "unix:/tmp/cable.sock"

# NOTE: Multi-backend pools are not supported in WarpDrive 0.3.0+
# Application servers (Puma/Falcon) handle load balancing via their workers.
# Each upstream = ONE endpoint (Unix socket OR TCP address).
#
# If you need to load balance across multiple backend servers, use:
# - A single upstream pointing to an external load balancer (nginx, HAProxy)
# - Or deploy multiple application servers behind a cloud load balancer
#
# Example single TCP upstream:
# [upstreams.api]
# protocol = "http"
# host = "api.internal"
# port = 8080

# Additional upstream examples (commented out)

# ActionCable WebSocket server with managed process
# [upstreams.cable_managed]
# protocol = "wss"
# host = "127.0.0.1"
# port = 3001
#
# [upstreams.cable_managed.process]
# command = "bundle"
# args = ["exec", "puma", "-p", "3001", "cable/config.ru"]
# env = { RAILS_ENV = "production" }
# depends_on = ["app"]  # Start after app
# restart = { always = { max_attempts = 10, delay_secs = 2 } }

# Sidekiq Web UI
# [upstreams.sidekiq]
# protocol = "http"
# host = "127.0.0.1"
# port = 7080
#
# [upstreams.sidekiq.process]
# command = "bundle"
# args = ["exec", "sidekiq-web", "-p", "7080"]
# env = { RAILS_ENV = "production" }
# depends_on = ["app"]
# restart = { on_failure = { max_attempts = 3, strategy = { linear = { base_delay_secs = 1, increment_secs = 2, max_delay_secs = 10 } } } }

# External service (gRPC example)
# [upstreams.grpc_service]
# protocol = "grpc"
# host = "127.0.0.1"
# port = 50051

# ============================================
# Routing Rules [REQUIRED]
# ============================================
# Routes are evaluated in order - first match wins.
# Each route must reference an upstream defined above.

# WebSocket connections (disabled - cable upstream not configured)
# [[routes]]
# path_prefix = "/cable"
# upstream = "cable"
# description = "WebSocket connections"

# API routes (disabled - api_pool removed in v0.3.0)
# [[routes]]
# path_prefix = "/api"
# upstream = "app"
# strip_prefix = true
# description = "API backend"

# Admin routes with path rewrite
[[routes]]
path_prefix = "/admin"
upstream = "app"
rewrite = "/backend/admin"  # /admin/users -> /backend/admin/users
description = "Admin interface"

# Host-based routing (disabled - api_pool removed)
# [[routes]]
# host = "api.example.com"
# upstream = "app"
# description = "API subdomain routing"

# Method-based routing (disabled - api_pool removed)
# [[routes]]
# path_prefix = "/graphql"
# methods = ["POST"]
# upstream = "app"
# description = "GraphQL endpoint"

# Header-based routing (disabled - api_pool removed)
# [[routes]]
# path_prefix = "/v2"
# upstream = "app"
# description = "API v2 with version header"
# [[routes.headers]]
# name = "X-API-Version"
# value = "2.0"

# Add custom headers to requests
# [[routes]]
# path_prefix = "/api"
# upstream = "api_pool"
# add_headers = [
#   { name = "X-Backend-Version", value = "v2" },
#   { name = "X-Routed-By", value = "warpdrive" }
# ]

# Plugin example (requires WASM plugin file)
# [[routes]]
# path_prefix = "/instrumented"
# upstream = "app"
# plugin = "plugins/http_logger.wasm"
# description = "Route with WASM plugin"

# Catch-all route (must be last)
[[routes]]
path_prefix = "/"
upstream = "app"
description = "Default application"

# ============================================
# Cache Configuration [OPTIONAL]
# ============================================
[cache]
size_bytes = 536870912  # 512 MB
# Uncomment to enable Redis L2 cache:
# redis_url = "redis://localhost:6379/0"

# ============================================
# Middleware Configuration [OPTIONAL]
# ============================================
[middleware]
forward_headers = true
x_sendfile_enabled = true
gzip_compression_enabled = true
log_requests = true

# Rate limiting
[middleware.rate_limit]
enabled = true
requests_per_second = 100
burst_size = 200

# Circuit breaker
[middleware.circuit_breaker]
enabled = true
failure_threshold = 5
timeout_secs = 60

# ============================================
# TLS Configuration [OPTIONAL]
# ============================================
# Uncomment for HTTPS with automatic Let's Encrypt certificates
# [tls]
# enabled = true
# domains = ["example.com", "www.example.com"]
# acme_directory = "/var/lib/warpdrive/acme"
# email = "admin@example.com"

# Or use manual certificates:
# [tls]
# enabled = true
# cert_path = "/etc/warpdrive/server.crt"
# key_path = "/etc/warpdrive/server.key"

# ============================================
# Static File Serving [OPTIONAL]
# ============================================
# Uncomment to serve static files directly from WarpDrive
# [static]
# enabled = true
# root = "/var/www/app/public"
# paths = ["/assets", "/packs", "/images", "/favicon.ico"]
# cache_control = "public, max-age=31536000, immutable"
# gzip = true
# fallthrough = true
