Flask OpenApi web service in uWSGI and NGinx

Viewed 56

I am having great problems getting this to work.

We have a web service which is configured in Flask with OpenApi.

During dev, it was setup within the if __name__ == '__main__' phase.

However, this is never called by uWSGI and so I have had to remove that condition.

Afterwards, the service was launched with an app.run(...) binding to a given ip and port. If I keep the app.run() call (with no parameters), the uWSGI log spits out the standard flask warning that it is only a dev server and should not be used for production.

Here is how the service is initialised :

import connexion
from flask_cors import CORS

from src.middlewares import Log as midLog
from src.middlewares import DB as midDb

import utils
from web.doc import bp_doc

# Instance a Flask server with Connexion library that validate openAPI
app = connexion.FlaskApp(__name__, specification_dir='specification/')

# DB init
app.app.wsgi_app = midDb.DB(app.app)

# Load OPEN API doc
utils.load_openapi()

# OpenAPI doc verif + CORS + Doc
app.add_api(sfile.File.path("tmp","config.adhoc","openapi.yaml"), options = {"swagger_ui": False},validate_responses=True,base_path="/")
CORS(app.app) # Build CORS Header
app.app.register_blueprint(bp_doc,url_prefix='/doc') # Register Blueprint

# Active Log
app.app.wsgi_app = midLog.Log(app.app,DEBUG)

#app.run()

The uWSGI ini file is configured thus :

[uwsgi]
module=wbsvc:application
wsgi-file=wbsvc.py

master = true
processes = 10

buffer-size=65535

socket = wbsvc.sock
chmod-socket = 660
vacuum = true
die-on-term = true

With the nginx config being like this :

server {
        listen 80;
        server_name localhost;

        client_max_body_size 10M;
        keepalive_timeout 60s;
        client_body_timeout 60s;

        sendfile on;
        client_body_buffer_size 20m;

        location /wbsvc {
                include uwsgi_params;
                uwsgi_pass unix:/home/webservice/svc/wbsvc.sock;
        }
}

The uWSGI server is setup as a service using the following definition :

    [Unit]
    Description=uWSGI configuration file for the web service
    After=network.target

    [Service]
    User=web_services
    Group=www-data

    WorkingDirectory=/home/webservice/svc/
    Environment="PATH=/home/webservice/svc/ws_env/bin"
    ExecStart=/home/webservice/svc/ws_env/bin/uwsgi --ini ws.ini

    [Install]
    WantedBy=multi-user.target

Everything runs up ok, but when I call the web service from a client system, there is a log created :

ERROR HANDLING
[pid: 17399|app: 0|req: 1/1] 81.250.236.200 () {40 vars in 734 bytes} [Fri Jul  8 11:52:24 2022] 
POST <url requested> => generated 106 bytes in 17 msecs (HTTP/1.1 500) 4 headers in 135 bytes (2 switches on core 0)

From my other logs, it would appear that the web service never gets called.

I am sure that other people have successfully setup a web service using python, flask, OpenApi, uWSGI and NGinx.

Can anyone help me out with this, I've been at it for over a week now with no real progress.

Extra details :

System : Ubuntu 18.04 LTS
Python 3.8
Nginx : 1.14.0
uWSGI : 2.0.20
Flask : 2.1.2

Edit :

Note that the requests are POST calls.

0 Answers
Related