{{- if .Values.ingress.enabled }}
{{- $fullName := include "assist.fullname" . -}}
{{- $socketioSvcPort := .Values.service.ports.socketio -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ $fullName }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "assist.labels" . | nindent 4 }}
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/configuration-snippet: |
      #set $sticky_used "no";
      #if ($sessionid != "") {
      #  set $sticky_used "yes";
      #}

      #add_header X-Debug-Session-ID $sessionid;
      #add_header X-Debug-Session-Type "wss";
      #add_header X-Sticky-Session-Used $sticky_used;
      #add_header X-Upstream-Server $upstream_addr;

      proxy_hide_header access-control-allow-headers;
      proxy_hide_header Access-Control-Allow-Origin;
      add_header 'Access-Control-Allow-Origin' $http_origin always;
      add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
      add_header 'Access-Control-Allow-Headers' 'sessionid, Content-Type, Authorization' always;
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';

    nginx.ingress.kubernetes.io/upstream-hash-by: $sessionid

  {{- with .Values.ingress.annotations }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  ingressClassName: "{{ tpl .Values.ingress.className . }}" 
  tls:
    - hosts:
        - {{ .Values.global.domainName }}
      {{- if .Values.ingress.tls.secretName}}
      secretName: {{ .Values.ingress.tls.secretName }}
      {{- end}}
  rules:
    - host: {{ .Values.global.domainName }} 
      http:
        paths:
          - pathType: Prefix
            backend:
              service:
                name: {{ $fullName }}
                port:
                  number: {{ $socketioSvcPort }}
            path: /ws-assist/(.*)
{{- end }}
