# only needed if not defined yet
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

proxy_read_timeout 600;
proxy_connect_timeout 600;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;

server {
    # runbot frontend
    listen 80;
    listen [::]:80;
    server_name  runbot.domain.com;

    location / {
	    proxy_pass http://127.0.0.1:8069;
    }

    # runbot frontend notifications: optionnal
    location /longpolling {
	    proxy_pass http://127.0.0.1:8070;
    }
    # not tested yet, replacement of longpolling to websocket for odoo 16.0
    # location /websocket { 
    #     proxy_set_header X-Forwarded-Host $remote_addr;
    #     proxy_set_header X-Forwarded-For $remote_addr;
    #     proxy_set_header X-Real-IP $remote_addr;
    #     proxy_set_header Host $host;
    #     proxy_set_header Upgrade $http_upgrade;
    #     proxy_set_header Connection $connection_upgrade;
    #     proxy_pass http://127.0.0.1:8080;
    # }
    
    # serve text log, zip, other docker outputs ...
    # server_name should be the same as the local builder (foced-host-name)
    location /runbot/static/ {
      alias /home/runbot_user/odoo/runbot/runbot/static/;
      autoindex off;
      location ~ /runbot/static/build/[^/]+/(logs|tests)/ {
          autoindex on;
          add_header 'Access-Control-Allow-Origin' 'http://runbot.domain.com';
      }
    }
}

server {  
    # config for running builds
    # subdomain redirect to the local runbot nginx with dynamic config
    # anothe nginx layer will listen to the 8080 port and redirect to the correct instance
    server_name  *.runbot.domain.com;
    location / {
        proxy_set_header Host $host:$proxy_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_pass http://127.0.0.1:8080;
    }
    # needed for v16.0 websockets
    location /websocket {
        proxy_set_header Host $host:$proxy_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://127.0.0.1:8080;
    }
}
