paths for deploying flask/wsgi differ from development

Viewed 22

I am developing an app and the development setup was really easy.

I run it locally with:

 $ . .venv/bin/activate
 (.venv) $ 
 (.venv) $ python -m flask run
  * Serving Flask app 'app'
  * Debug mode: on
 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  * Running on http://127.0.0.1:8080
 Press CTRL+C to quit
  * Restarting with stat
  * Debugger is active!
  * Debugger PIN: -###-###

and I have configured apache2 on my (ubuntu) laptop with:

 ProxyPass / http://127.0.0.1:8080

My code is structured like:

 app.py
 pages/scc_main/scc.html
 ...

The code has this:

import re

import jinja2
from flask import Flask
from flask import request

import data

app = Flask(__name__)
env = jinja2.Environment(loader=jinja2.FileSystemLoader("pages"))


@app.route('/')
def hello_world():
    return '<h2>Hello, World!</h2>'


@app.route('/contracts/scc')
@app.route('/contracts/scc/')
def contracts_main():
    main = env.get_template('scc_main/scc.html')
    context = data.build('scc_main')
    return main.render(**context)

And everything works great. As in:

 $ curl 'http://localhost/'
 <h2>Hello, World!</h2>$ 

But when I deploy. Wow. I set my site's root to point to the app. That is actually working. I can hit https://opencalaccess.org/ and it gets my static content.

I have:

 import sys
 import logging
 logging.basicConfig(
    level=logging.DEBUG,
    filename='/var/www/<full-path>/logs/contracts_scc.log',
    format='^(asctime)s %(message)s')
 sys.path.insert(0, '/var/www/<full-path>')
 sys.path.insert(0, '/var/www/<full-path>/.venv/lib/python3.8/site-packages')

And https://opencalaccess.org/contracts/scc works. But only after I change the Environment call above to:

 env = jinja2.Environment(loader=jinja2.FileSystemLoader("/var/www/full-path>/pages"))

Now, any link which is just a link is fine. But anything that looks at the flask.request.path gives me:

 The browser (or proxy) sent a request that this server could not understand.

What the heck? Setting up the dev environment was so easy. What do you have to do to get this working in deployment? Any suggestions?

ADDED:

Well, it seems clear that it is the WSGI part that is having the problem. My script is not receiving the request structure and so it cannot read any parameters. I have all my parameters on the URL, so my data building method reads the request.path to see what to do.

So, where to go from here. We will see.

0 Answers
Related