#!/bin/sh set -e # Carbon Web Server - Docker Entrypoint Script # This script handles configuration and startup of the Carbon server CONFIG_FILE="/app/server.conf" # Helper: normalize boolean env vars (true/false/yes/no/1/0 -> true/false) normalize_bool() { case "$(echo "$1" | tr '[:upper:]' '[:lower:]')" in true|yes|on|1) echo "true" ;; *) echo "false" ;; esac } # Normalize environment variables USE_HTTPS=$(normalize_bool "${USE_HTTPS:-false}") ENABLE_HTTP2=$(normalize_bool "${ENABLE_HTTP2:-false}") ENABLE_WEBSOCKET=$(normalize_bool "${ENABLE_WEBSOCKET:-false}") # Validate configuration constraints validate_config() { # HTTP/2 requires HTTPS if [ "$ENABLE_HTTP2" = "true" ] && [ "$USE_HTTPS" != "true" ]; then echo "ERROR: HTTP/2 requires HTTPS to be enabled" echo "Set USE_HTTPS=true or disable HTTP/2 with ENABLE_HTTP2=false" exit 1 fi } # Generate server.conf from environment variables generate_config() { # Skip generation if user mounted a custom config and KEEP_CONFIG is set if [ -f "$CONFIG_FILE" ] && [ "${KEEP_CONFIG:-false}" = "true" ]; then echo "Using existing server.conf (KEEP_CONFIG=true)" return 0 fi echo "Generating server.conf from environment variables..." cat > "$CONFIG_FILE" << EOF # Carbon Web Server Configuration (Auto-generated) # Generated at: $(date -u +"%Y-%m-%d %H:%M:%S UTC") running = true # Network configuration port = ${PORT:-8080} use_https = ${USE_HTTPS} enable_http2 = ${ENABLE_HTTP2} enable_websocket = ${ENABLE_WEBSOCKET} server_name = ${SERVER_NAME:-0.0.0.0} # Performance configuration max_threads = ${MAX_THREADS:-4} max_connections = ${MAX_CONNECTIONS:-1024} # Logging configuration log_file = /app/log/server.log log_mode = ${LOG_MODE:-classic} # Path configuration www_path = /app/www # SSL configuration (if HTTPS enabled) ssl_cert_path = ${SSL_CERT_PATH:-/app/ssl/cert/cert.pem} ssl_key_path = ${SSL_KEY_PATH:-/app/ssl/key/key.key} EOF if [ $? -ne 0 ]; then echo "ERROR: Failed to write configuration file" exit 1 fi } # Validate SSL certificates if HTTPS is enabled validate_ssl() { if [ "$USE_HTTPS" = "true" ]; then CERT_PATH="${SSL_CERT_PATH:-/app/ssl/cert/cert.pem}" KEY_PATH="${SSL_KEY_PATH:-/app/ssl/key/key.key}" if [ ! -f "$CERT_PATH" ]; then echo "ERROR: SSL certificate not found at $CERT_PATH" echo "Please mount your SSL certificate or set SSL_CERT_PATH" exit 1 fi if [ ! -r "$CERT_PATH" ]; then echo "ERROR: SSL certificate at $CERT_PATH is not readable" exit 1 fi if [ ! -f "$KEY_PATH" ]; then echo "ERROR: SSL key not found at $KEY_PATH" echo "Please mount your SSL key or set SSL_KEY_PATH" exit 1 fi if [ ! -r "$KEY_PATH" ]; then echo "ERROR: SSL key at $KEY_PATH is not readable" exit 1 fi echo "SSL certificates validated successfully" fi } # Create necessary directories setup_directories() { mkdir -p /app/log 2>/dev/null || true mkdir -p /app/www 2>/dev/null || true } # Print startup banner print_banner() { echo "========================================" echo " Carbon Web Server" echo "========================================" echo " Port: ${PORT:-8080}" echo " HTTPS: ${USE_HTTPS}" echo " HTTP/2: ${ENABLE_HTTP2}" echo " WebSocket: ${ENABLE_WEBSOCKET}" echo " Log Mode: ${LOG_MODE:-classic}" echo "========================================" } # Main entry point main() { setup_directories validate_config generate_config validate_ssl print_banner # Verify server binary exists and is executable if [ ! -x /app/server ]; then echo "ERROR: Server binary not found or not executable at /app/server" exit 1 fi echo "Starting Carbon server..." # Execute the server (replaces shell process) exec /app/server } main "$@"