apiVersion: v1
kind: ConfigMap
metadata:
  name: ambassador-nginx-conf
data:
  default.conf: |
        {{- if .Values.SSL }}
        server {
                listen      {{ .Values.service.httpTargetPort }} default_server;
                server_name {{ .Values.service.serverName }};
                return 301 https://$host$request_uri;
        }
        {{- end }}
        server {
                {{- if .Values.SSL }}
                listen               {{ .Values.service.httpsTargetPort }} ssl;
                ssl_certificate      /etc/nginx/ssl/tls.crt;
                ssl_certificate_key  /etc/nginx/ssl/tls.key;
                {{- else }}
                listen               {{ .Values.service.httpTargetPort }};
                listen               [::]:{{ .Values.service.httpTargetPort }};
                {{- end }}
                server_name          {{ .Values.service.serverName }};

                location /private/ {
                         client_max_body_size 2000M;
                         proxy_http_version 1.1;
                         proxy_read_timeout 3600;
                         {{- if .Values.DEV_PHASE.dev }}
                         {{- else }}
                         auth_request /auth;
                         auth_request_set $remoteUser $upstream_http_remote_user;
                         proxy_set_header REMOTE_USER $remoteUser;
                         add_header REMOTE_USER $remoteUser;
                         auth_request_set $access_token $upstream_http_access_token;
                         proxy_set_header Authorization $access_token;
                         add_header Authorization $access_token;
                         {{- end }}
                         proxy_pass http://{{ .Values.global.ambassador_service_name }}:80$request_uri;
                         proxy_pass_request_headers      on;
                         # for guacamole
                         proxy_buffering off;
                         proxy_set_header Upgrade $http_upgrade;
                         proxy_set_header Connection $http_connection;
                         # end guacamole additons
                }

                location = /auth {
                         client_max_body_size 2000M;
                         proxy_http_version 1.1;
                         proxy_pass http://{{ .Values.global.ambassador_service_name }}:80/auth/;
                         proxy_read_timeout 3600;
                         proxy_pass_request_body off;
                         proxy_set_header        Content-Length "";
                         proxy_set_header        X-Original-URI $request_uri;
                }

                location / {
                         client_max_body_size 2000M;
                         {{- if .Values.http_host }}
                         proxy_set_header Host {{ .Values.service.serverName }};
                         {{- else }}
                         proxy_set_header Host $http_host;
                         {{- end }}
                         proxy_set_header X-Real-IP $remote_addr;
                         proxy_set_header X-Scheme $scheme;
                         proxy_connect_timeout   60s;
                         proxy_send_timeout      60s;
                         proxy_read_timeout 60s;
                         proxy_http_version 1.1;
                         proxy_pass http://{{ .Values.global.ambassador_service_name }}:80;
                         proxy_set_header requestUri $request_uri;
                }

                {{- if .Values.restartrApi }}
                location ~ .(.*1\.json)$ {
                        proxy_set_header Host $http_host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Scheme $scheme;
                        proxy_http_version 1.1;
                        proxy_pass http://{{ .Values.global.restartr_api_service_name }}:5552/$1;
                        proxy_set_header requestUri $request_uri;

                }

                location /api/apidocs/ {
                         rewrite /api/(.*) /$1 break;
                         proxy_set_header Host $http_host;
                         proxy_set_header X-Real-IP $remote_addr;
                         proxy_set_header X-Scheme $scheme;
                         proxy_http_version 1.1;
                         proxy_pass http://{{ .Values.global.restartr_api_service_name }}:5552/;
                         proxy_set_header requestUri $request_uri;
                         #proxy_intercept_errors on;
                         #error_page 404 = @fallback;
                }

                location /api/ {
                         proxy_set_header Host $http_host;
                         proxy_set_header X-Real-IP $remote_addr;
                         proxy_set_header X-Scheme $scheme;
                         proxy_http_version 1.1;
                         proxy_pass http://{{ .Values.global.restartr_api_service_name }}:5552$request_uri;
                         proxy_set_header requestUri $request_uri;
                         #proxy_intercept_errors on;
                         #error_page 404 = @fallback;
                }

                #location @fallback {
                #        try_files /var/www/flasgger/flasgger/ui3/static/ =500;
                #}
                {{- end }}

                # Makes /airflow/ accessible if not authenticated
                location /airflow {
                         proxy_http_version 1.1;
                         {{- if .Values.DEV_PHASE.dev }}
                         {{- else }}
                         auth_request /auth;
                         auth_request_set $remoteUser $upstream_http_remote_user;
                         proxy_set_header REMOTE_USER $remoteUser;
                         add_header REMOTE_USER $remoteUser;
                         auth_request_set $access_token $upstream_http_access_token;
                         proxy_set_header Authorization $access_token;
                         add_header Authorization $access_token;
                         {{- end }}
                         proxy_set_header Host $http_host;
                         proxy_pass http://{{ .Values.global.ambassador_service_name }}:80$request_uri;
                         proxy_pass_request_headers  on;
                }
        }
