{{- if .Values.apache.enabled }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "mastodon.apache.vhostconfigmap" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: mastodon
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  mastodon-vhost.conf: |-
    <VirtualHost VirtualHost 127.0.0.1:{{ .Values.apache.containerPorts.http }} _default_:{{ .Values.apache.containerPorts.http }}>
      ServerName {{ include "mastodon.web.domain" . }}
      ServerAlias *
      <Location "/">
        ProxyPass http://{{ include "mastodon.web.fullname" . }}:{{ .Values.web.service.ports.http }}/
        ProxyPassReverse {{ include "mastodon.web.domain" . }}
        Order allow,deny
        Allow from all
      </Location>
      <Location "/api/v1/streaming">
        # Streaming uses normal API calls and websockets. We used this configuration
        # based on https://stackoverflow.com/questions/27526281/websockets-and-apache-proxy-how-to-configure-mod-proxy-wstunnel
        RewriteEngine On
        RewriteCond %{HTTP:Upgrade} =websocket [NC]
        RewriteRule /api/(.*)           ws://{{ include "mastodon.streaming.fullname" . }}:{{ .Values.streaming.service.ports.http }}/api/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /api/(.*)           http://{{ include "mastodon.streaming.fullname" . }}:{{ .Values.streaming.service.ports.http }}/api/$1 [P,L]
        ProxyPassReverse {{ include "mastodon.web.domain" . }}
        Order allow,deny
        Allow from all
      </Location>
      {{- if .Values.minio.enabled }}
      <Location "/{{ include "mastodon.s3.bucket" . }}">
        ProxyPass http://{{ include "mastodon.s3.host" . }}:{{ include "mastodon.s3.port" . }}/{{ include "mastodon.s3.bucket" . }}/
        ProxyPassReverse {{ include "mastodon.web.domain" . }}
        Order allow,deny
        Allow from all
      </Location>
      {{- end }}
    </VirtualHost>
{{- end }}
