# Global configuration
{

    {% if not ENABLE_WEB_PROXY %}
    # Enable proxying from all servers by default. Otherwise, X-Forwarded-* headers will
    # be overwritten.
    # https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#defaults
    servers {
        trusted_proxies static 0.0.0.0/0 ::/0
    }
    {% endif %}
    {{ patch("caddyfile-global")|indent(4) }}
}

# proxy directive snippet (with logging) to be used as follows:
#
#     import proxy "containername:port"
(proxy) {
    log {
        output stdout
        format filter {
            wrap json
            fields {
                common_log delete
                request>headers delete
                resp_headers delete
                tls delete
            }
        }
    }

    # This will compress requests that matches the default criteria set by Caddy.
    # see https://caddyserver.com/docs/caddyfile/directives/encode
    # for information about the defaults; i.e. how/when this will be applied.
    encode gzip

    reverse_proxy {args.0} {
        header_up X-Forwarded-Port {{ 443 if ENABLE_HTTPS else 80 }}
    }

    {{ patch("caddyfile-proxy")|indent(4) }}
}

{{ LMS_HOST }}{$default_site_port}, {{ PREVIEW_LMS_HOST }}{$default_site_port} {
    @favicon_matcher {
        path_regexp ^/favicon.ico$
    }
    rewrite @favicon_matcher /theming/asset/images/favicon.ico

    # Limit profile image upload size
    handle_path /api/profile_images/*/*/upload {
        request_body {
            max_size 1MB
        }
    }

    import proxy "lms:8000"

    {{ patch("caddyfile-lms")|indent(4) }}

    handle_path /* {
        request_body {
            max_size 4MB
        }
    }
}

{{ CMS_HOST }}{$default_site_port} {
    @favicon_matcher {
        path_regexp ^/favicon.ico$
    }
    rewrite @favicon_matcher /theming/asset/images/favicon.ico

    import proxy "cms:8000"

    {{ patch("caddyfile-cms")|indent(4) }}

    handle_path /* {
        request_body {
            max_size 250MB
        }
    }
}

{{ patch("caddyfile") }}
