import pymongo


client = pymongo.MongoClient("mongodb://IG:jdZwyec3Yb0yaBr8BPJoup1lfovAbGT342I2pX8wFqwCeLGvhXBLiL4vmhM@mongoprimary:27017/IG?authSource=IG")
db = client.get_database("IG")
collectionRes = db.get_collection("restaurant")
collectionLicense = db.get_collection("licence")
conf = ''

for restaurant in collectionRes.find():

    # dict has property 'domain' in restaurant
    if 'domain' in restaurant:
        print(restaurant['_id'])
        # find one license with restaurantId
        license = collectionLicense.find_one({'restaurantId': str (restaurant['_id'])})
           

        
        
        conf  +='''server { 
            server_name '''+restaurant['domain']+''';

            access_log /var/log/nginx/ig/acesss.'''+restaurant['domain']+'''.log;
            location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://127.0.0.1:8012;
                proxy_read_timeout 90;
                proxy_redirect off;
                proxy_http_version 1.1;
            }


        listen 443 ssl http2; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/demo.igarson.app/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/demo.igarson.app/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}


'''

# save conf to file
with open('apps.conf', 'w') as f:
    f.write(conf)
