# The site block for one slug under /srv/www. Caddy issues and renews the
# certificate itself, so this is the whole configuration for the host.
#
# /srv/www/pplus is a symlink to the current release (scripts/deploy.sh);
# Caddy follows symlinks by default. Routing lives in the URL hash, so the one
# page is /, and a path that is not a file is sent there (see handle_errors).
# `pixi run caddy-check` serves site/ with a real Caddy under this file and
# holds what it answers.
# Add both blocks: the snippet first, then the site.

# The security headers, for every answer: the files, and index.html served for a 404.
(pplus_headers) {
    # What the page may load and where it may send. Everything is this site's own,
    # except Compile & run, which posts the Mojo pane and its input to godbolt.org.
    #   script-src   the modules, plus the two inline scripts of index.html by
    #                hash: the theme set before paint, and the import map (which
    #                cannot be a file). tests/csp.test.mjs fails when index.html
    #                and these hashes disagree.
    #   style-src    'unsafe-inline' because the views and CodeMirror set style
    #                attributes and <style> elements; no markup comes from data.
    #   frame-src    the With Quarto gallery shows this app's own embeds.
    #   frame-ancestors  course pages embed views in iframes, so no
    #                X-Frame-Options here; framing is allowed from the course
    #                origins, and from a local `quarto preview` of them.
    header {
        Content-Security-Policy "default-src 'self'; script-src 'self' 'sha256-I0FTUDEvZjevwnUsTxL/Wv6yjSk4+kJk+rHFqkJdfzU=' 'sha256-RDS2ylVnVHGfhqa7CxiLHKiZwFxVxLSwfQQ6NYsJmuM='; style-src 'self' 'unsafe-inline'; font-src 'self'; img-src 'self' data: blob:; connect-src 'self' https://godbolt.org; frame-src 'self'; worker-src 'none'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self' https://scient.ing https://*.scient.ing http://localhost:* http://127.0.0.1:*"
        Referrer-Policy "no-referrer"
        X-Content-Type-Options "nosniff"
        Permissions-Policy "camera=(), microphone=(), geolocation=(), payment=(), usb=(), clipboard-write=(self)"
    }
}

pplus.scient.ing {
    root * /srv/www/pplus
    encode zstd gzip
    file_server

    # Caching. Only a file whose name changes with its content may be cached as
    # immutable, and no file here has such a name (no bundler adds hashes), so:
    #   the page, the modules and the stylesheets change on any deploy: the
    #     browser keeps them but asks again each time (no-cache), and the answer
    #     is a 304 with no body while the file is unchanged (file_server sends an
    #     ETag), so a stale module never runs against a new index.html;
    #   the fonts change only when fonts/ is replaced by hand, and a stale one
    #     is only an older glyph: kept for 30 days without asking;
    #   VERSION is what CI's smoke test reads back after a deploy: never kept.
    @page path / /index.html
    header @page Cache-Control "no-cache"
    @code path *.js *.css
    header @code Cache-Control "no-cache"
    @fonts path /fonts/*.woff2
    header @fonts Cache-Control "public, max-age=2592000"
    header /VERSION Cache-Control "no-store"
    # build.json names the three commits the site was built from: asked again each time, like the page
    header /build.json Cache-Control "no-cache"
    # the license and notices beside index.html: shown as text, not downloaded
    @notes path /LICENSE.md /NOTICE.md /THIRD_PARTY.md
    header @notes Content-Type "text/plain; charset=utf-8"

    import pplus_headers

    # A path that is not a file. The app's routes live after the #, so the only page is /, and index.html names its
    # scripts, styles and fonts relative to itself: served in place at /a/b/c it would ask for /a/b/dist/main.js
    # and come up blank. So a missing path that looks like a page (no file extension) is sent to /, and the browser
    # carries the # across; a missing file is a plain 404, which is what a script or font that is gone should be.
    handle_errors {
        import pplus_headers
        header Cache-Control "no-store"
        @lostpage {
            expression {http.error.status_code} == 404
            not path *.*
        }
        redir @lostpage / 302
        respond "{http.error.status_code} {http.error.status_text}" {http.error.status_code}
    }
}
