# DesignerCEP Caddy 配置文件
# 
# 部署架构：
# - app.aidg168.uk       → 前端应用（登录 + 主功能）
# - backend.aidg168.uk   → 后端 API
#
# 使用方法：
# 1. 将此文件上传到服务器 /etc/caddy/Caddyfile
# 2. sudo caddy validate --config /etc/caddy/Caddyfile
# 3. sudo systemctl restart caddy

# ==================== 全局配置 ====================
{
    # 如果使用 Cloudflare，关闭自动 HTTPS
    # auto_https off
    
    # 如果不使用 Cloudflare，保持默认（自动申请证书）
    email admin@aidg168.uk
}

# ==================== 前端应用 ====================
app.aidg168.uk {
    # 静态文件根目录
    root * /var/www/DesignerCEP/Server/static/app
    
    # SPA 路由支持（重要！）
    # 所有路由都返回 index.html，让 Vue Router 处理
    try_files {path} /index.html
    
    # 提供静态文件
    file_server
    
    # ========== 缓存策略 ==========
    
    # HTML 文件不缓存（确保更新即时生效）
    @html {
        path *.html
    }
    header @html {
        Cache-Control "no-cache, no-store, must-revalidate"
        Pragma "no-cache"
        Expires "0"
    }
    
    # JS/CSS 长期缓存（文件名有 hash，可以安全缓存）
    @assets {
        path *.js *.css *.woff *.woff2 *.ttf *.eot
    }
    header @assets {
        Cache-Control "public, max-age=31536000, immutable"
    }
    
    # 图片缓存
    @images {
        path *.png *.jpg *.jpeg *.gif *.svg *.ico *.webp
    }
    header @images {
        Cache-Control "public, max-age=2592000"
    }
    
    # ========== 安全头 ==========
    header {
        # 允许在 iframe 中加载（CEP 需要）
        X-Frame-Options "SAMEORIGIN"
        
        # 防止 MIME 类型嗅探
        X-Content-Type-Options "nosniff"
        
        # XSS 保护
        X-XSS-Protection "1; mode=block"
        
        # 隐藏服务器信息
        -Server
    }
    
    # ========== 压缩 ==========
    encode {
        gzip 6
        zstd
    }
    
    # ========== 日志 ==========
    log {
        output file /var/log/caddy/app.aidg168.uk.log {
            roll_size 50mb
            roll_keep 10
            roll_keep_for 720h
        }
        format json
        level INFO
    }
}

# ==================== 后端 API ====================
backend.aidg168.uk {
    # 反向代理到 FastAPI
    reverse_proxy localhost:8000 {
        # 传递客户端真实 IP
        header_up X-Real-IP {remote_host}
        header_up X-Forwarded-For {remote_host}
        header_up X-Forwarded-Proto {scheme}
        header_up X-Forwarded-Host {host}
        
        # 超时设置
        transport http {
            dial_timeout 5s
            response_header_timeout 30s
        }
    }
    
    # ========== 压缩 ==========
    encode gzip
    
    # ========== 日志 ==========
    log {
        output file /var/log/caddy/backend.aidg168.uk.log {
            roll_size 50mb
            roll_keep 10
            roll_keep_for 720h
        }
        format json
        level INFO
    }
}

# ==================== 主域名重定向（可选）====================
aidg168.uk, www.aidg168.uk {
    # 重定向到应用
    redir https://app.aidg168.uk{uri} permanent
}

