# FILE:            /etc/nginx/sites-available/data.urn.arpa.conf
# DESCRIPTION:     (...)
#
# LICENSE:         Public Domain dedication
#                  SPDX-License-Identifier: Unlicense
#
# USAGE (draft):
#                  TODO: these commands are just a draft for if someone
#                        actually would be interested in create some manual or
#                        something.
#
#                  Update:
#                      code /etc/nginx/sites-available/data.urn.arpa.conf
#                  Deploy with:
#                      sudo adduser urn-data
#                      su - urn-data
#                      whoami
#                      # urn-data
#                      mkdir /home/data-urn/.local/
#                      mkdir /home/data-urn/.local/share/
#                      mkdir /home/data-urn/.local/share/urn
#                      echo "urn-data shared home" > /home/data-urn/.local/share/urn/
#
#                      ls -s /etc/nginx/sites-available/data.urn.arpa.conf /etc/nginx/sites-enabled/data.urn.arpa.conf
#                      sudo systemctl reload nginx
#                  Disable with:
#                      sudo rm /etc/nginx/sites-enabled/data.urn.arpa.conf
#                      sudo systemctl reload nginx


# https://www.nginx.com/blog/maximizing-python-performance-with-nginx-parti-web-serving-and-caching/

# http {
#     upstream urn-data-arpa-intranet {
#        server 127.0.0.1:8888;
#     }
# }

# TODO: maybe implement as socket, not port? 
#       https://docs.gunicorn.org/en/stable/deploy.html

server {
    listen 80;
    listen [::]:80;

    server_name data.urn.arpa;

    ### root for static file cache, START _____________________________________
    # On most basic cases, if a file already exist on disk, this will skip
    # the processing be passed to python

    # root /usr/share/urn/;
    # root /home/data-urn/.local/share/urn;
    root /home/fititnt/.local/share/urn/data/;

    ### root for static file cache, END _______________________________________

    # if the user try to load this block without bare minimum prefix, like
    # http://data.urn.arpa/urn:data, we will give a hint that this is wrong
    location / {
        add_header Content-Type text/plain;
        return 200 '/urn:data ?';
    }

    location /urn:data {
        # checks for static file, if not found proxy to app
        try_files $uri @proxy_to_app;
    }

    location /urn:data:ckan {
        return 200 'not implemented';
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;
        proxy_pass http://urn-data-arpa-intranet;
    }

    # location /urn/data/  {
    #     # root /usr/share/urn/;
    #     root /home/data-urn/.local/share/urn;
    #     # root /home/fititnt/.local/share/urn/data;
    # }

}