# Caddyfile  --  Pflegesyndikat: Pinnwand (bulletin board & waiting list)
# Book: "Pinnwand-Warteliste" (EN), chapter 4 + appendix B.
#
# Lives on the vserver in ~/docker/caddy/ and serves:
#   pflegesyndikat.de       -> www/  (THE one site, public)
# NOTE (2026-11): app.pflegesyndikat.de is RETIRED – merged into
# pflegesyndikat.de, no vhost, no redirect. Do not re-add it.
# The site uses the PHP API: /api/* is dispatched to the front
# controller api/index.php and forwarded to PHP-FPM via FastCGI.
#
# NOTE (2026-11-07): `rewrite * /api/index.php` INSIDE the /api/* handler
# stopped working with Caddy 2.11.3+ (FastCGI path-confusion fix,
# CVE-2026-45135): the rewrite no longer reaches php_fastcgi, so PHP-FPM
# received "/index.php" instead of "/api/index.php" and every /api/*
# request answered 404 "File not found." – which also killed the
# Besucherzähler. Fix = point the front controller at the file with the
# php_fastcgi subdirectives `index`/`try_files` (see below), never with
# a hand-written `rewrite`.
#
# NOTE (2026-10-08): test.pflegesyndikat.de NO LONGER EXISTS and must NOT
# be re-added. The full webpage now lives at pflegesyndikat.de.
# Do NOT create a test.* block (not even behind basic auth) – DNS for
# test.* may still resolve, but Caddy intentionally serves nothing for it.
{
    email jangeorg@gmx.net
}

pflegesyndikat.de, www.pflegesyndikat.de {
    root * /srv/www
    encode zstd gzip

    # Security headers for ALL responses
    header {
        Strict-Transport-Security "max-age=31536000; includeSubDomains"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "DENY"
        Referrer-Policy "strict-origin-when-cross-origin"
        -Server
    }

    # Cache headers for assets
    @assets path /assets/* /css/* /js/*
    header @assets Cache-Control "public, max-age=604800"

    # --- 2FA backend: /api/v1/* -> twofa container (NEW 2026-11-18) --
    # MUST come BEFORE the /api/* PHP handler: Caddy matches the first
    # handle block whose matcher is satisfied. The 2FA REST API
    # (register / challenge / verify / TOTP / health) is served by the
    # Rust actix-web container; everything else under /api/* still falls
    # through to the PHP front controller below.
    handle /api/v1/* {
        reverse_proxy twofa:8080
    }

    # --- The gate to PHP: only /api/* -------------------------------
    handle /api/* {
        # Front controller (book ch. 7): /api/{route} is dispatched by
        # api/index.php. Caddy's file matcher must be able to SEE that
        # file, so ./api and ./lib are mounted into the Caddy container
        # at the same path /srv as into php-fpm (see docker-compose.yml).
        # `index` names the front controller; the =404 entry makes a
        # truly unknown path a real 404 instead of a silent 200.
        root * /srv
        php_fastcgi phpfpm:9000 {
            index /api/index.php
            try_files {path} {path}/index.php /api/index.php =404
        }
    }

    # --- Static (webpage build + admin.html) ------------------------
    handle {
        # MIME types for app download files. Caddy's built-in table does not
        # know the flatpak family, so without these:
        #   .flatpakref  -> served with NO Content-Type (opened as text file)
        #   .flatpak     -> served as text/html (users "downloaded" index.html)
        # The .exe gets an explicit type so SmartScreen/download managers
        # treat it as a binary.
        # `file_server` only sets Content-Type when it is still empty, so a
        # `header` directive on a matching request pins the type we want.
        # (Note: there is no `type` directive in the standard Caddy build.)
        @flatpakref   path *.flatpakref
        @flatpakrepo  path *.flatpakrepo
        @flatpak      path *.flatpak
        @sums         path /repo/SHA256SUMS
        @winexe       path /windows/*.exe
        @apk          path *.apk
        header @flatpakref   Content-Type application/vnd.flatpak.ref
        header @flatpakrepo  Content-Type application/vnd.flatpak.repo
        header @flatpak      Content-Type application/octet-stream
        header @sums         Content-Type text/plain
        header @winexe       Content-Type application/octet-stream
        # Android package: without this Caddy falls back to application/
        # octet-stream and some launchers refuse to offer an in-place
        # install. Pins the correct type for the 2FA Android app.
        header @apk          Content-Type application/vnd.android.package-archive

        try_files {path} {path}/ index.html
        file_server
    }

    log {
        output file /data/access.log {
            roll_size 10MiB
            roll_keep 5
        }
        format console
    }
}

# Gitea (~/docker/gitea, 127.0.0.1:3000)
git.pflegesyndikat.de {
    encode zstd gzip

    header {
        Strict-Transport-Security "max-age=31536000; includeSubDomains"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "SAMEORIGIN"
        Referrer-Policy "strict-origin-when-cross-origin"
        -Server
    }

    reverse_proxy gitea:3000

    log {
        output file /data/access.log {
            roll_size 10MiB
            roll_keep 5
        }
        format console
    }
}

# Mailcow UI + autoconfig (~/docker/mailcow, nginx-mailcow:8080)
# TLS is terminated here; mailcow runs with SKIP_LETS_ENCRYPT=y.
mail.pflegesyndikat.de, autoconfig.pflegesyndikat.de, autodiscover.pflegesyndikat.de {
    encode zstd gzip

    header {
        Strict-Transport-Security "max-age=31536000; includeSubDomains"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "SAMEORIGIN"
        Referrer-Policy "strict-origin-when-cross-origin"
        -Server
    }

    reverse_proxy nginx-mailcow:8080 {
        header_up X-Forwarded-Proto "https"
    }

    log {
        output file /data/access.log {
            roll_size 10MiB
            roll_keep 5
        }
        format console
    }
}
